Skip to content

Instantly share code, notes, and snippets.

@JakeTheCorn
Created January 22, 2018 15:42
Show Gist options
  • Save JakeTheCorn/0d5386fb4e722c2b95c02a86daecea41 to your computer and use it in GitHub Desktop.
Save JakeTheCorn/0d5386fb4e722c2b95c02a86daecea41 to your computer and use it in GitHub Desktop.
var node = mockNode();
mapTree(node, upCase);
function upCase(string) {
return string.toUpperCase();
}
function mapTree(node, mapper) {
return {
value: mapper(node.value),
nodes: node.nodes ? node.nodes.map(x => mapTree(x, mapper)) : null
};
}
function mockNode() {
return {
value: 'john',
nodes: [
{
value: 'amy',
nodes: [
{ value: 'sandy' },
{ value: 'art' }
]
},
{
value: 'billy',
nodes: [
{ value: 'ruby' },
{ value: 'sean' }
]
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment