mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
20 lines
510 B
JavaScript
20 lines
510 B
JavaScript
'use strict'
|
|
|
|
const scanner = require('object-scan')
|
|
|
|
// 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) => {
|
|
scanner([selector], {
|
|
joined: false,
|
|
filterFn: (key, value, { parents }) => {
|
|
onResult(value, parents[0], key);
|
|
}
|
|
})(tree);
|
|
}
|
|
|
|
module.exports = findInTree
|