UNPKG

modifyjs

Version:

Modify your objects with a mongo syntax.

1 lines 8.77 kB
"use strict";function _interopDefault(r){return r&&"object"==typeof r&&"default"in r?r["default"]:r}var clone=_interopDefault(require("clone")),equal=_interopDefault(require("deep-equal")),_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},_extends=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var i in o)Object.prototype.hasOwnProperty.call(o,i)&&(r[i]=o[i])}return r},isObject=function(r){return"object"===("undefined"==typeof r?"undefined":_typeof(r))&&null!==r},every=function(r,e){return r.every(function(r){return e&&e(r)||r})},has=function(r,e){return r.hasOwnProperty(e)},keys=function(r){return Object.keys(r)},each=function(r,e){Object.keys(r).forEach(function(o){e(r[o],o)})},libIsArray=function(r){return Array.isArray(r)},isBinary=function(r){return!!("undefined"!=typeof Uint8Array&&r instanceof Uint8Array||r&&r.$Uint8ArrayPolyfill)},_$1={isArray:libIsArray,each:each},isArray=function(r){return _$1.isArray(r)&&!isBinary(r)},isIndexable=function(r){return isArray(r)||isPlainObject(r)},isOperatorObject=function(r,e){if(!isPlainObject(r))return!1;var o=void 0;return _$1.each(r,function(i,n){var t="$"===n.substr(0,1);if(void 0===o)o=t;else if(o!==t){if(!e)throw new Error("Inconsistent operator: "+JSON.stringify(r));o=!1}}),!!o},isNumericKey=function(r){return/^[0-9]+$/.test(r)},isPlainObject=function(r){return r?"number"==typeof r?!1:"string"==typeof r?!1:"boolean"==typeof r?!1:isArray(r)?!1:null===r?!1:r instanceof RegExp?!1:"function"==typeof r?!1:r instanceof Date?!1:isBinary(r)?!1:!0:!1},ModifyJsError=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};"string"==typeof r&&e.field&&(r+=" for field '"+e.field+"'");var o=new Error(r);return o.name="ModifyJsError",o},_={all:every,each:each,keys:keys,has:has,isObject:isObject},modify=function(r,e,o){return o&&o.each?void 0:_modify(r,e,_extends({},o,{returnInsteadOfReplacing:!0}))},_modify=function(r,e,o){if(o=o||{},!isPlainObject(e))throw ModifyJsError("Modifier must be an object");e=clone(e);var i,n=isOperatorObject(e);return n?(i=clone(r),_.each(e,function(r,e){var n=MODIFIERS[e];if(!n)throw ModifyJsError("Invalid modifier specified "+e);_.each(r,function(r,t){if(""===t)throw ModifyJsError("An empty update path is not valid.");var f=t.split(".");if(!_.all(f))throw ModifyJsError("The update path '"+t+"' contains an empty field name, which is not allowed.");var a=findModTarget(i,f,{noCreate:NO_CREATE_MODIFIERS[e],forbidArray:"$rename"===e,arrayIndices:o.arrayIndices}),l=f.pop();n(a,l,r,t,i)})})):i=e,o.returnInsteadOfReplacing?i:(_.each(_.keys(r),function(e){"_id"!==e&&delete r[e]}),void _.each(i,function(e,o){r[o]=e}))},findModTarget=function(r,e,o){o=o||{};for(var i=!1,n=0;n<e.length;n++){var t=n===e.length-1,f=e[n],a=isIndexable(r);if(!a){if(o.noCreate)return void 0;var l=ModifyJsError("cannot use the part '"+f+"' to traverse "+r);throw l.setPropertyError=!0,l}if(r instanceof Array){if(o.forbidArray)return null;if("$"===f){if(i)throw ModifyJsError("Too many positional (i.e. '$') elements");if(!o.arrayIndices||!o.arrayIndices.length)throw ModifyJsError("The positional operator did not find the match needed from the query");f=o.arrayIndices[0],i=!0}else{if(!isNumericKey(f)){if(o.noCreate)return void 0;throw ModifyJsError("can't append to array using string field name ["+f+"]")}f=parseInt(f)}if(t&&(e[n]=f),o.noCreate&&f>=r.length)return void 0;for(;r.length<f;)r.push(null);if(!t)if(r.length===f)r.push({});else if("object"!==_typeof(r[f]))throw ModifyJsError("can't modify field '"+e[n+1]+"' of list value "+JSON.stringify(r[f]))}else if(console.log("Gozdecki: keyPart",f),!(f in r)){if(o.noCreate)return void 0;t||(r[f]={})}if(t)return r;r=r[f]}},NO_CREATE_MODIFIERS={$unset:!0,$pop:!0,$rename:!0,$pull:!0,$pullAll:!0},MODIFIERS={$currentDate:function(r,e,o){if("object"===("undefined"==typeof o?"undefined":_typeof(o))&&o.hasOwnProperty("$type")){if("date"!==o.$type)throw ModifyJsError("Minimongo does currently only support the date type in $currentDate modifiers",{field:e})}else if(o!==!0)throw ModifyJsError("Invalid $currentDate modifier",{field:e});r[e]=new Date},$min:function(r,e,o){if("number"!=typeof o)throw ModifyJsError("Modifier $min allowed for numbers only",{field:e});if(e in r){if("number"!=typeof r[e])throw ModifyJsError("Cannot apply $min modifier to non-number",{field:e});r[e]>o&&(r[e]=o)}else r[e]=o},$max:function(r,e,o){if("number"!=typeof o)throw ModifyJsError("Modifier $max allowed for numbers only",{field:e});if(e in r){if("number"!=typeof r[e])throw ModifyJsError("Cannot apply $max modifier to non-number",{field:e});r[e]<o&&(r[e]=o)}else r[e]=o},$inc:function(r,e,o){if("number"!=typeof o)throw ModifyJsError("Modifier $inc allowed for numbers only",{field:e});if(e in r){if("number"!=typeof r[e])throw ModifyJsError("Cannot apply $inc modifier to non-number",{field:e});r[e]+=o}else r[e]=o},$set:function(r,e,o){if(!_.isObject(r)){var i=ModifyJsError("Cannot set property on non-object field",{field:e});throw i.setPropertyError=!0,i}if(null===r){var i=ModifyJsError("Cannot set property on null",{field:e});throw i.setPropertyError=!0,i}r[e]=o},$setOnInsert:function(){},$unset:function(r,e){void 0!==r&&(r instanceof Array?e in r&&(r[e]=null):delete r[e])},$push:function(r,e,o){if(void 0===r[e]&&(r[e]=[]),!(r[e]instanceof Array))throw ModifyJsError("Cannot apply $push modifier to non-array",{field:e});if(!o||!o.$each)return void r[e].push(o);var i=o.$each;if(!(i instanceof Array))throw ModifyJsError("$each must be an array",{field:e});var n=void 0;if("$position"in o){if("number"!=typeof o.$position)throw ModifyJsError("$position must be a numeric value",{field:e});if(o.$position<0)throw ModifyJsError("$position in $push must be zero or positive",{field:e});n=o.$position}var t=void 0;if("$slice"in o){if("number"!=typeof o.$slice)throw ModifyJsError("$slice must be a numeric value",{field:e});if(o.$slice>0)throw ModifyJsError("$slice in $push must be zero or negative",{field:e});t=o.$slice}var f=void 0;if(o.$sort)throw ModifyJsError("$sort in $push not implemented yet");if(void 0===n)for(var a=0;a<i.length;a++)r[e].push(i[a]);else{for(var l=[n,0],a=0;a<i.length;a++)l.push(i[a]);Array.prototype.splice.apply(r[e],l)}f&&r[e].sort(f),void 0!==t&&(r[e]=0===t?[]:r[e].slice(t))},$pushAll:function(r,e,o){if(!("object"===("undefined"==typeof o?"undefined":_typeof(o))&&o instanceof Array))throw ModifyJsError("Modifier $pushAll/pullAll allowed for arrays only");var i=r[e];if(void 0===i)r[e]=o;else{if(!(i instanceof Array))throw ModifyJsError("Cannot apply $pushAll modifier to non-array",{field:e});for(var n=0;n<o.length;n++)i.push(o[n])}},$addToSet:function(r,e,o){var i=!1;if("object"===("undefined"==typeof o?"undefined":_typeof(o))){var n=Object.keys(o);"$each"===n[0]&&(i=!0)}var t=i?o.$each:[o],f=r[e];if(void 0===f)r[e]=t;else{if(!(f instanceof Array))throw ModifyJsError("Cannot apply $addToSet modifier to non-array",{field:e});_.each(t,function(r){for(var e=0;e<f.length;e++)if(equal(r,f[e]))return;f.push(r)})}},$pop:function(r,e,o){if(void 0!==r){var i=r[e];if(void 0!==i){if(!(i instanceof Array))throw ModifyJsError("Cannot apply $pop modifier to non-array",{field:e});"number"==typeof o&&0>o?i.splice(0,1):i.pop()}}},$pull:function(r,e){if(void 0!==r){var o=r[e];if(void 0!==o)throw o instanceof Array?ModifyJsError("$pull not implemented yet"):ModifyJsError("Cannot apply $pull/pullAll modifier to non-array",{field:e})}},$pullAll:function(r,e,o){if(!("object"===("undefined"==typeof o?"undefined":_typeof(o))&&o instanceof Array))throw ModifyJsError("Modifier $pushAll/pullAll allowed for arrays only",{field:e});if(void 0!==r){var i=r[e];if(void 0!==i){if(!(i instanceof Array))throw ModifyJsError("Cannot apply $pull/pullAll modifier to non-array",{field:e});for(var n=[],t=0;t<i.length;t++){for(var f=!1,a=0;a<o.length;a++)if(equal(i[t],o[a])){f=!0;break}f||n.push(i[t])}r[e]=n}}},$rename:function(r,e,o,i,n){if(i===o)throw ModifyJsError("$rename source must differ from target",{field:e});if(null===r)throw ModifyJsError("$rename source field invalid",{field:e});if("string"!=typeof o)throw ModifyJsError("$rename target must be a string",{field:e});if(o.indexOf("\x00")>-1)throw ModifyJsError("The 'to' field for $rename cannot contain an embedded null byte",{field:e});if(void 0!==r){var t=r[e];delete r[e];var f=o.split("."),a=findModTarget(n,f,{forbidArray:!0});if(null===a)throw ModifyJsError("$rename target field invalid",{field:e});var l=f.pop();a[l]=t}},$bit:function(r,e){throw ModifyJsError("$bit is not supported",{field:e})}};module.exports=modify;