cannot find undefined

pull/2/head
Louis Chatriot 12 years ago
parent 085eb2a0ae
commit ea0a25dee9
  1. 27
      lib/model.js
  2. 17
      test/model.test.js

@ -237,15 +237,16 @@ function modify (obj, updateQuery) {
function areThingsEqual (a, b) { function areThingsEqual (a, b) {
var aKeys , bKeys , i; var aKeys , bKeys , i;
// Strings, booleans, numbers, null, undefined // Strings, booleans, numbers, null
if (a === null || a === undefined || typeof a === 'string' || typeof a === 'boolean' || typeof a === 'number' || if (a === null || 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; } b === null || typeof b === 'string' || typeof b === 'boolean' || typeof b === 'number') { return a === b; }
// Dates // Dates
if (util.isDate(a) || util.isDate(b)) { return util.isDate(a) && util.isDate(b) && a.getTime() === b.getTime(); } 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) // 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 // a and b should be objects
try { try {
@ -270,17 +271,17 @@ function areThingsEqual (a, b) {
* @param {Object} obj * @param {Object} obj
* @param {String} field * @param {String} field
*/ */
//function getDotValue (obj, field) { function getDotValue (obj, field) {
//var fieldParts = typeof field === 'string' ? field.split('.') : 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) { if (fieldParts.length === 1) {
//return obj[fieldParts[0]]; return obj[fieldParts[0]];
//} else { } else {
//return matcherFunctions.$eq(obj[fieldParts[0]], fieldParts.slice(1)); return matcherFunctions.$eq(obj[fieldParts[0]], fieldParts.slice(1));
//} }
//} }
/** /**

@ -325,13 +325,13 @@ describe('Model', function () {
}); // ==== End of 'Modifying documents' ==== // }); // ==== End of 'Modifying documents' ==== //
describe.only('Finding documents', function () { describe('Finding documents', function () {
describe('Comparing things', function () { describe('Comparing things', function () {
it('Two things of different types cannot be equal, two identical native things are equal', 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' }] var toTest = [null, '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 , toTestAgainst = [null, 'somestring', 42, true, new Date(72998322), { hello: 'world' }] // Use another array so that we don't test pointer equality
, i, j , 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' }] var toTestAgainst = [null, undefined, 'somestring', 42, true, new Date(72998322), { hello: 'world' }]
, i , i
; ;
@ -361,6 +361,9 @@ describe('Model', function () {
for (i = 0; i < toTestAgainst.length; i += 1) { for (i = 0; i < toTestAgainst.length; i += 1) {
model.areThingsEqual([1, 2, 3], toTestAgainst[i]).should.equal(false); model.areThingsEqual([1, 2, 3], toTestAgainst[i]).should.equal(false);
model.areThingsEqual(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); model.match({ test: { ooo: 'yeah' } }, { "test.ooo": 'yeah' }).should.equal(true);
}); });
it('Can find undefined in first level or dot notation', function () { it('Cannot find undefined', function () {
model.match({ test: undefined }, { test: undefined }).should.equal(true); model.match({ test: undefined }, { test: undefined }).should.equal(false);
model.match({ test: { pp: undefined } }, { "test.pp": undefined }).should.equal(true); model.match({ test: { pp: undefined } }, { "test.pp": undefined }).should.equal(false);
}); });
}); });

Loading…
Cancel
Save