js-immutables
Version:
A small library to handle immutable data.
2 lines (1 loc) • 5.51 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var __assign=function(){return(__assign=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var p in r=arguments[e])Object.prototype.hasOwnProperty.call(r,p)&&(t[p]=r[p]);return t}).apply(this,arguments)};function __spreadArrays(){for(var t=0,r=0,e=arguments.length;r<e;r++)t+=arguments[r].length;var n=Array(t),p=0;for(r=0;r<e;r++)for(var a=arguments[r],o=0,i=a.length;o<i;o++,p++)n[p]=a[o];return n}function update(e,t){if("string"==typeof t)return update(e).path(t);if("function"!=typeof t)return createHandler(e,[],ObjectModifierImpl,ArrayModifierImpl);return performUpdates(e,Array.from(t(function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return createHandler(e,t,ObjectUpdaterImpl,ArrayUpdaterImpl)})))}var ObjectModifierImpl=function(){function e(t,r){this._state=t,this._path=r}return e.prototype.path=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return createHandler(this._state,__spreadArrays(this._path,t),e,ArrayModifierImpl)},e.prototype.map=function(t,r){return performUpdate(this._state,this._path,ObjectOps.map(t,r))},e.prototype.set=function(t,r){return performUpdate(this._state,this._path,ObjectOps.set(t,r))},e}(),ArrayModifierImpl=function(){function e(t,r){this._state=t,this._path=r}return e.prototype.path=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return createHandler(this._state,__spreadArrays(this._path,t),ObjectModifierImpl,e)},e.prototype.push=function(t){return performUpdate(this._state,this._path,ArrayOps.push(t))},e.prototype.map=function(t){return performUpdate(this._state,this._path,ArrayOps.map(t))},e.prototype.mapFirst=function(t,r){return performUpdate(this._state,this._path,ArrayOps.mapFirst(t,r))},e.prototype.pop=function(){return performUpdate(this._state,this._path,ArrayOps.pop())},e.prototype.filter=function(t){return performUpdate(this._state,this._path,ArrayOps.filter(t))},e.prototype.remove=function(t){return performUpdate(this._state,this._path,ArrayOps.remove(t))},e.prototype.removeFirst=function(t){return performUpdate(this._state,this._path,ArrayOps.removeFirst(t))},e.prototype.removeLast=function(t){return performUpdate(this._state,this._path,ArrayOps.removeLast(t))},e.prototype.clear=function(){return performUpdate(this._state,this._path,ArrayOps.clear())},e}(),ObjectUpdaterImpl=function(){function r(t,r){this._state=t,this._path=r}return r.prototype.select=function(t){return createHandler(this._state,__spreadArrays(this._path,t),r,ArrayUpdaterImpl)},r.prototype.map=function(t,r){return createUpdate(this._path,ObjectOps.map(t,r))},r.prototype.set=function(t,r){return createUpdate(this._path,ObjectOps.set(t,r))},r}(),ArrayUpdaterImpl=function(){function r(t,r){this._state=t,this._path=r}return r.prototype.select=function(t){return createHandler(this._state,__spreadArrays(this._path,t),ObjectUpdaterImpl,r)},r.prototype.push=function(t){return createUpdate(this._path,ArrayOps.push(t))},r.prototype.pop=function(){return createUpdate(this._path,ArrayOps.pop())},r.prototype.map=function(t){return createUpdate(this._path,ArrayOps.map(t))},r.prototype.mapFirst=function(t,r){return createUpdate(this._path,ArrayOps.mapFirst(t,r))},r.prototype.filter=function(t){return createUpdate(this._path,ArrayOps.filter(t))},r.prototype.remove=function(t){return createUpdate(this._path,ArrayOps.remove(t))},r.prototype.removeFirst=function(t){return createUpdate(this._path,ArrayOps.removeFirst(t))},r.prototype.removeLast=function(t){return createUpdate(this._path,ArrayOps.removeLast(t))},r.prototype.clear=function(){return createUpdate(this._path,ArrayOps.clear())},r}(),ObjectOps={set:function(e,n){return function(t){var r;return __assign(__assign({},t),((r={})[e]=n,r))}},map:function(e,n){return function(t){var r;return __assign(__assign({},t),((r={})[e]=n(t[e]),r))}}},ArrayOps={push:function(r){return function(t){return __spreadArrays(t,[r])}},pop:function(){return function(t){var r=__spreadArrays(t);return r.pop(),r}},map:function(r){return function(t){return t.map(r)}},mapFirst:function(e,n){return function(t){var r=t.findIndex(e);return-1===r?t:t.slice(0,r).concat(n(t[r],r),t.slice(r+1))}},filter:function(r){return function(t){return t.filter(r)}},remove:function(e){return function(t){return t.filter(function(t,r){return!e(t,r)})}},removeFirst:function(e){return function(t){var r=t.findIndex(e);return-1===r?t:0===r?t.slice(1):t.slice(0,r).concat(t.slice(r+1))}},removeLast:function(e){return function(t){for(var r=t.length-1;0<=r;--r)if(e(t[r],r))return 0===r?t.slice(1):t.slice(0,r).concat(t.slice(r+1));return t}},clear:function(){return function(t){return[]}}};function createHandler(t,r,e,n){var p=null;if(t){for(var a=t,o=0;o<r.length;++o){if(!(a=a[r[o]]))break}Array.isArray(a)?p=new n(t,r):a&&"object"==typeof a&&(p=new e(t,r))}return p}function createUpdate(t,r){return{path:t,mapper:r}}function performUpdates(t,r){var o=shallowCopy(t),i=1<r.length?{}:null;return r.forEach(function(t){var r=t.path,e=t.mapper,n="",p=o,a=null;r.forEach(function(t,r){n=0===r?t:"@"+t,i&&hasOwnProp(i,n)||(p[t]=shallowCopy(p[t]),i&&(i[n]=!0)),p=(a=p)[t]}),a?a[r[r.length-1]]=e(p):o=e(p)}),o}function performUpdate(t,r,e){return performUpdates(t,[{path:r,mapper:e}])}function shallowCopy(t){return Array.isArray(t)?__spreadArrays(t):t&&"object"==typeof t?__assign({},t):t}function hasOwnProp(t,r){return Object.prototype.hasOwnProperty.call(t,r)}exports.update=update;