Skip to content
This repository was archived by the owner on Apr 4, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 55 additions & 55 deletions lib/arboreal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
!function () {
var Arboreal = function () {
'use strict';

function include (array, item) {
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 {
Expand All @@ -120,9 +120,9 @@
}
}
})(object);

return root;

};

Arboreal.prototype.appendChild = function (data, id) {
Expand All @@ -148,11 +148,11 @@

Arboreal.prototype.root = function () {
var node = this;

if (!node.parent) {
return this;
}

while (node.parent) {
node = node.parent;
}
Expand All @@ -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;
Expand All @@ -191,40 +191,40 @@

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;
};

Arboreal.prototype.path = function (path, separator) {
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;
};

Expand All @@ -244,7 +244,7 @@
if (typeof module !== 'undefined' && module.exports) {
module.exports = Arboreal;
} else {
this.Arboreal = Arboreal;
return Arboreal;
}

}(this);
}();