diff --git a/lib/arboreal.js b/lib/arboreal.js index 0135c0c..6003b64 100644 --- a/lib/arboreal.js +++ b/lib/arboreal.js @@ -1,4 +1,4 @@ -!function () { +var Arboreal = function () { 'use strict'; function include (array, item) { @@ -7,12 +7,12 @@ function _traverseDown (context, iterator) { var doContinue = true; - + (function walkDown (node) { var i, newContext; - + if (!doContinue) return; - + if (iterator(node) === false) { //break the traversal loop if the iterator returns a falsy value doContinue = false; @@ -44,33 +44,33 @@ function _traverse (context, iterator, callback) { var visited = [], - callIterator = function (node) { - var id = node.id, - returned; - - if (! include(visited, id)) { - returned = iterator.call(node, node); - visited.push(id); - - if (returned === false) { - return returned; - } - } - }, - i, node; - + callIterator = function (node) { + var id = node.id, + returned; + + if (! include(visited, id)) { + returned = iterator.call(node, node); + visited.push(id); + + if (returned === false) { + return returned; + } + } + }, + i, node; + callback(context, callIterator); } function _removeChild (node) { var parent = node.parent, - child, - i; - + child, + i; + for (i = 0; i < parent.children.length; i++) { child = parent.children[i]; - + if (child === node) { return parent.children.splice(i, 1).shift(); } @@ -98,16 +98,16 @@ Arboreal.parse = function (object, childrenAttr) { var root, getNodeData = function (node) { - var attr, nodeData = {}; - for (attr in node) { - if (attr !== childrenAttr) nodeData[attr] = node[attr]; - } - return nodeData; - }; - + var attr, nodeData = {}; + for (attr in node) { + if (attr !== childrenAttr) nodeData[attr] = node[attr]; + } + return nodeData; + }; + (function walkDown(node, parent) { var newNode, i; - + if (!parent) { newNode = root = new Arboreal(null, getNodeData(node)); } else { @@ -120,9 +120,9 @@ } } })(object); - + return root; - + }; Arboreal.prototype.appendChild = function (data, id) { @@ -148,11 +148,11 @@ Arboreal.prototype.root = function () { var node = this; - + if (!node.parent) { return this; } - + while (node.parent) { node = node.parent; } @@ -173,10 +173,10 @@ Arboreal.prototype.toString = function () { var lines = []; - + this.traverseDown(function (node) { var separator = '|- ', indentation = '', i; - + if (node.depth === 0) { lines.push(node.id); return; @@ -191,21 +191,21 @@ Arboreal.prototype.find = function (finder) { var match = null, - iterator = (typeof finder === 'function') ? - finder : function (node) { - if (node.id === finder) { - match = node; - return false; - } - }; - + iterator = (typeof finder === 'function') ? + finder : function (node) { + if (node.id === finder) { + match = node; + return false; + } + }; + this.traverseDown(function (node) { if (iterator.call(this, node)) { match = node; return false; } }); - + return match; }; @@ -213,18 +213,18 @@ separator = separator || '/'; //allow path to begin with if (path[0] === separator) path = path.substring(1); - + var indexes = path.split(separator), - index = null, - context = this, - i; - + index = null, + context = this, + i; + for (i = 0; i < indexes.length; i++) { index = parseInt(indexes[i], 10); context = (context.children.length && context.children.length > index) ? - context.children[index] : null; + context.children[index] : null; } - + return context; }; @@ -244,7 +244,7 @@ if (typeof module !== 'undefined' && module.exports) { module.exports = Arboreal; } else { - this.Arboreal = Arboreal; + return Arboreal; } -}(this); +}();