diff --git a/lib/model.js b/lib/model.js index fe3a8f8..74f92e6 100644 --- a/lib/model.js +++ b/lib/model.js @@ -237,15 +237,16 @@ function modify (obj, updateQuery) { function areThingsEqual (a, b) { var aKeys , bKeys , i; - // Strings, booleans, numbers, null, undefined - if (a === null || a === undefined || typeof a === 'string' || typeof a === 'boolean' || typeof a === 'number' || - b === null || b === undefined || typeof b === 'string' || typeof b === 'boolean' || typeof b === 'number') { return a === b; } + // Strings, booleans, numbers, null + if (a === null || typeof a === 'string' || typeof a === 'boolean' || typeof a === 'number' || + b === null || typeof b === 'string' || typeof b === 'boolean' || typeof b === 'number') { return a === b; } // Dates if (util.isDate(a) || util.isDate(b)) { return util.isDate(a) && util.isDate(b) && a.getTime() === b.getTime(); } // Arrays (no match since arrays are used as a $in) - if (util.isArray(a) || util.isArray(b)) { return false; } + // undefined (no match since they mean field doesn't exist and can't be serialized) + if (util.isArray(a) || util.isArray(b) || a === undefined || b === undefined) { return false; } // a and b should be objects try { @@ -270,17 +271,17 @@ function areThingsEqual (a, b) { * @param {Object} obj * @param {String} field */ -//function getDotValue (obj, field) { - //var fieldParts = typeof field === 'string' ? field.split('.') : field; +function getDotValue (obj, field) { + var fieldParts = typeof field === 'string' ? field.split('.') : field; - //if (!obj) { return false; } // field cannot be empty here so that means there is no match + if (!obj) { return undefined; } // field cannot be empty so that means we should return undefined so that nothing can match - //if (fieldParts.length === 1) { - //return obj[fieldParts[0]]; - //} else { - //return matcherFunctions.$eq(obj[fieldParts[0]], fieldParts.slice(1)); - //} -//} + if (fieldParts.length === 1) { + return obj[fieldParts[0]]; + } else { + return matcherFunctions.$eq(obj[fieldParts[0]], fieldParts.slice(1)); + } +} /** diff --git a/test/model.test.js b/test/model.test.js index 2a8c304..49432cd 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -325,13 +325,13 @@ describe('Model', function () { }); // ==== End of 'Modifying documents' ==== // - describe.only('Finding documents', function () { + describe('Finding documents', function () { describe('Comparing things', function () { it('Two things of different types cannot be equal, two identical native things are equal', function () { - var toTest = [null, undefined, 'somestring', 42, true, new Date(72998322), { hello: 'world' }] - , toTestAgainst = [null, undefined, 'somestring', 42, true, new Date(72998322), { hello: 'world' }] // Use another array so that we don't test pointer equality + var toTest = [null, 'somestring', 42, true, new Date(72998322), { hello: 'world' }] + , toTestAgainst = [null, 'somestring', 42, true, new Date(72998322), { hello: 'world' }] // Use another array so that we don't test pointer equality , i, j ; @@ -353,7 +353,7 @@ describe('Model', function () { } }); - it('If one side is an array, comparison fails', function () { + it('If one side is an array or undefined, comparison fails', function () { var toTestAgainst = [null, undefined, 'somestring', 42, true, new Date(72998322), { hello: 'world' }] , i ; @@ -361,6 +361,9 @@ describe('Model', function () { for (i = 0; i < toTestAgainst.length; i += 1) { model.areThingsEqual([1, 2, 3], toTestAgainst[i]).should.equal(false); model.areThingsEqual(toTestAgainst[i], []).should.equal(false); + + model.areThingsEqual(undefined, toTestAgainst[i]).should.equal(false); + model.areThingsEqual(toTestAgainst[i], undefined).should.equal(false); } }); @@ -388,9 +391,9 @@ describe('Model', function () { model.match({ test: { ooo: 'yeah' } }, { "test.ooo": 'yeah' }).should.equal(true); }); - it('Can find undefined in first level or dot notation', function () { - model.match({ test: undefined }, { test: undefined }).should.equal(true); - model.match({ test: { pp: undefined } }, { "test.pp": undefined }).should.equal(true); + it('Cannot find undefined', function () { + model.match({ test: undefined }, { test: undefined }).should.equal(false); + model.match({ test: { pp: undefined } }, { "test.pp": undefined }).should.equal(false); }); });