{"id":2379,"date":"2014-12-19T20:03:30","date_gmt":"2014-12-19T20:03:30","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=2379"},"modified":"2014-12-19T20:06:55","modified_gmt":"2014-12-19T20:06:55","slug":"shorthand-substring-search-function-javascript","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/shorthand-substring-search-function-javascript\/","title":{"rendered":"A shorthand substring search function for JavaScript"},"content":{"rendered":"<p>Being a full stack developer, I&#8217;ve grown to love certain things about many different languages. The problem with that, however, is that I often find it annoying when doing something in one language is so much easier in another language.<\/p>\n<p>Just today, I finally became annoyed enough with the JavaScript indexOf function that I decided to write a quick function for the String prototype.<\/p>\n<p>If you&#8217;ve used Python for anything more than 5 minutes, you&#8217;ll instantly fall in love with the &#8220;in&#8221; syntax, and how flexible it is across strings or lists, i.e.<\/p>\n<pre>if 'bob' in 'bobcat'<\/pre>\n<p>After using that super-simple syntax on the back-end, you get a little annoyed when you have to write dozens of string search functions in JavaScript like so:<\/p>\n<pre>if ('bobcat'.indexOf('bob') !== -1)<\/pre>\n<p>Of course, a single conditional like that is nothing too daunting. But, when you have to run dozens of comparisons, it gets a little monotonous. Not to mention, the code looks a little sloppy.<\/p>\n<p>So, to get past this, I just threw together a quick String prototype function:<\/p>\n<pre class=\"prettyprint\"><code>String.prototype.in = function (needle, case_sensitive) {\r\n   if (typeof case_sensitive === 'undefined')\r\n      case_sensitive = false;\r\n\r\n   if (case_sensitive)\r\n      return (needle.indexOf(this) !== -1);\r\n   else\r\n      return (needle.toLowerCase().indexOf(this.toLowerCase()) !== -1);\r\n}<\/code><\/pre>\n<p>Now, I can feel a little happier as I write my string search functions like so:<\/p>\n<pre>if ('bob'.in('bobcat'))<\/pre>\n<p>&nbsp;<\/p>\n<p>So to return to that whole bit about loving different features of different languages, it goes without saying that I absolutely love the Prototype concept in JavaScript!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Make your life about 1% easier by creating a convenience substring search method on the JavaScript String prototype.<\/p>\n","protected":false},"author":1,"featured_media":2381,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[5],"tags":[227],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2379"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=2379"}],"version-history":[{"count":4,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2379\/revisions"}],"predecessor-version":[{"id":2384,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2379\/revisions\/2384"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media\/2381"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=2379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=2379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=2379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}