Created
August 24, 2018 11:11
-
-
Save tomasreichmann/ce2455b68dbae6139632ea519ecea120 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getTreeLeaves = (tree: any, getChildren = (node: any) => node && node.children) => { | |
const leaves = []; | |
const nodes = Array.isArray(tree) ? [...tree] : [tree]; | |
let node; | |
let children; | |
while ( | |
// tslint:disable-next-line:no-conditional-assignment ban-comma-operator | |
node = nodes.shift(), children = getChildren(node), | |
((children && children.length) || (leaves.push(node) && nodes.length)) | |
) { | |
if (children) { | |
nodes.push(...children); | |
} | |
} | |
return leaves; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment