Skip to content

Commit

Permalink
Added modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Aug 10, 2013
1 parent 08aa7b0 commit eefe3a5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ lastStepModifierFunctions.$set = function (obj, field, value) {
};


/**
* Unset a field
*/
lastStepModifierFunctions.$unset = function (obj, field, value) {
delete obj[field];
};


/**
* Push an element to the end of an array field
*/
Expand Down
42 changes: 42 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,48 @@ describe('Model', function () {
_.isEqual(modified, { yup: { subfield: 'changed', yop: 'yes indeed' }, totally: { doesnt: { exist: 'now it does' } } }).should.equal(true);
});
}); // End of '$set modifier'

describe('$unset modifier', function () {

it('Can delete a field, not throwing an error if the field doesnt exist', function () {
var obj, updateQuery, modified;

obj = { yup: 'yes', other: 'also' }
updateQuery = { $unset: { yup: true } }
modified = model.modify(obj, updateQuery);
assert.deepEqual(modified, { other: 'also' });

obj = { yup: 'yes', other: 'also' }
updateQuery = { $unset: { nope: true } }
modified = model.modify(obj, updateQuery);
assert.deepEqual(modified, obj);

obj = { yup: 'yes', other: 'also' }
updateQuery = { $unset: { nope: true, other: true } }
modified = model.modify(obj, updateQuery);
assert.deepEqual(modified, { yup: 'yes' });
});

it('Can unset sub-fields and entire nested documents', function () {
var obj, updateQuery, modified;

obj = { yup: 'yes', nested: { a: 'also', b: 'yeah' } }
updateQuery = { $unset: { nested: true } }
modified = model.modify(obj, updateQuery);
assert.deepEqual(modified, { yup: 'yes' });

obj = { yup: 'yes', nested: { a: 'also', b: 'yeah' } }
updateQuery = { $unset: { 'nested.a': true } }
modified = model.modify(obj, updateQuery);
assert.deepEqual(modified, { yup: 'yes', nested: { b: 'yeah' } });

obj = { yup: 'yes', nested: { a: 'also', b: 'yeah' } }
updateQuery = { $unset: { 'nested.a': true, 'nested.b': true } }
modified = model.modify(obj, updateQuery);
assert.deepEqual(modified, { yup: 'yes', nested: {} });
});

}); // End of '$unset modifier'

describe('$inc modifier', function () {
it('Throw an error if you try to use it with a non-number or on a non number field', function () {
Expand Down

0 comments on commit eefe3a5

Please sign in to comment.