Skip to content

Instantly share code, notes, and snippets.

@antonmedv
Created December 12, 2018 05:49
Show Gist options
  • Save antonmedv/065462763d750c9db93154dce996eaa1 to your computer and use it in GitHub Desktop.
Save antonmedv/065462763d750c9db93154dce996eaa1 to your computer and use it in GitHub Desktop.
Search Snippet
global.find = re => json => [...find(json, re)]
function* find(v, regex, path = '') {
if (regex.test(path)) {
yield path
return
}
if (typeof v === 'undefined' || v === null) {
return
}
if (Array.isArray(v)) {
let i = 0
for (let value of v) {
const prefix = (path === '' ? 'this' : path)
yield* find(value, regex, prefix + '[' + i++ + ']')
}
return
}
if (typeof v === 'object' && v.constructor === Object) {
const entries = Object.entries(v)
for (let [key, value] of entries) {
yield* find(value, regex, path + '.' + key)
}
return
}
if (regex.test(v)) {
yield path
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment