Skip to content
Browse files

jslinted + small code cleanup

  • Loading branch information...
1 parent cfd673c commit 0cb7cf283c0d9b4c823c574ff9679190f5ef2ff7 @luccastera luccastera committed Oct 25, 2012
Showing with 56 additions and 50 deletions.
  1. +19 −17 lib/nested_set.js
  2. +10 −8 tests/helpers.js
  3. +27 −25 tests/nested_set_test.js
View
36 lib/nested_set.js
@@ -1,18 +1,20 @@
-'use strict'
+/*globals require, console, module */
+
+'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
async = require('async');
var NestedSetPlugin = function(schema, options) {
- options = options || {}
+ options = options || {};
var modelName = options.modelName || 'User';
schema.add({ lft: {type: Number, min: 0} });
schema.add({ rgt: {type: Number, min: 0} });
schema.add({ parentId: {type: Schema.ObjectId} });
-
+
schema.pre('save', function(next) {
var self = this;
var model = mongoose.model(modelName);
@@ -134,51 +136,51 @@ var NestedSetPlugin = function(schema, options) {
schema.method('isLeaf', function() {
return this.lft && this.rgt && (this.rgt - this.lft === 1);
});
-
+
// Returns true if the node is a child node (i.e. has a parent)
schema.method('isChild', function() {
return !!this.parentId;
});
-
+
// Returns true if other is a descendant of self
schema.method('isDescendantOf', function(other) {
var self = this;
return other.lft < self.lft && self.lft < other.rgt;
});
-
+
// Returns true if other is an ancestor of self
schema.method('isAncestorOf', function(other) {
var self = this;
return self.lft < other.lft && other.lft < self.rgt;
});
-
+
// returns the parent node
schema.method('parent', function(callback) {
var self = this;
var model = mongoose.model(modelName);
model.findOne({_id: self.parentId}, callback);
});
-
+
// Returns the list of ancestors + current node
schema.method('selfAndAncestors', function(callback) {
var self = this;
var model = mongoose.model(modelName);
model.where('lft').lte(self.lft).where('rgt').gte(self.rgt).run(callback);
});
-
+
// Returns the list of ancestors
schema.method('ancestors', function(callback) {
var self = this;
self.selfAndAncestors(function(err, nodes) {
if (err) {
callback(err, null);
} else {
- var nodesMinusSelf = nodes.filter(function(node) { return self.username != node.username });
+ var nodesMinusSelf = nodes.filter(function(node) { return self.username != node.username; });
callback(null, nodesMinusSelf);
}
});
});
-
+
// Returns the list of children
schema.method('children', function(callback) {
var self = this;
@@ -204,35 +206,35 @@ var NestedSetPlugin = function(schema, options) {
var model = mongoose.model(modelName);
model.where('lft').gte(self.lft).where('rgt').lte(self.rgt).run(callback);
});
-
+
// Returns the list of descendants
schema.method('descendants', function(callback) {
var self = this;
self.selfAndDescendants(function(err, nodes) {
if (err) {
callback(err, null);
} else {
- var nodesMinusSelf = nodes.filter(function(node) { return self.username != node.username });
+ var nodesMinusSelf = nodes.filter(function(node) { return self.username != node.username; });
callback(null, nodesMinusSelf);
}
});
});
-
+
// Returns the list of all nodes with the same parent + current node
schema.method('selfAndSiblings', function(callback) {
var self = this;
var model = mongoose.model(modelName);
model.find({parentId: self.parentId}, callback);
});
-
+
// Returns the list of all nodes with the same parent
schema.method('siblings', function(callback) {
var self = this;
self.selfAndSiblings(function(err, nodes) {
if (err) {
callback(err, null);
} else {
- var nodesMinusSelf = nodes.filter(function(node) { return self.username != node.username });
+ var nodesMinusSelf = nodes.filter(function(node) { return self.username != node.username; });
callback(null, nodesMinusSelf);
}
});
@@ -245,6 +247,6 @@ var NestedSetPlugin = function(schema, options) {
callback(err, nodes.length);
});
});
-}
+};
module.exports = exports = NestedSetPlugin;
View
18 tests/helpers.js
@@ -1,19 +1,21 @@
-'use strict'
+/*globals require, exports, console */
-var ObjectId = require('mongoose').Types.ObjectId
+'use strict';
+
+var ObjectId = require('mongoose').Types.ObjectId;
exports.isObjectId = function(id) {
- if (!id) {return false}
+ if (!id) {return false; }
try {
if ('string' === typeof id) {
- new ObjectId(id)
+ new ObjectId(id);
} else {
- new ObjectId(id.toString())
+ new ObjectId(id.toString());
}
} catch (err) {
console.log(err);
- return false
+ return false;
}
- return true
-}
+ return true;
+};
View
52 tests/nested_set_test.js
@@ -1,4 +1,6 @@
-'use strict'
+/*globals require, module, console, setTimeout */
+
+'use strict';
var testCase = require('nodeunit').testCase,
mongoose = require('mongoose'),
@@ -8,7 +10,7 @@ var testCase = require('nodeunit').testCase,
Helpers = require('./helpers');
var UserSchema,
- User
+ User;
var tests = testCase({
setUp: function(next) {
@@ -57,14 +59,14 @@ var tests = testCase({
stanley,
dwight,
oscar
- ], function(item, cb) { item.save(cb) }, function(err) {
+ ], function(item, cb) { item.save(cb); }, function(err) {
setTimeout(callback, 500);
- })
+ });
},
function(callback) {
next();
}
- ])
+ ]);
},
tearDown: function(callback) {
mongoose.disconnect(callback);
@@ -143,7 +145,7 @@ var tests = testCase({
});
},
'isLeaf should return true if node is leaf': function(test) {
- test.expect(2)
+ test.expect(2);
User.findOne({username: 'michael'}, function(err, user) {
User.rebuildTree(user, 1, function() {
User.findOne({username: 'kelly'}, function(err, kelly) {
@@ -157,7 +159,7 @@ var tests = testCase({
});
},
'isChild should return true if node has a parent': function(test) {
- test.expect(2)
+ test.expect(2);
User.findOne({username: 'michael'}, function(err, user) {
User.rebuildTree(user, 1, function() {
User.findOne({username: 'kelly'}, function(err, kelly) {
@@ -175,12 +177,12 @@ var tests = testCase({
User.findOne({username: 'michael'}, function(err, user) {
User.rebuildTree(user, 1, function() {
User.findOne({username: 'kelly'}, function(err, kelly) {
- test.ok(!err)
- test.ok(kelly)
+ test.ok(!err);
+ test.ok(kelly);
kelly.parent(function(err, node) {
test.ok(!err);
- test.equal('meredith',node.username)
- test.done()
+ test.equal('meredith',node.username);
+ test.done();
});
});
});
@@ -193,7 +195,7 @@ var tests = testCase({
User.findOne({username: 'kelly'}, function(err, kelly) {
kelly.selfAndAncestors(function(err, people) {
test.ok(!err);
- test.deepEqual(['kelly', 'meredith', 'michael'], people.map(function(p) {return p.username}).sort());
+ test.deepEqual(['kelly', 'meredith', 'michael'], people.map(function(p) {return p.username; }).sort());
test.done();
});
});
@@ -207,7 +209,7 @@ var tests = testCase({
User.findOne({username: 'kelly'}, function(err, kelly) {
kelly.ancestors(function(err, people) {
test.ok(!err);
- test.deepEqual(['meredith', 'michael'], people.map(function(p) {return p.username}).sort());
+ test.deepEqual(['meredith', 'michael'], people.map(function(p) {return p.username; }).sort());
test.done();
});
});
@@ -221,7 +223,7 @@ var tests = testCase({
User.findOne({username: 'michael'}, function(err, michael) {
michael.ancestors(function(err, people) {
test.ok(!err);
- test.deepEqual([], people.map(function(p) {return p.username}).sort());
+ test.deepEqual([], people.map(function(p) {return p.username; }).sort());
test.done();
});
});
@@ -237,7 +239,7 @@ var tests = testCase({
test.ok(!err);
test.deepEqual(
['angela', 'jim', 'meredith', 'michael'],
- people.map(function(p) {return p.username}).sort());
+ people.map(function(p) {return p.username; }).sort());
test.done();
});
});
@@ -251,7 +253,7 @@ var tests = testCase({
User.findOne({username: 'michael'}, function(err, michael) {
michael.children(function(err, people) {
test.ok(!err);
- test.deepEqual(['angela', 'jim', 'meredith'], people.map(function(p) {return p.username}).sort());
+ test.deepEqual(['angela', 'jim', 'meredith'], people.map(function(p) {return p.username; }).sort());
test.done();
});
});
@@ -267,7 +269,7 @@ var tests = testCase({
test.ok(!err);
test.deepEqual(
['angela', 'creed', 'dwight', 'jim', 'kelly', 'meredith', 'michael', 'oscar', 'phyllis', 'stanley'],
- people.map(function(p) {return p.username}).sort()
+ people.map(function(p) {return p.username; }).sort()
);
test.done();
});
@@ -284,7 +286,7 @@ var tests = testCase({
test.ok(!err);
test.deepEqual(
['angela', 'creed', 'dwight', 'jim', 'kelly', 'meredith', 'oscar', 'phyllis', 'stanley'],
- people.map(function(p) {return p.username}).sort()
+ people.map(function(p) {return p.username; }).sort()
);
test.done();
});
@@ -293,7 +295,7 @@ var tests = testCase({
});
},
'level should return 0 for root node': function(test) {
- test.expect(2)
+ test.expect(2);
User.findOne({username: 'michael'}, function(err, user) {
User.rebuildTree(user, 1, function() {
User.findOne({username: 'michael'}, function(err, michael) {
@@ -315,7 +317,7 @@ var tests = testCase({
test.ok(!err);
test.deepEqual(
['angela', 'jim', 'meredith'],
- people.map(function(p) {return p.username}).sort()
+ people.map(function(p) {return p.username; }).sort()
);
test.done();
});
@@ -332,7 +334,7 @@ var tests = testCase({
test.ok(!err);
test.deepEqual(
['angela', 'jim'],
- people.map(function(p) {return p.username}).sort()
+ people.map(function(p) {return p.username; }).sort()
);
test.done();
});
@@ -368,7 +370,7 @@ var tests = testCase({
test.expect(4);
var user = new User({
username: 'joe'
- })
+ });
user.save(function(err, joe) {
test.ok(!err);
test.equal('joe', joe.username);
@@ -378,7 +380,7 @@ var tests = testCase({
});
},
'adding a new node to a built tree should re-arrange the tree correctly': function(test) {
- test.expect(18)
+ test.expect(18);
User.findOne({username: 'michael'}, function(err, michael) {
User.rebuildTree(michael, 1, function() {
User.findOne({username: 'creed'}, function(err, creed) {
@@ -424,14 +426,14 @@ var tests = testCase({
});
},
'removing a node to a built tree should re-arrange the tree correctly': function(test) {
- test.expect(22)
+ test.expect(22);
User.findOne({username: 'michael'}, function(err, michael) {
User.rebuildTree(michael, 1, function() {
User.findOne({username: 'creed'}, function(err, creed) {
var newUser = new User({
username: 'joe',
parentId: creed._id
- })
+ });
newUser.save(function(err, joe) {
User.find(function(err, users) {
// see docs/test_tree_after_leaf_insertion.png for the graphical representation of this tree

0 comments on commit 0cb7cf2

Please sign in to comment.
Something went wrong with that request. Please try again.