Skip to content

Commit

Permalink
Sticking to style
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Feb 3, 2016
1 parent 8b4e0ec commit 58edd6e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 43 deletions.
14 changes: 10 additions & 4 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
lastStepModifierFunctions.$max = function (obj, field, value) {
if (typeof obj[field] === 'undefined') obj[field] = value;
else if (value > obj[field]) obj[field] = value;
if (typeof obj[field] === 'undefined') {
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
*/
lastStepModifierFunctions.$min = function (obj, field, value) {
if (typeof obj[field] === 'undefined') obj[field] = value;
else if (value < obj[field]) obj[field] = value;
if (typeof obj[field] === 'undefined') { 
obj[field] = value;
} else if (value < obj[field]) {
obj[field] = value;
}
};

// Given its name, create the complete modifier function
Expand Down
76 changes: 37 additions & 39 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,15 +709,15 @@ describe('Model', function () {
modified = model.modify(obj, { $pull: { arr: { b: 3 } } });
assert.deepEqual(modified, { arr: [{ b: 2 }] });
});

it('Can use any kind of nedb query with $pull', function () {
var obj = { arr: [4, 7, 12, 2], other: 'yup' }
, modified
;
;

modified = model.modify(obj, { $pull: { arr: { $gte: 5 } } });
assert.deepEqual(modified, { arr: [4, 2], other: 'yup' });

obj = { arr: [{ b: 4 }, { b: 7 }, { b: 1 }], other: 'yeah' };
modified = model.modify(obj, { $pull: { arr: { b: { $gte: 5} } } });
assert.deepEqual(modified, { arr: [{ b: 4 }, { b: 1 }], other: 'yeah' });
Expand All @@ -727,73 +727,71 @@ describe('Model', 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 () {
var obj = { some:'thing', number: 10}
, updateQuery = {$max:{number:12}}
, modified = model.modify(obj,updateQuery);
var obj = { some:'thing', number: 10 }
, updateQuery = { $max: { number:12 } }
, modified = model.modify(obj, updateQuery);

modified.should.deep.equal({some:'thing',number:12});

obj.should.deep.equal({some:'thing', number:10});
modified.should.deep.equal({ some: 'thing', number: 12 });
obj.should.deep.equal({ some: 'thing', number: 10 });
});

it('Will not update the field if new value is smaller than current one', function () {
var obj = { some:'thing', number: 10}
, updateQuery = {$max:{number:9}}
, modified = model.modify(obj,updateQuery);
var obj = { some:'thing', number: 10 }
, updateQuery = { $max: { number: 9 } }
, 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 () {
var obj = {some:'thing'}
, updateQuery = {$max:{number:10}}
, modified = model.modify(obj,updateQuery);
var obj = { some: 'thing' }
, updateQuery = { $max: { number: 10 } }
, 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 () {
var obj = {some:'thing', somethingElse:{number:10}}
, updateQuery = {$max:{'somethingElse.number':12}}
var obj = { some: 'thing', somethingElse: { number:10 } }
, updateQuery = { $max: { 'somethingElse.number': 12 } }
, 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'

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 () {
var obj = { some:'thing', number: 10}
, updateQuery = {$min:{number:8}}
, modified = model.modify(obj,updateQuery);
var obj = { some:'thing', number: 10 }
, updateQuery = { $min: { number: 8 } }
, modified = model.modify(obj, updateQuery);

modified.should.deep.equal({some:'thing',number:8});

obj.should.deep.equal({some:'thing', number:10});
modified.should.deep.equal({ some: 'thing', number: 8 });
obj.should.deep.equal({ some: 'thing', number: 10 });
});

it('Will not update the field if new value is greater than current one', function () {
var obj = { some:'thing', number: 10}
, updateQuery = {$min:{number:12}}
, modified = model.modify(obj,updateQuery);
var obj = { some: 'thing', number: 10 }
, updateQuery = { $min: { number: 12 } }
, 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 () {
var obj = {some:'thing'}
, updateQuery = {$min:{number:10}}
, modified = model.modify(obj,updateQuery);
var obj = { some: 'thing' }
, updateQuery = { $min: { number: 10 } }
, 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 () {
var obj = {some:'thing', somethingElse:{number:10}}
, updateQuery = {$min:{'somethingElse.number':8}}
, modified = model.modify(obj,updateQuery);
var obj = { some: 'thing', somethingElse: { number: 10 } }
, updateQuery = { $min: { 'somethingElse.number': 8 } }
, 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'

Expand Down

0 comments on commit 58edd6e

Please sign in to comment.