Skip to content

Commit

Permalink
Seperate example to seprate file and bootstrap library
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdi Schmidt committed Aug 12, 2014
1 parent cb2bf60 commit c89f276
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 66 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ database system, it's troublesome to implement procedurally.
This library helps bridging the gap between back-end data structures
and front-end implementations.

Example
----
Please refer to [example.js](example.js) for an example on usage.

API
----
The library follows a DSL-like usage structure, whereby method chaining
Expand All @@ -25,7 +29,7 @@ Please see [API.md](API.md) for more information.

Requests
----
I Highly encourage anybody using this library to suggest features
I highly encourage anybody using this library to suggest features
that will find useful, or features that are simply lacking.

Please either create a issue, or feel free to submit a pull request.
Expand Down
69 changes: 69 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 childrne:', model.find({title: 'child', left: 3}).children());


70 changes: 5 additions & 65 deletions nested-set-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,69 +123,9 @@ NestedSetModelNode.prototype.isChild = function() {
return this.left > 0 && this.right < (this.model.length * 2);
}

// 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 childrne:', model.find({title: 'child', left: 3}).children());
// bootstrap
if (typeof define !== 'undefined') {
define('NestedSetModel', NestedSetModel);
}

module.exports = NestedSetModel;

0 comments on commit c89f276

Please sign in to comment.