From 8c6a8d858edf6f75e19294f4066c252e64d11184 Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Sat, 25 Jan 2020 20:49:40 -0800 Subject: [PATCH] chore: improved performance --- lib/find-in-tree.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/find-in-tree.js b/lib/find-in-tree.js index ff386b85..38aa5d5c 100644 --- a/lib/find-in-tree.js +++ b/lib/find-in-tree.js @@ -1,8 +1,6 @@ 'use strict' 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 // 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}]}} // selector: **[*].bar const findInTree = (tree, selector, onResult) => { - const scan = scanner([selector]) - for (const pathStr of scan(tree)) { - const path = toPath(pathStr) - const val = get(tree, path) - const parent = get(tree, path.slice(0, -1), {}) - onResult(val, parent, path) - } + scanner([selector], { + joined: false, + filterFn: (key, value, { parents }) => { + onResult(value, parents[0], key); + } + })(tree); } module.exports = findInTree