From bd94569d863bf2832807187ea8fa0d45ae289e95 Mon Sep 17 00:00:00 2001 From: lvbreda Date: Thu, 1 Nov 2012 11:20:56 +0100 Subject: [PATCH 1/4] Added sorting functionality for 'a.b' --- packages/minimongo/sort.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/minimongo/sort.js b/packages/minimongo/sort.js index 6d1d8077fe..9cd73e3327 100644 --- a/packages/minimongo/sort.js +++ b/packages/minimongo/sort.js @@ -44,14 +44,18 @@ LocalCollection._compileSort = function (spec) { var _func; var code = "_func = (function(c){return function(a,b){var x;"; for (var i = 0; i < keys.length; i++) { - if (i !== 0) - code += "if(x!==0)return x;"; - code += "x=" + (asc[i] ? "" : "-") + - "c(a[" + JSON.stringify(keys[i]) + "],b[" + - JSON.stringify(keys[i]) + "]);"; + var splittedKeys = keys[i].split("."); + var keyString = ""; + for(o = 0;o Date: Wed, 12 Dec 2012 11:59:21 -0800 Subject: [PATCH 2/4] Test for sub-key sort. Doesn't pass yet. --- packages/minimongo/minimongo_tests.js | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/minimongo/minimongo_tests.js b/packages/minimongo/minimongo_tests.js index b3713eb25c..16331319ae 100644 --- a/packages/minimongo/minimongo_tests.js +++ b/packages/minimongo/minimongo_tests.js @@ -875,6 +875,40 @@ Tinytest.add("minimongo - sort", function (test) { {a: 47, b: 1, _id: "47_1"}]); }); +Tinytest.add("minimongo - subkey sort", function (test) { + var c = new LocalCollection(); + + // normal case + c.insert({a: {b: 1}}); + c.insert({a: {b: 2}}); + c.insert({a: {b: 3}}); + test.equal( + _.pluck(c.find({}, {sort: {'a.b': -1}}).fetch(), 'a'), + [{b: 3}, {b: 2}, {b: 1}]); + + // isn't an object + c.insert({a: 1}); + test.equal( + _.pluck(c.find({}, {sort: {'a.b': -1}}).fetch(), 'a'), + [{b: 3}, {b: 2}, {b: 1}, 1]); + + // complex object + c.insert({a: {b: {c: 1}}}); + test.equal( + _.pluck(c.find({}, {sort: {'a.b': -1}}).fetch(), 'a'), + [{b: {c: 1}}, {b: 3}, {b: 2}, {b: 1}, 1]); + + // no such top level prop + c.insert({c: 1}); + test.equal( + _.pluck(c.find({}, {sort: {'a.b': -1}}).fetch(), 'a'), + [{b: {c: 1}}, {b: 3}, {b: 2}, {b: 1}, 1, undefined]); + + // no such mid level prop. just test that it doesn't throw. + c.find({}, {sort: {'a.nope.c': -1}}).fetch(); +}); + + Tinytest.add("minimongo - modify", function (test) { var modify = function (doc, mod, result) { var copy = LocalCollection._deepcopy(doc); From 4c78d3dc5116d4fbf1f146019ef425e6f2f1a50a Mon Sep 17 00:00:00 2001 From: Nick Martin Date: Wed, 12 Dec 2012 12:00:38 -0800 Subject: [PATCH 3/4] Make sub-key sort check for undefined before dereferencing. This way it doesn't throw when given an object that doesn't contain all the keys being searched on. --- packages/minimongo/sort.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/minimongo/sort.js b/packages/minimongo/sort.js index 9cd73e3327..53114153dc 100644 --- a/packages/minimongo/sort.js +++ b/packages/minimongo/sort.js @@ -11,8 +11,6 @@ // first object comes first in order, 1 if the second object comes // first, or 0 if neither object comes before the other. -// XXX sort does not yet support subkeys ('a.b') .. fix that! - LocalCollection._compileSort = function (spec) { var keys = []; var asc = []; @@ -44,16 +42,22 @@ LocalCollection._compileSort = function (spec) { var _func; var code = "_func = (function(c){return function(a,b){var x;"; for (var i = 0; i < keys.length; i++) { - var splittedKeys = keys[i].split("."); - var keyString = ""; - for(o = 0;o Date: Fri, 14 Dec 2012 20:05:22 -0800 Subject: [PATCH 4/4] Feedback from review. --- packages/minimongo/minimongo_tests.js | 8 ++++---- packages/minimongo/sort.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/minimongo/minimongo_tests.js b/packages/minimongo/minimongo_tests.js index 16331319ae..ec3cf13f4a 100644 --- a/packages/minimongo/minimongo_tests.js +++ b/packages/minimongo/minimongo_tests.js @@ -879,8 +879,8 @@ Tinytest.add("minimongo - subkey sort", function (test) { var c = new LocalCollection(); // normal case - c.insert({a: {b: 1}}); c.insert({a: {b: 2}}); + c.insert({a: {b: 1}}); c.insert({a: {b: 3}}); test.equal( _.pluck(c.find({}, {sort: {'a.b': -1}}).fetch(), 'a'), @@ -889,8 +889,8 @@ Tinytest.add("minimongo - subkey sort", function (test) { // isn't an object c.insert({a: 1}); test.equal( - _.pluck(c.find({}, {sort: {'a.b': -1}}).fetch(), 'a'), - [{b: 3}, {b: 2}, {b: 1}, 1]); + _.pluck(c.find({}, {sort: {'a.b': 1}}).fetch(), 'a'), + [1, {b: 1}, {b: 2}, {b: 3}]); // complex object c.insert({a: {b: {c: 1}}}); @@ -905,7 +905,7 @@ Tinytest.add("minimongo - subkey sort", function (test) { [{b: {c: 1}}, {b: 3}, {b: 2}, {b: 1}, 1, undefined]); // no such mid level prop. just test that it doesn't throw. - c.find({}, {sort: {'a.nope.c': -1}}).fetch(); + test.equal(c.find({}, {sort: {'a.nope.c': -1}}).count(), 6); }); diff --git a/packages/minimongo/sort.js b/packages/minimongo/sort.js index 53114153dc..94420cf237 100644 --- a/packages/minimongo/sort.js +++ b/packages/minimongo/sort.js @@ -53,7 +53,7 @@ LocalCollection._compileSort = function (spec) { aCode += '&&a' + keyString; bCode += '&&b' + keyString; } - if (i !== 0){ + if (i !== 0) { code += "if(x!==0)return x;"; } code += "x=" + (asc[i] ? "" : "-") +