From 79ae8c737a0157aec0e9601fd2ccb79208c1181d Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Tue, 28 May 2013 15:45:53 +0200 Subject: [PATCH] Can compare null and numbers --- lib/model.js | 30 ++++++++++++++++++++++++++++++ test/model.test.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/lib/model.js b/lib/model.js index d46df86..5c9e1b2 100644 --- a/lib/model.js +++ b/lib/model.js @@ -130,6 +130,35 @@ function deepCopy (obj) { } +/** + * Utility function for comparing things + * Assumes type checking was already done (a and b already have the same type) + */ +function compareNumbersAndStrings (a, b) { + if (a < b) { return -1; } + if (a > b) { return 1; } + return 0; +} + +/** + * Compare things + * Things are defined as any native types (string, number, boolean, null, date) and objects + * In the case of objects and arrays, we compare the serialized versions + * If two objects dont have the same type, the (arbitrary) type hierarchy is: null, number, strings, dates, boolean, objects, arrays + * Return -1 if a < b, 1 if a > b and 0 if a = b (note that equality here is NOT the same as defined in areThingsEqual!) + */ +function compareThings (a, b) { + // null is less than everything except null + if (a === null) { return b === null ? 0 : -1; } + if (b === null) { return a === null ? 0 : 1; } + + // Comparing numbers + if (typeof a === 'number') { return typeof b === 'number' ? compareNumbersAndStrings(a, b) : -1; } + if (typeof b === 'number') { return typeof a === 'number' ? compareNumbersAndStrings(a, b) : 1; } +} + + + // ============================================================== // Updating documents // ============================================================== @@ -479,3 +508,4 @@ module.exports.modify = modify; module.exports.getDotValue = getDotValue; module.exports.match = match; module.exports.areThingsEqual = areThingsEqual; +module.exports.compareThings = compareThings; diff --git a/test/model.test.js b/test/model.test.js index 4ee42c0..558a23d 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -329,6 +329,39 @@ describe('Model', function () { }); // ==== End of 'Modifying documents' ==== // + describe.only('Comparing things', function () { + + it('null is less than everything except null', function () { + var otherStuff = ["string", "", -1, 0, 5.3, 12, true, false, {}, { hello: 'world' }, [], ['quite', 5]]; + + model.compareThings(null, null).should.equal(0); + + otherStuff.forEach(function (stuff) { + model.compareThings(null, stuff).should.equal(-1); + model.compareThings(stuff, null).should.equal(1); + }); + }); + + it('After null, numbers are the smallest', function () { + var otherStuff = ["string", "", true, false, {}, { hello: 'world' }, [], ['quite', 5]] + , numbers = [-12, 0, 12, 5.7]; + + model.compareThings(-12, 0).should.equal(-1); + model.compareThings(0, -3).should.equal(1); + model.compareThings(5.7, 2).should.equal(1); + model.compareThings(5.7, 12.3).should.equal(-1); + + otherStuff.forEach(function (stuff) { + numbers.forEach(function (number) { + model.compareThings(number, stuff).should.equal(-1); + model.compareThings(stuff, number).should.equal(1); + }); + }); + }); + + }); // ==== End of 'Comparing things' ==== // + + describe('Querying', function () { describe('Comparing things', function () {