public
Last active

Basic function that searches / filters any object or function and returns matched properties.

  • Download Gist
Grep.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/*
Grep.js
Author : Nic da Costa ( @nic_daCosta )
Created : 2012/11/14
Version : 0.2
(c) Nic da Costa
License : MIT, GPL licenses
 
Overview:
Basic function that searches / filters any object or function and returns matched properties.
This receives either a string or regex as the initial parameter( strSearch ), the sencond parameter ( isRecursive == Bool) is
used to determine whether to search the property's properties should the current property be of type object / function.
Main area of use would be for DevTools purposes, when needing to find a specific property but only part of
the property name is known.
Is now an inherited method on any Object / Function due to Object.prototype.
Thus can go ObjectName.Grep( 'SearchTerm' ).
eg: navigator.Grep( 'geo' );
var foo = function(){}; foo.Grep( 'proto' );
 
*Tested in Chrome Dev Tools*
 
*/
 
 
Object.prototype.Grep = function( strSearch , isRecursive ) {
 
// Checks if seach string is not empty/undefined
if ( !strSearch ) {
 
return this;
 
};
 
// Used to prevent maxing out callstack for sub-lookups due to __proto__ == Object
isRecursive = isRecursive || false;
 
// Declare necessary local variables to hold necessary values
var objToIterate = this,
typeOfObject = typeof objToIterate,
objKeys = [],
objResult = {};
 
// if item that needs to be iterated over is an object or function, get all properties ( including non enumerable properties )
if ( typeOfObject === 'object' || typeOfObject === 'function' ) {
objKeys = Object.getOwnPropertyNames( objToIterate );
 
};
 
// Loop through all the properties
objKeys.forEach( function( item ) {
 
var itemValue;
/*
Initially check if search phrase is a regular expression, if so check if there is a match, else
check if key matches search string, if so add, if not, check if object and iterate through object's keys
*/
if ( ( strSearch instanceof RegExp ) ? item.match( strSearch ) : item.toLowerCase().indexOf( strSearch.toLowerCase() ) >= 0 ) {
 
itemValue = objToIterate[ item ];
 
}
else if ( typeof objToIterate[ item ] === 'object' && !isRecursive ){
 
itemValue = Grep.call( objToIterate[ item ] , strSearch , true );
 
}
 
// Check if Item Value has a value, if so, add to results
if ( itemValue ) {
 
objResult[ item ] = itemValue;
 
}
 
} );
 
// checks if objResult is empty, if so, return empty string.
return ( Object.getOwnPropertyNames( objResult ).length ) ? objResult : '';
 
}

Been using this a few times lately. Really like it. Thank you.

@paulirish pleasure! Glad you like it and more importantly find it useful.

@garycourt thanks for the code. I have run basics tests, and you are correct, there are improvements but in majority it is slight ( except in safari ). Here is the jsPerf link should anyone be interested. As it all comes down to the difference between forEach vs for loop.

One small thing though, is adding in the counter may cause it to yield incorrect results, due to counter++ if the property is an object. As that object may not always yield a result, so you would end up with alot of properties in your returned object that are blank strings.

Never the less, thanks for the suggestions! I plan on giving it a slight over haul shortly to also resolve some other smaller issues so will work it in. :)

Isn't grepping among only non-inherited properties the atypical use case? With this implementation, document.Grep('get') finds all sorts of things, but not document.getElementById (which is inherited from Document.prototype).

https://gist.github.com/johan/5550897 implements a grep(document, 'get') which does, and doesn't pollute object prototypes, which often breaks code. (Also a grep.own which works more like this one, for when you need that.)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.