Skip to content

Commit

Permalink
Adding check for valid numbers when object is supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
rmomii committed Dec 19, 2016
1 parent 01d6c7b commit b5a7995
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/order.js
Expand Up @@ -18,9 +18,14 @@ module.exports = function(mongoose) {
fields = sort;
} else if (typeof sort === 'object') {
for (var property in sort) {
if (sort[property] < 0) {
fields.push('-' + property);
if (!isNaN(parseInt(sort[property]))) {
if (sort[property] < 0) {
fields.push('-' + property);
} else {
fields.push(property);
}
} else {
//property supplied is NaN; default to 1/ascending
fields.push(property);
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/lib/order.js
Expand Up @@ -179,4 +179,21 @@ describe('order', function () {
sortClauseItems[1].name.should.equals(-1);
});

it ('should sort fields in default ascending order when values in supplied object are not valid numbers', function () {
var options = {
sort : {
'home': 1,
'name': 'invalid number'
}
};

Kitteh
.find()
.order(options);

sortClauseItems.should.have.length(2);
sortClauseItems[0].home.should.equals(1);
sortClauseItems[1].name.should.equals(1);
});

});

0 comments on commit b5a7995

Please sign in to comment.