Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-Root #6

Merged
merged 4 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions cosmoz-default-tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,50 @@
* @param {string} pathLocatorSeparator [this.pathLocatorSeparator] (The string which separates the path.)
*/
Cosmoz.DefaultTree.prototype.getPathNodes = function (pathLocator, nodeObj, pathLocatorSeparator) {
var tree = nodeObj || this._treeData;

if (!pathLocator) {
return this._roots;
return tree;
}

return Object.keys(tree)
.map(function (key) {
var subTree = {};
subTree[key] = tree[key];
return this._getPathNodes(pathLocator, subTree, pathLocatorSeparator);
}, this)
.filter(function (item) {
return item && item.length > 0;
})
.sort(function (a, b) {
var undefCounter = function (item) {
return item === undefined;
},
defCounter = function (item) {
return item;
},
aUndefCount = a.filter(undefCounter).length,
bUndefCount = b.filter(undefCounter).length,
aDefCount = a.filter(defCounter).length,
bDefCount = b.filter(defCounter).length;

if (aUndefCount < bUndefCount) {
return -1;
}
if (aUndefCount > bUndefCount) {
return 1;
}
if (aDefCount < bDefCount) {
return 1;
}
if (aDefCount > bDefCount) {
return -1;
}
return 0;
})[0];
},
Cosmoz.DefaultTree.prototype._getPathNodes = function (pathLocator, nodeObj, pathLocatorSeparator) {

// Defaults
pathLocatorSeparator = pathLocatorSeparator || this.pathLocatorSeparator;

Expand All @@ -170,8 +210,11 @@

// Get the nodes on the path
nodes = path.map(function (nodeKey) {
node = pathSegment[nodeKey] !== node ? pathSegment[nodeKey] : undefined;
if (node && this.hasChildren(node)) {
if (!pathSegment) {
return;
}
node = pathSegment[nodeKey];
if (node) {
pathSegment = node[this.childProperty];
}
return node;
Expand Down
82 changes: 65 additions & 17 deletions test/cosmoz-tree_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -242,30 +242,78 @@
node301Path = missingAncestorTree.getPathNodes(node301.pathLocator),
node401Path = missingAncestorTree.getPathNodes(node401.pathLocator),
n_1_2_3_301 = missingAncestorTree.getPathNodes('1.2.3.301'),
n_1_2_301 = missingAncestorTree.getPathNodes('1.2.301'),
n_1_2 = missingAncestorTree.getPathNodes('1.2'),
n_2_301 = missingAncestorTree.getPathNodes('2.301');

assert.equal(node301Path.filter(function (n) { return n; }).length, 2);
assert.equal(node301.name, 'Node301')
assert.deepEqual(node301Path[2], node301);

assert.equal(node401Path.length, 4);
assert.equal(node401Path[2], node301);
assert.equal(node401Path[3], node401);
n_1_2_2 = missingAncestorTree.getPathNodes('1.2.2'),
n_1_4_4 = missingAncestorTree.getPathNodes('1.4.4'),
n_1_2_7_8 = missingAncestorTree.getPathNodes('1.2.7.8'),
n_1_2_7_301 = missingAncestorTree.getPathNodes('1.2.7.301'),
n_2_301 = missingAncestorTree.getPathNodes('2.301'),
n_601_301 = missingAncestorTree.getPathNodes('601.301.10.11');

// since 301 is its own root, it should be the only node returned
assert.equal(node301Path.length, 1);
assert.equal(node301.name, 'Node301');
assert.deepEqual(node301Path[0], node301);

// node401 is directly under 301, should only be two nodes returned
assert.equal(node401Path.length, 2);
assert.equal(node401Path[0], node301);
assert.equal(node401Path[1], node401);

// 1.2.3.301
assert.equal(n_1_2_3_301[0].name, 'Node2');
assert.isNotOk(n_1_2_3_301[1]);
assert.equal(n_1_2_3_301[2].name, 'Node301');
assert.equal(n_1_2_3_301[0].name, 'Node301');

// 1.2.301
// make sure we don't get stuck in the first root
assert.equal(n_1_2_301[0].name, 'Node301');
assert.equal(n_1_2_301.length, 1);

// 1.2
assert.equal(n_1_2.length, 1);
assert.equal(n_1_2[0].name, 'Node2');
assert.equal(n_1_2.length, 2);
assert.equal(n_1_2[0].name, 'Node1');
assert.equal(n_1_2[1].name, 'Node2');

// 1.2.2
// make sure we don't pick the same node twice at the end
assert.equal(n_1_2_2.length, 3);
assert.equal(n_1_2_2[0].name, 'Node1');
assert.equal(n_1_2_2[1].name, 'Node2');
assert.isNotOk(n_1_2_2[2]);

// 1.4.4
// make sure we handle nodes without 'children' property
assert.equal(n_1_4_4.length, 3);
assert.equal(n_1_4_4[0].name, 'Node1');
assert.equal(n_1_4_4[1].name, 'Nochildren');
assert.isNotOk(n_1_4_4[2]);

// 1.2.7.8
// make sure we handle multiple undefineds at the end
assert.equal(n_1_2_7_8.length, 4);
assert.equal(n_1_2_7_8[0].name, 'Node1');
assert.equal(n_1_2_7_8[1].name, 'Node2');
assert.isNotOk(n_1_2_7_8[2]);
assert.isNotOk(n_1_2_7_8[3]);

// 601.301.10.11
// same amount of undefined values: make sure the match with more defined values
// is in favor
assert.equal(n_601_301.length, 4);
assert.equal(n_601_301.filter(function (n) {
return n;
}).length, 2);

// 1.2.7.301 - impossible case ?
// assert.equal(n_1_2_7_301.length, 4);
// assert.equal(n_1_2_7_301[0].name, 'Node1');
// assert.equal(n_1_2_7_301[1].name, 'Node2');
// assert.isNotOk(n_1_2_7_301[2]);
// assert.isNotOk(n_1_2_7_301[3]);

// 2.301
assert.equal(n_2_301.length, 2);
assert.equal(n_2_301[0].name, 'Node2');
assert.equal(n_2_301[1].name, 'Node301');
assert.equal(n_2_301.length, 1);
assert.equal(n_2_301[0].name, 'Node301');
});
});
}());
Expand Down
48 changes: 42 additions & 6 deletions test/data/missingAncestorTree.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{
"2": {
"id": "167d1485-7d4f-4c7d-86cd-a4fb00f31245",
"name": "Node2",
"pathLocator": "1.2",
"children": {}
},

"301": {
"id": "3a7654f1-e3e6-49c7-b6a8-a4fb00f31245",
Expand All @@ -20,5 +14,47 @@

}
}
},

"601": {
"id": "f8e2a26d-c5cb-45b4-9551-1cd0bkodke676297b",
"name": "Node601",
"pathLocator": "1.601.301",
"children": {
"301": {
"id": "167d1485-7d4f-4c7d-8496cd-a4fb00f31245",
"name": "Node601_301_2",
"pathLocator": "1.601.301.2",
"children": {
}
}
}
},

"1": {
"id": "f8e2a26d-c5cb-45b4-9551-1cd0b676297b",
"name": "Node1",
"pathLocator": "1",
"children": {
"2": {
"id": "167d1485-7d4f-4c7d-86cd-a4fb00f31245",
"name": "Node2",
"pathLocator": "1.2",
"children": {
}
},
"4": {
"id": "7554a7e3-b676-4a66-8d54-d55b8da9c473",
"name": "Nochildren",
"pathLocator": "1.4"
},
"301": {
"id": "37990cf3-0cc9-42ae-94d2-8c584004b20a",
"name": "Node301 wrong",
"pathLocator": "1.301",
"children": {
}
}
}
}
}