From a6b8ca70af6ff8447dcfbdc6d7099a538e1686ec Mon Sep 17 00:00:00 2001 From: cvette Date: Mon, 22 Dec 2014 18:45:39 +0100 Subject: [PATCH 1/4] fix sorting with undefined in memory connector --- lib/connectors/memory.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index c43e5093..386b6c14 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -348,9 +348,14 @@ Memory.prototype.all = function all(model, filter, callback) { function sorting(a, b) { for (var i = 0, l = this.length; i < l; i++) { - if (a[this[i].key] > b[this[i].key]) { + + if(a[this[i].key] === undefined && b[this[i].key] === undefined) { + return 0; + } + + if (b[this[i].key] === undefined || a[this[i].key] > b[this[i].key]) { return 1 * this[i].reverse; - } else if (a[this[i].key] < b[this[i].key]) { + } else if (a[this[i].key] === undefined || a[this[i].key] < b[this[i].key]) { return -1 * this[i].reverse; } } From 31c7973763b1b026a1d03170a4baf6fb67f9571d Mon Sep 17 00:00:00 2001 From: cvette Date: Mon, 22 Dec 2014 18:54:56 +0100 Subject: [PATCH 2/4] code style --- lib/connectors/memory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 386b6c14..a8b40e84 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -349,7 +349,7 @@ Memory.prototype.all = function all(model, filter, callback) { function sorting(a, b) { for (var i = 0, l = this.length; i < l; i++) { - if(a[this[i].key] === undefined && b[this[i].key] === undefined) { + if (a[this[i].key] === undefined && b[this[i].key] === undefined) { return 0; } From e45407256e928ccde77fca3f1ff3b3742d6500cc Mon Sep 17 00:00:00 2001 From: Christian Vette Date: Mon, 22 Dec 2014 21:06:01 +0100 Subject: [PATCH 3/4] fix sorting of undefined values with multiple columns --- lib/connectors/memory.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index a8b40e84..4d98e05e 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -347,18 +347,19 @@ Memory.prototype.all = function all(model, filter, callback) { }); function sorting(a, b) { + var undefinedA, undefinedB; + for (var i = 0, l = this.length; i < l; i++) { + undefinedB = b[this[i].key] === undefined && a[this[i].key] !== undefined; + undefinedA = a[this[i].key] === undefined && b[this[i].key] !== undefined; - if (a[this[i].key] === undefined && b[this[i].key] === undefined) { - return 0; - } - - if (b[this[i].key] === undefined || a[this[i].key] > b[this[i].key]) { + if (undefinedB || a[this[i].key] > b[this[i].key]) { return 1 * this[i].reverse; - } else if (a[this[i].key] === undefined || a[this[i].key] < b[this[i].key]) { + } else if (undefinedA || a[this[i].key] < b[this[i].key]) { return -1 * this[i].reverse; } } + return 0; } }; From 99acd364d22847a35847d49a43f86221f06485de Mon Sep 17 00:00:00 2001 From: Christian Vette Date: Mon, 5 Jan 2015 21:56:17 +0100 Subject: [PATCH 4/4] added test for sorting undefined values --- test/memory.test.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/test/memory.test.js b/test/memory.test.js index f07ab015..f3f16fd0 100644 --- a/test/memory.test.js +++ b/test/memory.test.js @@ -183,7 +183,7 @@ describe('Memory connector', function () { }); }); - it('support order with multiple fields', function (done) { + it('should support order with multiple fields', function (done) { User.find({order: 'vip ASC, seq DESC'}, function (err, posts) { should.not.exist(err); posts[0].seq.should.be.eql(4); @@ -192,6 +192,17 @@ describe('Memory connector', function () { }); }); + it('should sort undefined values to the end when ordered DESC', function (done) { + User.find({order: 'vip ASC, order DESC'}, function (err, posts) { + console.log(posts); + should.not.exist(err); + + posts[4].seq.should.be.eql(1); + posts[5].seq.should.be.eql(0); + done(); + }); + }); + it('should throw if order has wrong direction', function (done) { User.find({order: 'seq ABC'}, function (err, posts) { should.exist(err); @@ -200,11 +211,11 @@ describe('Memory connector', function () { }); it('should support neq operator for number', function (done) { - User.find({where: {order: {neq: 6}}}, function (err, users) { + User.find({where: {seq: {neq: 4}}}, function (err, users) { should.not.exist(err); users.length.should.be.equal(5); for (var i = 0; i < users.length; i++) { - users[i].order.should.not.be.equal(6); + users[i].seq.should.not.be.equal(4); } done(); }); @@ -242,7 +253,6 @@ describe('Memory connector', function () { email: 'john@b3atl3s.co.uk', role: 'lead', birthday: new Date('1980-12-08'), - order: 2, vip: true }, {