daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Dec 12th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global.find = re => json => [...find(json, re)]
  2.  
  3. function* find(v, regex, path = '') {
  4.   if (regex.test(path)) {
  5.     yield path
  6.     return
  7.   }
  8.  
  9.   if (typeof v === 'undefined' || v === null) {
  10.     return
  11.   }
  12.  
  13.   if (Array.isArray(v)) {
  14.     let i = 0
  15.     for (let value of v) {
  16.       const prefix = (path === '' ? 'this' : path)
  17.       yield* find(value, regex, prefix + '[' + i++ + ']')
  18.     }
  19.     return
  20.   }
  21.  
  22.   if (typeof v === 'object' && v.constructor === Object) {
  23.     const entries = Object.entries(v)
  24.     for (let [key, value] of entries) {
  25.       yield* find(value, regex, path + '.' + key)
  26.     }
  27.     return
  28.   }
  29.  
  30.   if (regex.test(v)) {
  31.     yield path
  32.   }
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top