Skip to content

Commit

Permalink
making Node and Forestry compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcdonald committed Mar 1, 2016
1 parent b88d9f7 commit da1f5ad
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 27 deletions.
18 changes: 10 additions & 8 deletions src/Forestry.js
Expand Up @@ -16,20 +16,22 @@ export default class Forestry extends Node {
return children.map((child, idx) => this._children[idx] ? new Forestry(child, this._childrenProp, this) : child);
}

_setChildren(children) {
this._children = children.map(child => child instanceof Forestry);
set children(children) {
this._children = children.map(child => child instanceof Forestry && child._childrenProp === this._childrenProp);
this.data[this._childrenProp] = children.map((child, idx) => this._children[idx] ? child.data : child);
}

addChild(...children) {
children = children.map(child => new Forestry(child, this._childrenProp, this));
this._setChildren(this.children.concat(children));
children = children.map(child => {
if (child instanceof Node) {
child.parent = this;
return child;
}
return new Forestry(child, this._childrenProp, this)
});
this.children = this.children.concat(children);
}

// clone() {
// return new Forestry(cloneData(this.data), this._childrenProp);
// }

_clone() {
return new Forestry(cloneData(this.data, this._childrenProp), this._childrenProp);
}
Expand Down
28 changes: 16 additions & 12 deletions src/Node.js
Expand Up @@ -14,7 +14,7 @@ export default class Node {
return this._children.slice();
}

_setChildren(children) {
set children(children) {
this._children = children;
}

Expand Down Expand Up @@ -54,8 +54,14 @@ export default class Node {
}

addChild(...children) {
children = children.map(child => new Node(child, this));
this._setChildren(this.children.concat(children));
children = children.map(child => {
if (child instanceof Node) {
child.parent = this;
return child;
}
return new Node(child, this)
});
this.children = this.children.concat(children);
}

remove() {
Expand All @@ -64,7 +70,7 @@ export default class Node {
}
let children = this.parent.children;
children.splice(this.index, 1);
this.parent._setChildren(children);
this.parent.children = children;
this.parent = null;
return this;
}
Expand Down Expand Up @@ -101,26 +107,24 @@ export default class Node {
mapped = newNode;
return;
}
let children = node.parent.children,
idx = node.index;
children.splice(idx, 1, newNode);
node.parent._setChildren(children);
let children = node.parent.children;
children.splice(node.index, 1, newNode)
node.parent.children = children;
}, TRAVERSAL_TYPES.DFS_POST);
return mapped;
}

clone() {
return this.reduce((root, node) => {
let clone = node._clone();
clone.children = [];
if (!root) {
let clone = this._clone();
clone._setChildren([]);
return clone;
}
let route = node.climb((node, route) => [node.index].concat(route), []),
parent = route.slice(1, -1)
.reduce((node, idx) => node.children[idx], root);
parent.addChild(cloneData(node.data, node._childrenProp));
parent.children[parent.children.length - 1]._setChildren([]);
parent.addChild(clone);
return root;
});
}
Expand Down
113 changes: 113 additions & 0 deletions test/Mixed.spec.js
@@ -0,0 +1,113 @@
import tape from 'tape';
import Forestry from '../src/Forestry';
import Node from '../src/Node';
import climbTest from './common/climb';
import traverseTest from './common/traverse';
import findTest from './common/find';
import filterTest from './common/filter';
import reduceTest from './common/reduce';
import cloneTest from './common/clone';
import mapTest from './common/map';


const nodeWithinForestrySetup = (dataGen) => {
const [d1, d2, d3, d4, d5] = dataGen()();
let model = {
reading: d1,
echoes: []
},
root = new Forestry(model, 'echoes'),
node = new Node(d2);
node.addChild(d4, d5);
root.addChild(node, { reading: d3 });
return root;
}

const forestryWithinNodeSetup = (dataGen) => {
const [d1, d2, d3, d4, d5] = dataGen()();
let model = {
reading: d2,
echoes: [{
reading: d4
}, {
reading: d5
}]
},
root = new Node(d1),
node = new Forestry(model, 'echoes');
root.addChild(node, d3);
return root;
}

const forestryWithinForestrySetup = (dataGen) => {
const [d1, d2, d3, d4, d5] = dataGen()();
let model = {
reading: d2,
echoes: [{
reading: d4
}, {
reading: d5
}]
},
root = new Forestry({ guffaws: d1, energy: []}, 'energy'),
node = new Forestry(model, 'echoes');
root.addChild(node, { guffaws: d3 });
return root;
}

const setData = (node, val) => {
if (node instanceof Forestry) {
if (node.data.hasOwnProperty('reading')) {
node.data.reading = val;
}
node.data.guffaws = val
} else {
node.data = val;
}
}
const getData = node => {
if (node instanceof Forestry) {
if (node.data.hasOwnProperty('reading')) {
return node.data.reading;
}
return node.data.guffaws;
} else {
return node.data;
}
}

tape('Mixed Tree I - Node Within Forestry Tree', t => {

climbTest(t, nodeWithinForestrySetup);
traverseTest(t, nodeWithinForestrySetup, getData, setData);
findTest(t, nodeWithinForestrySetup, getData);
filterTest(t, nodeWithinForestrySetup, getData);
reduceTest(t, nodeWithinForestrySetup, getData);
cloneTest(t, nodeWithinForestrySetup, getData, setData);
mapTest(t, nodeWithinForestrySetup, getData, setData);

});

tape('Mixed Tree II - Forestry Tree Within Node', t => {

climbTest(t, forestryWithinNodeSetup);
traverseTest(t, forestryWithinNodeSetup, getData, setData);
findTest(t, forestryWithinNodeSetup, getData);
filterTest(t, forestryWithinNodeSetup, getData);
reduceTest(t, forestryWithinNodeSetup, getData);
cloneTest(t, forestryWithinNodeSetup, getData, setData);
mapTest(t, forestryWithinNodeSetup, getData, setData);

});

tape('Mixed Tree III - Forestry Tree Within Forestry Tree', t => {

climbTest(t, forestryWithinForestrySetup);
traverseTest(t, forestryWithinForestrySetup, getData, setData);
findTest(t, forestryWithinForestrySetup, getData);
filterTest(t, forestryWithinForestrySetup, getData);
reduceTest(t, forestryWithinForestrySetup, getData);
cloneTest(t, forestryWithinForestrySetup, getData, setData);
mapTest(t, forestryWithinForestrySetup, getData, setData);

});
14 changes: 7 additions & 7 deletions test/common/map.js
Expand Up @@ -25,13 +25,13 @@ export default (t, setup, getData, setData) => {
t.test('maps to arbitary objects', t => {
t.test(1);
const [d1, d2, d3, d4, d5] = simpleDataGen()();
let root = setup(simpleDataGen),
mapped = root.map(node => {
return {
data: getData(node),
children: node.children
}
});
let root = setup(simpleDataGen);
let mapped = root.map(node => {
return {
data: getData(node),
children: node.children
}
});
t.deepEqual(mapped, {
data: d1,
children: [{
Expand Down

0 comments on commit da1f5ad

Please sign in to comment.