Skip to content

Commit

Permalink
Search on keys, not paths
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Dec 15, 2018
1 parent bb79c98 commit 35d5d74
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions find.js
@@ -1,11 +1,6 @@
'use strict' 'use strict'


function* find(v, regex, path = []) { function* find(v, regex, path = []) {
if (regex.test(path.join(''))) {
yield path
return
}

if (typeof v === 'undefined' || v === null) { if (typeof v === 'undefined' || v === null) {
return return
} }
Expand All @@ -21,7 +16,13 @@ function* find(v, regex, path = []) {
if (typeof v === 'object' && v.constructor === Object) { if (typeof v === 'object' && v.constructor === Object) {
const entries = Object.entries(v) const entries = Object.entries(v)
for (let [key, value] of entries) { for (let [key, value] of entries) {
yield* find(value, regex, path.concat(['.' + key])) const nextPath = path.concat(['.' + key])

if (regex.test(key)) {
yield nextPath
}

yield* find(value, regex, nextPath)
} }
return return
} }
Expand Down

0 comments on commit 35d5d74

Please sign in to comment.