chore: improved performance

This commit is contained in:
Lukas Siemon 2020-01-25 20:49:40 -08:00 committed by Jannis Redmann
parent e9699f98ba
commit 8c6a8d858e

View file

@ -1,8 +1,6 @@
'use strict' 'use strict'
const scanner = require('object-scan') const scanner = require('object-scan')
const toPath = require('lodash/toPath')
const get = require('lodash/get')
// For all items in `tree` matching a path selector specified // For all items in `tree` matching a path selector specified
// in `selector`, call `onResult` with the item, its parent and // in `selector`, call `onResult` with the item, its parent and
@ -11,13 +9,12 @@ const get = require('lodash/get')
// tree: {foo: [{bar: 1}], hey: {there: [{bar: 2}]}} // tree: {foo: [{bar: 1}], hey: {there: [{bar: 2}]}}
// selector: **[*].bar // selector: **[*].bar
const findInTree = (tree, selector, onResult) => { const findInTree = (tree, selector, onResult) => {
const scan = scanner([selector]) scanner([selector], {
for (const pathStr of scan(tree)) { joined: false,
const path = toPath(pathStr) filterFn: (key, value, { parents }) => {
const val = get(tree, path) onResult(value, parents[0], key);
const parent = get(tree, path.slice(0, -1), {})
onResult(val, parent, path)
} }
})(tree);
} }
module.exports = findInTree module.exports = findInTree