Skip to content

Commit

Permalink
#129: Keep order of tree entries returned by the data provider
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Hufmann <Bernd.Hufmann@ericsson.com>
  • Loading branch information
bhufmann committed Oct 16, 2020
1 parent da6b753 commit 972030d
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ const entryToTreeNode = (entry: Entry) => ({
export const listToTree = (list: Entry[]): TreeNode[] => {
const rootNodes: TreeNode[] = [];
const lookup: { [key: string]: TreeNode } = {};
// Fill-in the lookup table
list.forEach(entry => {
lookup[entry.id] = entryToTreeNode(entry);
});
Object.keys(lookup).forEach(id => {
const entry = lookup[id];
if (entry.parentId === -1) {
entry.isRoot = true;
rootNodes.push(entry);
} else if (entry.parentId in lookup) {
const p = lookup[entry.parentId];
p.children.push(entry);
// Create the tree in the order it has been received
list.forEach(entry => {
const node = lookup[entry.id];
if ((entry.parentId !== undefined) && (entry.parentId !== -1)) {
const parent: TreeNode = lookup[entry.parentId];
if (parent) {
parent.children.push(node);
} else {
// no parent available, treat is as root node
node.isRoot = true;
rootNodes.push(node);
}
} else {
node.isRoot = true;
rootNodes.push(node);
}
});
return rootNodes;
Expand Down

0 comments on commit 972030d

Please # to comment.