Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix .sort() limit stop

Add tests for .sort() limit
  • Loading branch information...
commit 388bff0302249ae435af0a1312f03974e642cce6 1 parent ab30d4c
@maritz authored
Showing with 76 additions and 3 deletions.
  1. +9 −2 lib/retrieve.js
  2. +67 −1 test/findTests.js
View
11 lib/retrieve.js
@@ -191,7 +191,14 @@ exports.sort = function (options, ids) {
var stop = 100;
if (Array.isArray(options.limit) && options.limit.length > 0) {
start = options.limit[0];
- stop = options.limit[1] || (scored ? options.limit[0]+stop : stop); // the limit arguments for sets and sorted sets work differently
+ if (scored) { // the limit arguments for sets and sorted sets work differently
+ // stop is a 0-based index from the start of all zset members
+ stop = options.limit[1] ? start+options.limit[1] : start+stop;
+ stop--;
+ } else {
+ // stop is a 1-based index from the defined start limit (the wanted behaviour)
+ stop = options.limit[1] || stop;
+ }
}
@@ -235,7 +242,7 @@ var sortScored = function (field, direction, start, stop, callback) {
if (start < 0 || stop < 0) {
Nohm.logError('Notice: tried to limit a scored sort with a negative start('+start+') or stop('+stop+').');
}
- if (stop < start) {
+ if (stop < start) {
Nohm.logError('Notice: tried to limit a scored sort with a higher start('+start+') than stop('+stop+').');
}
this.getClient()[method](
View
68 test/findTests.js
@@ -355,7 +355,6 @@ loadArray: function (t) {
if (err) {
console.dir(err);
}
- console.log(ids.sort());
t.same(ids.sort(), [users[0].id, users[1].id].sort(), 'The found id did not match the id of the saved object.');
t.done();
});
@@ -655,6 +654,50 @@ loadArray: function (t) {
t.done();
});
},
+
+ "all by name DESC": function (t) {
+ t.expect(2);
+
+ var sorted_ids = this.users.sort(function (a, b) {
+ a = a.p('name');
+ b = b.p('name');
+ return a < b ? 1 : (a > b ? -1 : 0);
+ }).map(function (user) {
+ return ''+user.id;
+ });
+
+ UserFindMockup.sort({
+ field: 'name',
+ direction: 'DESC'
+ }, function (err, ids) {
+ t.same(null, err, 'Sorting caused an error: '+err);
+ t.same(sorted_ids, ids, 'Sorting went wrong.');
+ t.done();
+ });
+ },
+
+ "all by name LIMIT 2, 3": function (t) {
+ t.expect(2);
+
+ var sorted_ids = this.users.sort(function (a, b) {
+ a = a.p('name');
+ b = b.p('name');
+ return a > b ? 1 : (a < b ? -1 : 0);
+ }).slice(2, 5)
+ .map(function (user) {
+ return ''+user.id;
+ });
+
+ UserFindMockup.sort({
+ field: 'name',
+ limit: [2,3]
+ }, function (err, ids) {
+ t.same(null, err, 'Sorting caused an error: '+err);
+ t.same(sorted_ids, ids, 'Sorting went wrong.');
+ t.done();
+ });
+ },
+
"all by number": function (t) {
t.expect(2);
@@ -674,6 +717,7 @@ loadArray: function (t) {
t.done();
});
},
+
"all by number DESC": function (t) {
t.expect(2);
@@ -694,6 +738,28 @@ loadArray: function (t) {
t.same(sorted_ids, ids, 'Sorting went wrong.');
t.done();
});
+ },
+
+ "all by number LIMIT 3, 3": function (t) {
+ t.expect(2);
+
+ var sorted_ids = this.users.sort(function (a, b) {
+ a = a.p('number');
+ b = b.p('number');
+ return a > b ? 1 : (a < b ? -1 : 0);
+ }).slice(3, 6)
+ .map(function (user) {
+ return ''+user.id;
+ });
+
+ UserFindMockup.sort({
+ field: 'number',
+ limit: [3,3]
+ }, function (err, ids) {
+ t.same(null, err, 'Sorting caused an error: '+err);
+ t.same(sorted_ids, ids, 'Sorting went wrong.');
+ t.done();
+ });
}
/*
"provided and default": function (t) {
Please sign in to comment.
Something went wrong with that request. Please try again.