js-immutables
Version:
A small library to handle immutable data.
16 lines (13 loc) • 5.27 kB
JavaScript
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var t=function(){return(t=Object.assign||function(t){for(var r,n=1,e=arguments.length;n<e;n++)for(var o in r=arguments[n])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t}).apply(this,arguments)};function r(){for(var t=0,r=0,n=arguments.length;r<n;r++)t+=arguments[r].length;var e=Array(t),o=0;for(r=0;r<n;r++)for(var i=arguments[r],u=0,p=i.length;u<p;u++,o++)e[o]=i[u];return e}function n(t,r){if("string"==typeof r)return n(t).path(r);if("function"==typeof r){return f(t,Array.from(r((function(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];return s(t,r,i,u)}))))}return s(t,[],e,o)}var e=function(){function t(t,r){this._state=t,this._path=r}return t.prototype.path=function(){for(var n=[],e=0;e<arguments.length;e++)n[e]=arguments[e];return s(this._state,r(this._path,n),t,o)},t.prototype.map=function(t,r){return c(this._state,this._path,p.map(t,r))},t.prototype.set=function(t,r){return c(this._state,this._path,p.set(t,r))},t}(),o=function(){function t(t,r){this._state=t,this._path=r}return t.prototype.path=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return s(this._state,r(this._path,n),e,t)},t.prototype.push=function(t){return c(this._state,this._path,a.push(t))},t.prototype.map=function(t){return c(this._state,this._path,a.map(t))},t.prototype.mapFirst=function(t,r){return c(this._state,this._path,a.mapFirst(t,r))},t.prototype.pop=function(){return c(this._state,this._path,a.pop())},t.prototype.filter=function(t){return c(this._state,this._path,a.filter(t))},t.prototype.remove=function(t){return c(this._state,this._path,a.remove(t))},t.prototype.removeFirst=function(t){return c(this._state,this._path,a.removeFirst(t))},t.prototype.removeLast=function(t){return c(this._state,this._path,a.removeLast(t))},t.prototype.clear=function(){return c(this._state,this._path,a.clear())},t}(),i=function(){function t(t,r){this._state=t,this._path=r}return t.prototype.select=function(n){return s(this._state,r(this._path,n),t,u)},t.prototype.map=function(t,r){return h(this._path,p.map(t,r))},t.prototype.set=function(t,r){return h(this._path,p.set(t,r))},t}(),u=function(){function t(t,r){this._state=t,this._path=r}return t.prototype.select=function(n){return s(this._state,r(this._path,n),i,t)},t.prototype.push=function(t){return h(this._path,a.push(t))},t.prototype.pop=function(){return h(this._path,a.pop())},t.prototype.map=function(t){return h(this._path,a.map(t))},t.prototype.mapFirst=function(t,r){return h(this._path,a.mapFirst(t,r))},t.prototype.filter=function(t){return h(this._path,a.filter(t))},t.prototype.remove=function(t){return h(this._path,a.remove(t))},t.prototype.removeFirst=function(t){return h(this._path,a.removeFirst(t))},t.prototype.removeLast=function(t){return h(this._path,a.removeLast(t))},t.prototype.clear=function(){return h(this._path,a.clear())},t}(),p={set:function(r,n){return function(e){var o;return t(t({},e),((o={})[r]=n,o))}},map:function(r,n){return function(e){var o;return t(t({},e),((o={})[r]=n(e[r]),o))}}},a={push:function(t){return function(n){return r(n,[t])}},pop:function(){return function(t){var n=r(t);return n.pop(),n}},map:function(t){return function(r){return r.map(t)}},mapFirst:function(t,r){return function(n){var e=n.findIndex(t);return-1===e?n:n.slice(0,e).concat(r(n[e],e),n.slice(e+1))}},filter:function(t){return function(r){return r.filter(t)}},remove:function(t){return function(r){return r.filter((function(r,n){return!t(r,n)}))}},removeFirst:function(t){return function(r){var n=r.findIndex(t);return-1===n?r:0===n?r.slice(1):r.slice(0,n).concat(r.slice(n+1))}},removeLast:function(t){return function(r){for(var n=r.length-1;n>=0;--n)if(t(r[n],n))return 0===n?r.slice(1):r.slice(0,n).concat(r.slice(n+1));return r}},clear:function(){return function(t){return[]}}};function s(t,r,n,e){var o=null;if(t){for(var i=t,u=0;u<r.length;++u){if(!(i=i[r[u]]))break}Array.isArray(i)?o=new e(t,r):i&&"object"==typeof i&&(o=new n(t,r))}return o}function h(t,r){return{path:t,mapper:r}}function f(t,r){var n=_(t),e=r.length>1?{}:null;return r.forEach((function(t){var r=t.path,o=t.mapper,i="",u=n,p=null;r.forEach((function(t,r){var n,o;i=0===r?t:"@"+t,e&&(n=e,o=i,Object.prototype.hasOwnProperty.call(n,o))||(u[t]=_(u[t]),e&&(e[i]=!0)),p=u,u=u[t]})),p?p[r[r.length-1]]=o(u):n=o(u)})),n}function c(t,r,n){return f(t,[{path:r,mapper:n}])}function _(n){return Array.isArray(n)?r(n):n&&"object"==typeof n?t({},n):n}export{n as update};