MCPcopy Create free account
hub / github.com/Autodesk/react-base-table / flattenOnKeys

Function flattenOnKeys

src/utils.js:101–121  ·  view source on GitHub ↗
(tree, keys, depthMap = {}, dataKey = 'id')

Source from the content-addressed store, hash-verified

99}
100
101export function flattenOnKeys(tree, keys, depthMap = {}, dataKey = 'id') {
102 if (!keys || !keys.length) return tree;
103
104 const array = [];
105 const keysSet = new Set();
106 keys.forEach(x => keysSet.add(x));
107
108 let stack = [].concat(tree);
109 stack.forEach(x => (depthMap[x[dataKey]] = 0));
110 while (stack.length > 0) {
111 const item = stack.shift();
112
113 array.push(item);
114 if (keysSet.has(item[dataKey]) && Array.isArray(item.children) && item.children.length > 0) {
115 stack = [].concat(item.children, stack);
116 item.children.forEach(x => (depthMap[x[dataKey]] = depthMap[item[dataKey]] + 1));
117 }
118 }
119
120 return array;
121}
122
123// Babel7 changed the behavior of @babel/plugin-transform-spread in https://github.com/babel/babel/pull/6763
124// [...array] is transpiled to array.concat() while it was [].concat(array) before

Callers 1

constructorMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected