9 lines
4.3 KiB
JavaScript
9 lines
4.3 KiB
JavaScript
"use strict";function throwStr(e){throw new Error(e)}function getKeysAndSymbols(e){var r=Object.keys(e);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(e)):r}function clone(e){if(Array.isArray(e))return e.slice();for(var r=getKeysAndSymbols(e),t={},n=0;n<r.length;n++){var o=r[n];t[o]=e[o]}return t}function doMerge(e,r,t){var n=t;!(null!=n)&&throwStr(INVALID_ARGS);for(var o=!1,a=arguments.length,s=Array(a>3?a-3:0),l=3;l<a;l++)s[l-3]=arguments[l];for(var i=0;i<s.length;i++){var u=s[i];if(null!=u){var c=getKeysAndSymbols(u);if(c.length)for(var d=0;d<=c.length;d++){var f=c[d];if(!e||void 0===n[f]){var g=u[f];r&&isObject(n[f])&&isObject(g)&&(g=doMerge(e,r,n[f],g)),void 0!==g&&g!==n[f]&&(o||(o=!0,n=clone(n)),n[f]=g)}}}}return n}function isObject(e){var r=void 0===e?"undefined":_typeof(e);return null!=e&&"object"===r}function addLast(e,r){return Array.isArray(r)?e.concat(r):e.concat([r])}function addFirst(e,r){return Array.isArray(r)?r.concat(e):[r].concat(e)}function removeLast(e){return e.length?e.slice(0,e.length-1):e}function removeFirst(e){return e.length?e.slice(1):e}function insert(e,r,t){return e.slice(0,r).concat(Array.isArray(t)?t:[t]).concat(e.slice(r))}function removeAt(e,r){return r>=e.length||r<0?e:e.slice(0,r).concat(e.slice(r+1))}function replaceAt(e,r,t){if(e[r]===t)return e;for(var n=e.length,o=Array(n),a=0;a<n;a++)o[a]=e[a];return o[r]=t,o}function getIn(e,r){if(!Array.isArray(r)&&throwStr(INVALID_ARGS),null!=e){for(var t=e,n=0;n<r.length;n++){var o=r[n];if(void 0===(t=null!=t?t[o]:void 0))return t}return t}}function set(e,r,t){var n="number"==typeof r?[]:{},o=null==e?n:e;if(o[r]===t)return o;var a=clone(o);return a[r]=t,a}function doSetIn(e,r,t,n){var o=void 0,a=r[n];if(n===r.length-1)o=t;else{o=doSetIn(isObject(e)&&isObject(e[a])?e[a]:"number"==typeof r[n+1]?[]:{},r,t,n+1)}return set(e,a,o)}function setIn(e,r,t){return r.length?doSetIn(e,r,t,0):t}function update(e,r,t){return set(e,r,t(null==e?void 0:e[r]))}function updateIn(e,r,t){return setIn(e,r,t(getIn(e,r)))}function merge(e,r,t,n,o,a){for(var s=arguments.length,l=Array(s>6?s-6:0),i=6;i<s;i++)l[i-6]=arguments[i];return l.length?doMerge.call.apply(doMerge,[null,!1,!1,e,r,t,n,o,a].concat(l)):doMerge(!1,!1,e,r,t,n,o,a)}function mergeDeep(e,r,t,n,o,a){for(var s=arguments.length,l=Array(s>6?s-6:0),i=6;i<s;i++)l[i-6]=arguments[i];return l.length?doMerge.call.apply(doMerge,[null,!1,!0,e,r,t,n,o,a].concat(l)):doMerge(!1,!0,e,r,t,n,o,a)}function mergeIn(e,r,t,n,o,a,s){var l=getIn(e,r);null==l&&(l={});for(var i=void 0,u=arguments.length,c=Array(u>7?u-7:0),d=7;d<u;d++)c[d-7]=arguments[d];return i=c.length?doMerge.call.apply(doMerge,[null,!1,!1,l,t,n,o,a,s].concat(c)):doMerge(!1,!1,l,t,n,o,a,s),setIn(e,r,i)}function omit(e,r){for(var t=Array.isArray(r)?r:[r],n=!1,o=0;o<t.length;o++)if(hasOwnProperty.call(e,t[o])){n=!0;break}if(!n)return e;for(var a={},s=getKeysAndSymbols(e),l=0;l<s.length;l++){var i=s[l];t.indexOf(i)>=0||(a[i]=e[i])}return a}function addDefaults(e,r,t,n,o,a){for(var s=arguments.length,l=Array(s>6?s-6:0),i=6;i<s;i++)l[i-6]=arguments[i];return l.length?doMerge.call.apply(doMerge,[null,!0,!1,e,r,t,n,o,a].concat(l)):doMerge(!0,!1,e,r,t,n,o,a)}Object.defineProperty(exports,"__esModule",{value:!0});var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};exports.clone=clone,exports.addLast=addLast,exports.addFirst=addFirst,exports.removeLast=removeLast,exports.removeFirst=removeFirst,exports.insert=insert,exports.removeAt=removeAt,exports.replaceAt=replaceAt,exports.getIn=getIn,exports.set=set,exports.setIn=setIn,exports.update=update,exports.updateIn=updateIn,exports.merge=merge,exports.mergeDeep=mergeDeep,exports.mergeIn=mergeIn,exports.omit=omit,exports.addDefaults=addDefaults;/*!
|
|
* Timm
|
|
*
|
|
* Immutability helpers with fast reads and acceptable writes.
|
|
*
|
|
* @copyright Guillermo Grau Panea 2016
|
|
* @license MIT
|
|
*/
|
|
var INVALID_ARGS="INVALID_ARGS",hasOwnProperty={}.hasOwnProperty,timm={clone:clone,addLast:addLast,addFirst:addFirst,removeLast:removeLast,removeFirst:removeFirst,insert:insert,removeAt:removeAt,replaceAt:replaceAt,getIn:getIn,set:set,setIn:setIn,update:update,updateIn:updateIn,merge:merge,mergeDeep:mergeDeep,mergeIn:mergeIn,omit:omit,addDefaults:addDefaults};exports.default=timm; |