util-helpers
Version:
34 lines (31 loc) • 1.12 kB
JavaScript
import { isArray, forEach, isObject } from 'ut2';
function internalFindTreeSelect(tree, predicate, childrenField, path) {
if (path === void 0) { path = []; }
var result = [];
if (isArray(tree)) {
forEach(tree, function (item) {
path.push(item);
if (predicate(item)) {
result = path;
return false;
}
if (isObject(item)) {
var childs = item[childrenField];
if (isArray(childs) && childs.length > 0) {
var findChildren = internalFindTreeSelect(childs, predicate, childrenField, path);
if (findChildren.length > 0) {
result = findChildren;
return false;
}
}
}
path.pop();
});
}
return result;
}
function findTreeSelect(tree, predicate, childrenField) {
if (childrenField === void 0) { childrenField = 'children'; }
return internalFindTreeSelect(tree, predicate, childrenField);
}
export { findTreeSelect as default };