This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as path from 'path'; | |
import * as url from 'url'; | |
function forEachDeep(obj, cb, options = { depth: 6 }) { | |
(function walk(value, property = undefined, parent = null, objPath = []) { | |
return value && typeof value === 'object' && objPath.length <= options.depth | |
? Object.entries(value).forEach(([key, val]) => | |
walk(val, key, value, [...objPath, key]) | |
) | |
: cb([property, value], parent, objPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const print = x => console.log(JSON.stringify(x, null, 2)); | |
const animals = [ | |
{ value: 'mammals', parent: 'animals' }, | |
{ value: 'animals', parent: null }, | |
{ value: 'poodle', parent: 'dogs' }, | |
{ value: 'dogs', parent: 'mammals' }, | |
{ value: 'labrador',parent: 'dogs' }, | |
{ value: 'siamese', parent: 'cats' }, | |
{ value: 'cats', parent: 'mammals' }, |