Created
May 1, 2020 13:50
-
-
Save Lucifier129/14a7fa78259f32face8fe24eb4e9c4d1 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
const createTreeModule = (operations) => { | |
const append = (tree, node) => { | |
if (operations.matchChild(tree, node)) { | |
return operations.appendChild(tree, node); | |
} | |
return operations.map(tree, (child) => { | |
return append(child, node); | |
}); | |
}; | |
const fromList = (list) => { | |
return list.reduce((tree, item) => { | |
return append(tree, operations.itemToNode(item)); | |
}, operations.createRoot()); | |
}; | |
return { | |
...operations, | |
fromList, | |
append, | |
}; | |
}; | |
const TreeModule0 = createTreeModule({ | |
createRoot: () => { | |
return {}; | |
}, | |
itemToNode: (item) => { | |
return item; | |
}, | |
map: (node, f) => { | |
let newNode = {}; | |
for (let key in node) { | |
newNode[key] = f(node[key]); | |
} | |
return newNode; | |
}, | |
matchChild: (tree, node) => { | |
if (node.parent === null) return true; | |
return tree.hasOwnProperty(node.parent); | |
}, | |
appendChild: (tree, node) => { | |
if (node.parent === null) { | |
return { | |
...tree, | |
[node.id]: {}, | |
}; | |
} | |
return { | |
...tree, | |
[node.parent]: { | |
...tree[node.parent], | |
[node.id]: {}, | |
}, | |
}; | |
}, | |
}); | |
const TreeModule1 = createTreeModule({ | |
createRoot: () => { | |
return { | |
parent: null, | |
name: '', | |
children: [], | |
}; | |
}, | |
itemToNode: (item) => { | |
return { | |
parent: item.parent, | |
name: item.id, | |
children: [], | |
}; | |
}, | |
map: (node, f) => { | |
return { | |
...node, | |
children: node.children.map(f), | |
}; | |
}, | |
matchChild: (tree, node) => { | |
if (node.parent === null) return true; | |
return node.parent === tree.name; | |
}, | |
appendChild: (tree, node) => { | |
if (node.parent === null) return node; | |
return { | |
...tree, | |
children: [...tree.children, node], | |
}; | |
}, | |
}); | |
const categories = [ | |
{ id: 'animals', parent: null }, | |
{ id: 'mammals', parent: 'animals' }, | |
{ id: 'cats', parent: 'mammals' }, | |
{ id: 'dogs', parent: 'mammals' }, | |
{ id: 'chihuahua', parent: 'dogs' }, | |
{ id: 'labrador', parent: 'dogs' }, | |
{ id: 'persian', parent: 'cats' }, | |
{ id: 'siamese', parent: 'cats' }, | |
]; | |
const test = (TreeModule, name = '') => { | |
const tree0 = TreeModule.fromList(categories); | |
console.log(name, 'tree0', JSON.stringify(tree0, null, 2)); | |
const tree1 = TreeModule.append( | |
tree0, | |
TreeModule.itemToNode({ id: 'husky', parent: 'dogs' }) | |
); | |
console.log(name, 'tree1', JSON.stringify(tree1, null, 2)); | |
}; | |
test(TreeModule0, 'TreeModule0'); | |
console.log('-------------------------------------------') | |
test(TreeModule1, 'TreeModule1'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
codata in typescript