Advertisement
Guest User

Untitled

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