Skip to content
Permalink
Browse files

Search on keys, not paths

  • Loading branch information...
antonmedv committed Dec 15, 2018
1 parent bb79c98 commit 35d5d741f4d768b12ce0d5291f9b1a2ad6fba856
Showing with 7 additions and 6 deletions.
  1. +7 −6 find.js
13 find.js
@@ -1,11 +1,6 @@
'use strict'

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

if (typeof v === 'undefined' || v === null) {
return
}
@@ -21,7 +16,13 @@ function* find(v, regex, path = []) {
if (typeof v === 'object' && v.constructor === Object) {
const entries = Object.entries(v)
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
}

0 comments on commit 35d5d74

Please sign in to comment.
You can’t perform that action at this time.