|
|
|
@ -223,12 +223,17 @@ function compareThings (a, b) { |
|
|
|
|
* @param {Model} value |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// Set a field to a new value
|
|
|
|
|
/** |
|
|
|
|
* Set a field to a new value |
|
|
|
|
*/ |
|
|
|
|
lastStepModifierFunctions.$set = function (obj, field, value) { |
|
|
|
|
obj[field] = value; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Push an element to the end of an array field
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Push an element to the end of an array field |
|
|
|
|
*/ |
|
|
|
|
lastStepModifierFunctions.$push = function (obj, field, value) { |
|
|
|
|
// Create the array if it doesn't exist
|
|
|
|
|
if (!obj.hasOwnProperty(field)) { obj[field] = []; } |
|
|
|
@ -237,9 +242,12 @@ lastStepModifierFunctions.$push = function (obj, field, value) { |
|
|
|
|
obj[field].push(value); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Add an element to an array field only if it is not already in it
|
|
|
|
|
// No modification if the element is already in the array
|
|
|
|
|
// Note that it doesn't check whether the original array contains duplicates
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Add an element to an array field only if it is not already in it |
|
|
|
|
* No modification if the element is already in the array |
|
|
|
|
* Note that it doesn't check whether the original array contains duplicates |
|
|
|
|
*/ |
|
|
|
|
lastStepModifierFunctions.$addToSet = function (obj, field, value) { |
|
|
|
|
// Create the array if it doesn't exist
|
|
|
|
|
if (!obj.hasOwnProperty(field)) { obj[field] = []; } |
|
|
|
@ -248,7 +256,26 @@ lastStepModifierFunctions.$addToSet = function (obj, field, value) { |
|
|
|
|
if (obj[field].indexOf(value) === -1) { obj[field].push(value); } |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Increment a numeric field's value
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Remove the first or last element of an array |
|
|
|
|
*/ |
|
|
|
|
lastStepModifierFunctions.$pop = function (obj, field, value) { |
|
|
|
|
if (!util.isArray(obj[field])) { throw "Can't $pop an element from non-array values"; } |
|
|
|
|
if (typeof value !== 'number') { throw value + " isn't an integer, can't use it with $pop"; } |
|
|
|
|
if (value === 0) { return; } |
|
|
|
|
|
|
|
|
|
if (value > 0) { |
|
|
|
|
obj[field] = obj[field].slice(0, obj[field].length - 1); |
|
|
|
|
} else { |
|
|
|
|
obj[field] = obj[field].slice(1); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Increment a numeric field's value |
|
|
|
|
*/ |
|
|
|
|
lastStepModifierFunctions.$inc = function (obj, field, value) { |
|
|
|
|
if (typeof value !== 'number') { throw value + " must be a number"; } |
|
|
|
|
|
|
|
|
|