Sticking to style

pull/2/head
Louis Chatriot 9 years ago
parent 8b4e0ecc90
commit 58edd6e407
  1. 14
      lib/model.js
  2. 76
      test/model.test.js

@ -390,16 +390,22 @@ lastStepModifierFunctions.$inc = function (obj, field, value) {
* Updates the value of the field, only if specified field is greater than the current value of the field * Updates the value of the field, only if specified field is greater than the current value of the field
*/ */
lastStepModifierFunctions.$max = function (obj, field, value) { lastStepModifierFunctions.$max = function (obj, field, value) {
if (typeof obj[field] === 'undefined') obj[field] = value; if (typeof obj[field] === 'undefined') {
else if (value > obj[field]) obj[field] = value; obj[field] = value;
} else if (value > obj[field]) {
obj[field] = value;
}
}; };
/** /**
* Updates the value of the field, only if specified field is smaller than the current value of the field * Updates the value of the field, only if specified field is smaller than the current value of the field
*/ */
lastStepModifierFunctions.$min = function (obj, field, value) { lastStepModifierFunctions.$min = function (obj, field, value) {
if (typeof obj[field] === 'undefined') obj[field] = value; if (typeof obj[field] === 'undefined') { 
else if (value < obj[field]) obj[field] = value; obj[field] = value;
} else if (value < obj[field]) {
obj[field] = value;
}
}; };
// Given its name, create the complete modifier function // Given its name, create the complete modifier function

@ -709,15 +709,15 @@ describe('Model', function () {
modified = model.modify(obj, { $pull: { arr: { b: 3 } } }); modified = model.modify(obj, { $pull: { arr: { b: 3 } } });
assert.deepEqual(modified, { arr: [{ b: 2 }] }); assert.deepEqual(modified, { arr: [{ b: 2 }] });
}); });
it('Can use any kind of nedb query with $pull', function () { it('Can use any kind of nedb query with $pull', function () {
var obj = { arr: [4, 7, 12, 2], other: 'yup' } var obj = { arr: [4, 7, 12, 2], other: 'yup' }
, modified , modified
; ;
modified = model.modify(obj, { $pull: { arr: { $gte: 5 } } }); modified = model.modify(obj, { $pull: { arr: { $gte: 5 } } });
assert.deepEqual(modified, { arr: [4, 2], other: 'yup' }); assert.deepEqual(modified, { arr: [4, 2], other: 'yup' });
obj = { arr: [{ b: 4 }, { b: 7 }, { b: 1 }], other: 'yeah' }; obj = { arr: [{ b: 4 }, { b: 7 }, { b: 1 }], other: 'yeah' };
modified = model.modify(obj, { $pull: { arr: { b: { $gte: 5} } } }); modified = model.modify(obj, { $pull: { arr: { b: { $gte: 5} } } });
assert.deepEqual(modified, { arr: [{ b: 4 }, { b: 1 }], other: 'yeah' }); assert.deepEqual(modified, { arr: [{ b: 4 }, { b: 1 }], other: 'yeah' });
@ -727,73 +727,71 @@ describe('Model', function () {
describe('$max modifier', function () { describe('$max modifier', function () {
it('Will set the field to the updated value if value is greater than current one, without modifying the original object', function () { it('Will set the field to the updated value if value is greater than current one, without modifying the original object', function () {
var obj = { some:'thing', number: 10} var obj = { some:'thing', number: 10 }
, updateQuery = {$max:{number:12}} , updateQuery = { $max: { number:12 } }
, modified = model.modify(obj,updateQuery); , modified = model.modify(obj, updateQuery);
modified.should.deep.equal({some:'thing',number:12}); modified.should.deep.equal({ some: 'thing', number: 12 });
obj.should.deep.equal({ some: 'thing', number: 10 });
obj.should.deep.equal({some:'thing', number:10});
}); });
it('Will not update the field if new value is smaller than current one', function () { it('Will not update the field if new value is smaller than current one', function () {
var obj = { some:'thing', number: 10} var obj = { some:'thing', number: 10 }
, updateQuery = {$max:{number:9}} , updateQuery = { $max: { number: 9 } }
, modified = model.modify(obj,updateQuery); , modified = model.modify(obj, updateQuery);
modified.should.deep.equal({some:'thing',number:10}); modified.should.deep.equal({ some:'thing', number:10 });
}); });
it('Will create the field if it does not exist', function () { it('Will create the field if it does not exist', function () {
var obj = {some:'thing'} var obj = { some: 'thing' }
, updateQuery = {$max:{number:10}} , updateQuery = { $max: { number: 10 } }
, modified = model.modify(obj,updateQuery); , modified = model.modify(obj, updateQuery);
modified.should.deep.equal({some:'thing', number:10}); modified.should.deep.equal({ some: 'thing', number: 10 });
}); });
it('Works on embedded documents', function () { it('Works on embedded documents', function () {
var obj = {some:'thing', somethingElse:{number:10}} var obj = { some: 'thing', somethingElse: { number:10 } }
, updateQuery = {$max:{'somethingElse.number':12}} , updateQuery = { $max: { 'somethingElse.number': 12 } }
, modified = model.modify(obj,updateQuery); , modified = model.modify(obj,updateQuery);
modified.should.deep.equal({some:'thing',somethingElse:{number:12}}); modified.should.deep.equal({ some: 'thing', somethingElse: { number:12 } });
}); });
});// End of '$max modifier' });// End of '$max modifier'
describe('$min modifier', function () { describe('$min modifier', function () {
it('Will set the field to the updated value if value is smaller than current one, without modifying the original object', function () { it('Will set the field to the updated value if value is smaller than current one, without modifying the original object', function () {
var obj = { some:'thing', number: 10} var obj = { some:'thing', number: 10 }
, updateQuery = {$min:{number:8}} , updateQuery = { $min: { number: 8 } }
, modified = model.modify(obj,updateQuery); , modified = model.modify(obj, updateQuery);
modified.should.deep.equal({some:'thing',number:8}); modified.should.deep.equal({ some: 'thing', number: 8 });
obj.should.deep.equal({ some: 'thing', number: 10 });
obj.should.deep.equal({some:'thing', number:10});
}); });
it('Will not update the field if new value is greater than current one', function () { it('Will not update the field if new value is greater than current one', function () {
var obj = { some:'thing', number: 10} var obj = { some: 'thing', number: 10 }
, updateQuery = {$min:{number:12}} , updateQuery = { $min: { number: 12 } }
, modified = model.modify(obj,updateQuery); , modified = model.modify(obj, updateQuery);
modified.should.deep.equal({some:'thing',number:10}); modified.should.deep.equal({ some: 'thing', number: 10 });
}); });
it('Will create the field if it does not exist', function () { it('Will create the field if it does not exist', function () {
var obj = {some:'thing'} var obj = { some: 'thing' }
, updateQuery = {$min:{number:10}} , updateQuery = { $min: { number: 10 } }
, modified = model.modify(obj,updateQuery); , modified = model.modify(obj, updateQuery);
modified.should.deep.equal({some:'thing', number:10}); modified.should.deep.equal({ some: 'thing', number: 10 });
}); });
it('Works on embedded documents', function () { it('Works on embedded documents', function () {
var obj = {some:'thing', somethingElse:{number:10}} var obj = { some: 'thing', somethingElse: { number: 10 } }
, updateQuery = {$min:{'somethingElse.number':8}} , updateQuery = { $min: { 'somethingElse.number': 8 } }
, modified = model.modify(obj,updateQuery); , modified = model.modify(obj, updateQuery);
modified.should.deep.equal({some:'thing',somethingElse:{number:8}}); modified.should.deep.equal({ some: 'thing', somethingElse: { number: 8 } } );
}); });
});// End of '$min modifier' });// End of '$min modifier'

Loading…
Cancel
Save