Skip to content

Instantly share code, notes, and snippets.

@nicdaCosta
Last active March 9, 2024 13:39
Show Gist options
  • Save nicdaCosta/4072412 to your computer and use it in GitHub Desktop.
Save nicdaCosta/4072412 to your computer and use it in GitHub Desktop.
Basic function that searches / filters any object or function and returns matched properties.
/*
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 : '';
}
@garycourt
Copy link

Performance improvement: https://gist.github.com/4132771

@nicdaCosta
Copy link
Author

@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. :)

@johan
Copy link

johan commented May 9, 2013

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment