{"id":888,"date":"2013-03-12T12:48:46","date_gmt":"2013-03-12T12:48:46","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=888"},"modified":"2013-03-12T12:48:46","modified_gmt":"2013-03-12T12:48:46","slug":"grep-js","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/grep-js\/","title":{"rendered":"Grep.js"},"content":{"rendered":"<p>Inspired by a client project with thousands of lines of poorly structured, badly written ExtJS code, I wrote a grep implementation to recursively search the contents of Javascript variables, <a href=\"https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js\">available on github<\/a>.<\/p>\n<p>This provides a single function &#8220;grep&#8221;, which can be used directly or added to the global namespace. It recursively searches objects &#8211; keys, values, arrays and functions &#8211; for values matching a regular expression. It does a breadth-first search, stopping after a number of entries.It returns a stringified versions of the variable name and value, so you can see exactly what you&#8217;d need to write to get the desired value. e.g:<\/p>\n<h2>Tests<\/h2>\n<pre lang=\"javascript\">var lib = ...contents of grep.js...\nlib.test();<\/pre>\n<p>testTopLevelArray: Passed<br \/>\ntestSubLevelArray: Passed<br \/>\ntestInt: Passed<br \/>\ntestString: Passed<br \/>\ntestRecursion: Passed<br \/>\ntestObject: Passed<br \/>\ntestSubObject: Passed<br \/>\ntestFunction: Passed<br \/>\ntestWindow: Passed<br \/>\ntestRemoveTopLevel: Passed<br \/>\ntestRemoveTopLevel: Passed<br \/>\ntestRemoveSecondLevel: Passed<br \/>\ntestRemoveSecondLevel: Passed<br \/>\ntestKeys: Passed<br \/>\ntestValues: Passed<\/p>\n<h2>Installation<\/h2>\n<p>You can call lib.grep directly, or call install to place it in the global namespace:<\/p>\n<pre lang=\"javascript\">lib.install();<\/pre>\n<h2>Examples<\/h2>\n<p>Since &#8220;document&#8221; is a global, grep returns values you can use to access the results:<\/p>\n<pre lang=\"javascript\">grep(\"document\", \"location\")<\/pre>\n<p>[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]<\/p>\n<pre lang=\"javascript\">grep(\"document\", \"location\").keys()<\/pre>\n<p>[&#8220;document.location&#8221;, &#8220;document.location.assign&#8221;, &#8220;document.location.replace&#8221;, &#8220;document.location.reload&#8221;, &#8220;document.location.ancestorOrigins&#8221;, &#8220;document.location.origin&#8221;, &#8220;document.location.hash&#8221;, &#8220;document.location.search&#8221;, &#8220;document.location.pathname&#8221;, &#8220;document.location.port&#8221;]<\/p>\n<pre lang=\"javascript\">grep(\"document\", \"location$\").keys();<\/pre>\n<p>[&#8220;document.location&#8221;, &#8220;document.defaultView.location&#8221;, &#8220;document.activeElement.ownerDocument.location&#8221;, &#8220;document.head.ownerDocument.location&#8221;, &#8220;document.body.ownerDocument.location&#8221;, &#8220;document.defaultView.top.location&#8221;, &#8220;document.defaultView.window.location&#8221;, &#8220;document.defaultView.document.location&#8221;, &#8220;document.defaultView.Modernizr.geolocation&#8221;, &#8220;document.documentElement.classList.7&#8221;]<\/p>\n<pre lang=\"javascript\">grep(\"document\", \"location$\").values()<\/pre>\n<p>[&#8220;https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js&#8221;, &#8220;https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js&#8221;, &#8220;https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js&#8221;, &#8220;https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js&#8221;, &#8220;https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js&#8221;, &#8220;https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js&#8221;, &#8220;https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js&#8221;, &#8220;https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js&#8221;, &#8220;true&#8221;, &#8220;geolocation&#8221;]<\/p>\n<p><a href=\"https:\/\/github.com\/garysieling\/grep-js\/blob\/master\/search\/grep.js\">Get it on github, here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inspired by a client project with thousands of lines of poorly structured, badly written ExtJS code, I wrote a grep implementation to recursively search the contents of Javascript variables, available on github. This provides a single function &#8220;grep&#8221;, which can be used directly or added to the global namespace. It recursively searches objects &#8211; keys, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/grep-js\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Grep.js&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[13],"tags":[261,302,440,495],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/888"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=888"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/888\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}