Last active
June 15, 2020 22:59
-
-
Save fabiomontefuscolo/56b0d99a5ec23530c5a44f608b38820f to your computer and use it in GitHub Desktop.
Search and inspect helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.defineProperty( | |
Object.prototype, 'inspect_walk', { | |
enumerable: false, | |
value: function (compare, callback) { | |
var visited = []; | |
var stack = [{ | |
'path' : [], | |
'key' : '', | |
'value' : this | |
}]; | |
var last, keys, key, val; | |
while(stack.length > 0) { | |
last = stack[ stack.length - 1 ]; | |
if(last.value && visited.indexOf(last.value) < 0 && typeof last.value === 'object') { | |
keys = Object.keys(last.value); | |
visited.push(last.value) | |
for(var i = 0; i < keys.length; i++) { | |
key = keys[i]; | |
val = last.value[key]; | |
stack.push({ | |
'parent': last, | |
'path' : last.path.concat(key), | |
'key' : key, | |
'value' : val | |
}); | |
} | |
} else { | |
cur = stack.pop(); | |
if (compare(cur)) { | |
callback(cur); | |
} | |
} | |
} | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment