-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.js
70 lines (60 loc) · 1.28 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var NestedSetModel = require('./nested-set-model');
// An example input set
/*
1 root 12
/ \
2 parent 9 10 middle 11
/
3 child 8
/
4 child 7
/
5 leaf 6
*/
var set = [
{
id: 1,
title: 'root',
left: 1,
right: 12
},
{
id: 4,
title: 'child',
left: 3,
right: 8
},
{
id: 2,
title: 'parent',
left: 2,
right: 9,
},
{
id: 3,
title: 'middle',
left: 10,
right: 11
},
{
id:5,
title: 'child',
left: 4,
right: 7
},
{
id:6,
title: 'leaf',
left: 5,
right: 6
}
];
var model = new NestedSetModel(set);
if (model.find({ title: 'child', left: 3}).isLeaf() === false) {
console.info('SUCCESS: {title: "child", left: 3} is not a leaf node');
} else {
console.error('FAIL: {title: "child", left: 3} is a leaf node, but shouldn\'t be');
}
console.log('It\'s parents:', model.find({title: 'child', left: 3}).parents());
console.log('It\'s children:', model.find({title: 'child', left: 3}).children());
console.log('It\'s descendants:', model.find({title: 'child', left: 3}).descendants());