UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

70 lines (59 loc) 2.1 kB
export function treeReduce(nodes, fn, initialValue) { var childName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'children'; var parentNode = arguments.length > 4 ? arguments[4] : undefined; return nodes.reduce(function (previousValue, node, index) { var newValue = fn(previousValue, node, index, parentNode); var children = node[childName]; if (children && children.length) { return treeReduce(children, fn, newValue, childName, node); } return newValue; }, initialValue); } export function treeForEach(nodes, fn) { var childName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; var parentNode = arguments.length > 3 ? arguments[3] : undefined; nodes.forEach(function (node, index) { fn(node, index, parentNode); var children = node[childName]; if (children && children.length) { treeForEach(children, fn, childName, node); } }); } export function treeSome(nodes, fn) { var childName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; var parentNode = arguments.length > 3 ? arguments[3] : undefined; return nodes.some(function (node, index) { if (!fn(node, index, parentNode)) { var children = node[childName]; if (children && children.length) { return treeSome(children, fn, childName, node); } } return true; }); } export function treeFind(nodes, fn) { var childName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; var parentNode = arguments.length > 3 ? arguments[3] : undefined; var result; nodes.some(function (node, index) { var found = fn(node, index, parentNode); if (found) { result = node; return true; } var children = node[childName]; if (children && children.length) { var foundChild = treeFind(children, fn, childName, node); if (foundChild) { result = foundChild; return true; } } return false; }); return result; } //# sourceMappingURL=treeUtils.js.map