Fix for node v4 and ES6

pull/2/head
Louis Chatriot 9 years ago
parent 6bebcf2ebd
commit 28abf624f0
  1. 7
      lib/model.js
  2. 6
      package.json
  3. 19
      test/model.test.js

@ -409,12 +409,13 @@ function modify (obj, updateQuery) {
if (!modifierFunctions[m]) { throw "Unknown modifier " + m; } if (!modifierFunctions[m]) { throw "Unknown modifier " + m; }
try { // Can't rely on Object.keys throwing on non objects since ES6{
keys = Object.keys(updateQuery[m]); // Not 100% satisfying as non objects can be interpreted as objects but no false negatives so we can live with it
} catch (e) { if (typeof updateQuery[m] !== 'object') {
throw "Modifier " + m + "'s argument must be an object"; throw "Modifier " + m + "'s argument must be an object";
} }
keys = Object.keys(updateQuery[m]);
keys.forEach(function (k) { keys.forEach(function (k) {
modifierFunctions[m](newDoc, k, updateQuery[m][k]); modifierFunctions[m](newDoc, k, updateQuery[m][k]);
}); });

@ -21,12 +21,12 @@
}, },
"dependencies": { "dependencies": {
"async": "0.2.10", "async": "0.2.10",
"underscore": "~1.4.4",
"binary-search-tree": "0.2.4", "binary-search-tree": "0.2.4",
"mkdirp": "~0.5.1" "mkdirp": "~0.5.1",
"underscore": "~1.4.4"
}, },
"devDependencies": { "devDependencies": {
"chai": "1.0.x", "chai": "^3.2.0",
"mocha": "1.4.x", "mocha": "1.4.x",
"request": "2.9.x", "request": "2.9.x",
"sinon": "1.3.x", "sinon": "1.3.x",

@ -1,6 +1,7 @@
var model = require('../lib/model') var model = require('../lib/model')
, should = require('chai').should() , should = require('chai').should()
, assert = require('chai').assert , assert = require('chai').assert
, expect = require('chai').expect
, _ = require('underscore') , _ = require('underscore')
, async = require('async') , async = require('async')
, util = require('util') , util = require('util')
@ -309,9 +310,9 @@ describe('Model', function () {
, updateQuery = { replace: 'done', bloup: [ 1, 8], _id: 'donttryit' } , updateQuery = { replace: 'done', bloup: [ 1, 8], _id: 'donttryit' }
; ;
(function () { expect(function () {
model.modify(obj, updateQuery); model.modify(obj, updateQuery);
}).should.throw(); }).to.throw("You cannot change a document's _id");
updateQuery._id = 'keepit'; updateQuery._id = 'keepit';
model.modify(obj, updateQuery); // No error thrown model.modify(obj, updateQuery); // No error thrown
@ -321,27 +322,27 @@ describe('Model', function () {
var obj = { some: 'thing' } var obj = { some: 'thing' }
, updateQuery = { replace: 'me', $modify: 'metoo' }; , updateQuery = { replace: 'me', $modify: 'metoo' };
(function () { expect(function () {
model.modify(obj, updateQuery); model.modify(obj, updateQuery);
}).should.throw(); }).to.throw("You cannot mix modifiers and normal fields");
}); });
it('Throw an error if trying to use an inexistent modifier', function () { it('Throw an error if trying to use an inexistent modifier', function () {
var obj = { some: 'thing' } var obj = { some: 'thing' }
, updateQuery = { $set: 'this exists', $modify: 'not this one' }; , updateQuery = { $set: { it: 'exists' }, $modify: 'not this one' };
(function () { expect(function () {
model.modify(obj, updateQuery); model.modify(obj, updateQuery);
}).should.throw(); }).to.throw(/^Unknown modifier .modify/);
}); });
it('Throw an error if a modifier is used with a non-object argument', function () { it('Throw an error if a modifier is used with a non-object argument', function () {
var obj = { some: 'thing' } var obj = { some: 'thing' }
, updateQuery = { $set: 'this exists' }; , updateQuery = { $set: 'this exists' };
(function () { expect(function () {
model.modify(obj, updateQuery); model.modify(obj, updateQuery);
}).should.throw(); }).to.throw(/Modifier .set's argument must be an object/);
}); });
describe('$set modifier', function () { describe('$set modifier', function () {

Loading…
Cancel
Save