diff --git a/README.md b/README.md index 322da3f..2403708 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/example.js b/example.js new file mode 100644 index 0000000..ac7749a --- /dev/null +++ b/example.js @@ -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()); + + diff --git a/nested-set-model.js b/nested-set-model.js index f51519a..d183f54 100644 --- a/nested-set-model.js +++ b/nested-set-model.js @@ -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;