Guest User

Untitled

a guest
Dec 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  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. }
Add Comment
Please, Sign In to add comment