Skip to content

Commit 8ad9819

Browse files
committed
Update package.json
2 parents 7c707af + 7640621 commit 8ad9819

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flattree",
3-
"version": "0.9.0",
3+
"version": "0.10.0",
44
"description": "Convert hierarchical tree structure to flat structure.",
55
"main": "lib/index.js",
66
"scripts": {

src/node.js

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ class Node {
1111

1212
this.children = this.children || [];
1313
}
14+
// Returns a boolean value indicating whether a node is a descendant of a given node or not.
15+
// @param {object} node Specifies the node that may be contained by (a descendant of) a specified node.
16+
// @return {boolean} Returns true if a node is a descendant of a specified node, otherwise false. A descendant can be a child, grandchild, great-grandchild, and so on.
17+
contains(node) {
18+
while ((node instanceof Node) && (node !== this)) {
19+
if (node.parent === this) {
20+
return true;
21+
}
22+
node = node.parent;
23+
}
24+
return false;
25+
}
1426
// Gets a child node at the specified index.
1527
// @param {number} The index of the child node.
1628
// @return {object} Returns an object that defines the node, null otherwise.

test/index.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,28 @@ test('[flatten] Ensure the parent node is an instance of Node after calling flat
565565
t.end();
566566
});
567567

568-
test('[node] getChildren/getParent/getFirstChild/getPreviousSibling/getNextSibling', (t) => {
568+
test('[node] Node API', (t) => {
569569
const tree = JSON.parse(fixtures.tree);
570570
const nodes = flatten(tree, { openAllNodes: true });
571571
const root = _.find(nodes, node => node.id === '<root>');
572572
const alpha = _.find(nodes, node => node.id === 'alpha');
573573
const bravo = _.find(nodes, node => node.id === 'bravo');
574+
const juliet = _.find(nodes, node => node.id === 'juliet');
575+
576+
// contains
577+
t.equal(root.contains(null), false);
578+
t.equal(root.contains(undefined), false);
579+
t.equal(root.contains({}), false);
580+
t.equal(root.contains([]), false);
581+
t.equal(root.contains(root), false);
582+
t.equal(root.contains(alpha), true);
583+
t.equal(root.contains(bravo), true);
584+
t.equal(root.contains(juliet), true);
585+
t.equal(alpha.contains(bravo), false);
586+
t.equal(alpha.contains(juliet), false);
587+
t.equal(bravo.contains(alpha), false);
588+
t.equal(bravo.contains(juliet), true);
589+
t.equal(juliet.contains(root), false);
574590

575591
// hasChildren
576592
t.same(root.hasChildren(), true);

0 commit comments

Comments
 (0)