From 2b79daa5065312955516029d34566dad08882413 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Wed, 22 May 2013 18:11:49 +0200 Subject: [PATCH] Undefined fields are removed --- lib/model.js | 20 +++++++++++++++++++- test/db.test.js | 3 ++- test/model.test.js | 39 ++++++++++++++++++--------------------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/lib/model.js b/lib/model.js index 7601384..fe3a8f8 100644 --- a/lib/model.js +++ b/lib/model.js @@ -66,8 +66,8 @@ function serialize (obj) { res = JSON.stringify(obj, function (k, v) { checkKey(k, v); + if (typeof v === undefined) { return null; } if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || v === null) { return v; } - //if (v && v.constructor && v.constructor.name === 'Date') { console.log("==============="); return { $$date: v.toString() }; } return v; }); @@ -265,6 +265,24 @@ function areThingsEqual (a, b) { } +/** + * Get a value from object with dot notation + * @param {Object} obj + * @param {String} 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 (fieldParts.length === 1) { + //return obj[fieldParts[0]]; + //} else { + //return matcherFunctions.$eq(obj[fieldParts[0]], fieldParts.slice(1)); + //} +//} + + /** * Test for field equality * @param {Object} obj The model to check diff --git a/test/db.test.js b/test/db.test.js index bcd2712..7350e49 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -284,11 +284,12 @@ describe('Database', function () { }); }); - it.only('By default, matching on an array field is matching any of its components', function (done) { + it('By default, matching on an array field is matching any of its components', function (done) { d.insert({ fruits: ['orange', 'apple', 'pear'] }, function () { d.insert({ fruits: ['banana', 'coconut', 'pear'] }, function () { d.find({ fruits: 'pear' }, function (err, docs) { console.log(docs); + // TODO done(); }); }); diff --git a/test/model.test.js b/test/model.test.js index e979b97..2a8c304 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -10,7 +10,7 @@ describe('Model', function () { describe('Serialization, deserialization', function () { - it('Can serialize and deserialize strings', function (done) { + it('Can serialize and deserialize strings', function () { var a, b, c; a = { test: "Some string" }; @@ -28,11 +28,9 @@ describe('Model', function () { a.test.indexOf('\n').should.not.equal(-1); b.indexOf('\n').should.equal(-1); c.test.indexOf('\n').should.not.equal(-1); - - done(); }); - it('Can serialize and deserialize booleans', function (done) { + it('Can serialize and deserialize booleans', function () { var a, b, c; a = { test: true }; @@ -40,11 +38,9 @@ describe('Model', function () { c = model.deserialize(b); b.indexOf('\n').should.equal(-1); c.test.should.equal(true); - - done(); }); - it('Can serialize and deserialize numbers', function (done) { + it('Can serialize and deserialize numbers', function () { var a, b, c; a = { test: 5 }; @@ -52,11 +48,9 @@ describe('Model', function () { c = model.deserialize(b); b.indexOf('\n').should.equal(-1); c.test.should.equal(5); - - done(); }); - it('Can serialize and deserialize null', function (done) { + it('Can serialize and deserialize null', function () { var a, b, c; a = { test: null }; @@ -64,11 +58,20 @@ describe('Model', function () { c = model.deserialize(b); b.indexOf('\n').should.equal(-1); assert.isNull(a.test); + }); - done(); + it('undefined fields are removed when serialized', function() { + var a = { bloup: undefined, hello: 'world' } + , b = model.serialize(a) + , c = model.deserialize(b) + ; + + Object.keys(c).length.should.equal(1); + c.hello.should.equal('world'); + assert.isUndefined(c.bloup); }); - it('Can serialize and deserialize a date', function (done) { + it('Can serialize and deserialize a date', function () { var a, b, c , d = new Date(); @@ -78,11 +81,9 @@ describe('Model', function () { b.indexOf('\n').should.equal(-1); c.test.constructor.name.should.equal('Date'); c.test.getTime().should.equal(d.getTime()); - - done(); }); - it('Can serialize and deserialize sub objects', function (done) { + it('Can serialize and deserialize sub objects', function () { var a, b, c , d = new Date(); @@ -93,11 +94,9 @@ describe('Model', function () { c.test.something.should.equal(39); c.test.also.getTime().should.equal(d.getTime()); c.test.yes.again.should.equal('yes'); - - done(); }); - it('Can serialize and deserialize sub arrays', function (done) { + it('Can serialize and deserialize sub arrays', function () { var a, b, c , d = new Date(); @@ -108,8 +107,6 @@ describe('Model', function () { c.test[0].should.equal(39); c.test[1].getTime().should.equal(d.getTime()); c.test[2].again.should.equal('yes'); - - done(); }); it('Reject field names beginning with a $ sign', function (done) { @@ -328,7 +325,7 @@ describe('Model', function () { }); // ==== End of 'Modifying documents' ==== // - describe('Finding documents', function () { + describe.only('Finding documents', function () { describe('Comparing things', function () {