Skip to content

Commit

Permalink
Using member constructors for easier composition
Browse files Browse the repository at this point in the history
  • Loading branch information
drkibitz committed Jan 28, 2014
1 parent 29beb7a commit 5bce6ae
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Expand Up @@ -34,7 +34,7 @@ gulp.task('uglify', function () {
]);
cb(null, file);
}))
.pipe(uglify({wrap: 'nodes'}))
.pipe(uglify())
.pipe(map(function (file, cb) {
file.contents = Buffer.concat([new Buffer(banner), file.contents]);
cb(null, file);
Expand All @@ -48,7 +48,7 @@ function testTask(requireModule) {
.pipe(mocha({
reporter: 'spec',
globals: {
nodes: require(requireModule)
'nodes': require(requireModule)
}
}));
};
Expand Down
4 changes: 2 additions & 2 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jsdoc.conf.json
Expand Up @@ -11,7 +11,7 @@
},
"systemName" : "Qi Nodes",
"footer" : "",
"copyright" : "Copyright © 2013 Dr. Kibitz",
"copyright" : "Copyright © 2013-2014 Dr. Kibitz",
"navType" : "vertical",
"theme" : "spacelab",
"linenums" : false,
Expand Down
26 changes: 13 additions & 13 deletions src/index.js
Expand Up @@ -15,7 +15,7 @@
* @memberof module:qi-nodes
*/
function HierarchyRequestError(message) {
this.message = (message || 'A NodeObject was inserted somewhere it doesn\'t belong.');
this.message = (message || 'A node was inserted somewhere it doesn\'t belong.');
}
HierarchyRequestError.prototype = new Error();
HierarchyRequestError.prototype.name = 'HierarchyRequestError';
Expand Down Expand Up @@ -69,12 +69,10 @@ function setRoot(node) {
* When a leaf is removed from a rooted tree, the previous root
* reference is removed for all objects contained in that leaf.
* @constructor
* @param {Object=} parent Optional parent object to append this object to
* @memberof module:qi-nodes
*/
function NodeObject(parent) {
function NodeObject() {
cleanNode(this);
if (parent) this.appendTo(parent);
}

/**
Expand Down Expand Up @@ -156,7 +154,7 @@ proto.appendTo = function appendTo(parent, node) {
node = node || this;

if (node == parent) {
throw new HierarchyRequestError();
throw new this.HierarchyRequestError();

// may be a noop
} else if (node != lastChild) {
Expand Down Expand Up @@ -184,7 +182,9 @@ proto.appendTo = function appendTo(parent, node) {
* @return {module:qi-nodes.NodeObject} created object optionally added to parent
*/
proto.create = function create(parent) {
return new NodeObject(parent);
var node = new this.NodeObject();
if (parent) this.appendTo(parent, node);
return node;
};

/**
Expand All @@ -193,7 +193,7 @@ proto.create = function create(parent) {
* @return {module:qi-nodes.NodeObject} created rooted object
*/
proto.createRoot = function createRoot() {
var root = new NodeObject();
var root = new this.RootObject();
root.root = root;
return root;
};
Expand Down Expand Up @@ -322,7 +322,7 @@ proto.insertAfter = function insertAfter(sibling, node) {
if (node != sibling && node != nextSibling) {
parent = sibling.parent;
if (!parent || node == parent) {
throw new HierarchyRequestError();
throw new this.HierarchyRequestError();
} else if (node.parent) {
removeNode(node);
}
Expand Down Expand Up @@ -364,7 +364,7 @@ proto.insertBefore = function insertBefore(sibling, node) {
if (node != sibling && node != previousSibling) {
parent = sibling.parent;
if (!parent || node == parent) {
throw new HierarchyRequestError();
throw new this.HierarchyRequestError();
} else if (node.parent) {
removeNode(node);
}
Expand Down Expand Up @@ -415,7 +415,7 @@ proto.prependTo = function prependTo(parent, node) {
node = node || this;

if (node == parent) {
throw new HierarchyRequestError();
throw new this.HierarchyRequestError();

// may be a noop
} else if (node != firstChild) {
Expand Down Expand Up @@ -510,6 +510,6 @@ proto.swap = function swap(node1, node2) {
return node1;
};

exports = module.exports = proto.createRoot();
exports.HierarchyRequestError = HierarchyRequestError;
exports.NodeObject = NodeObject;
proto.NodeObject = proto.RootObject = NodeObject;
proto.HierarchyRequestError = HierarchyRequestError;
module.exports = proto.createRoot();
2 changes: 1 addition & 1 deletion test/unit/HierarchyRequestError.test.js
Expand Up @@ -9,7 +9,7 @@ describe('HierarchyRequestError', function () {
var err = new HierarchyRequestError();
assert.strictEqual(err.name, 'HierarchyRequestError');
assert.deepEqual(err, {
message: 'A NodeObject was inserted somewhere it doesn\'t belong.'
message: 'A node was inserted somewhere it doesn\'t belong.'
});
});

Expand Down

0 comments on commit 5bce6ae

Please sign in to comment.