mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 23:29:35 +02:00
24 lines
664 B
JavaScript
24 lines
664 B
JavaScript
|
'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
|
||
|
// the path to the item.
|
||
|
// Example:
|
||
|
// 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)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = findInTree
|