UNPKG

js-immutables

Version:

A small library to handle immutable data.

2 lines (1 loc) 4.63 kB
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((t=t||self).jsMessages={})}(this,function(t){"use strict";var o=function(){return(o=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 n(){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],p=0,u=i.length;p<u;p++,o++)e[o]=i[p];return e}var e=(i.prototype.path=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return _(this._state,n(this._path,t),i,p)},i.prototype.map=function(t,r){return v(this._state,this._path,h.map(t,r))},i.prototype.set=function(t,r){return v(this._state,this._path,h.set(t,r))},i);function i(t,r){this._state=t,this._path=r}var p=(u.prototype.path=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return _(this._state,n(this._path,t),e,u)},u.prototype.push=function(t){return v(this._state,this._path,c.push(t))},u.prototype.map=function(t){return v(this._state,this._path,c.map(t))},u.prototype.mapFirst=function(t,r){return v(this._state,this._path,c.mapFirst(t,r))},u.prototype.pop=function(){return v(this._state,this._path,c.pop())},u.prototype.filter=function(t){return v(this._state,this._path,c.filter(t))},u.prototype.remove=function(t){return v(this._state,this._path,c.remove(t))},u.prototype.removeFirst=function(t){return v(this._state,this._path,c.removeFirst(t))},u.prototype.removeLast=function(t){return v(this._state,this._path,c.removeLast(t))},u.prototype.clear=function(){return v(this._state,this._path,c.clear())},u);function u(t,r){this._state=t,this._path=r}var a=(r.prototype.select=function(t){return _(this._state,n(this._path,t),r,s)},r.prototype.map=function(t,r){return l(this._path,h.map(t,r))},r.prototype.set=function(t,r){return l(this._path,h.set(t,r))},r);function r(t,r){this._state=t,this._path=r}var s=(f.prototype.select=function(t){return _(this._state,n(this._path,t),a,f)},f.prototype.push=function(t){return l(this._path,c.push(t))},f.prototype.pop=function(){return l(this._path,c.pop())},f.prototype.map=function(t){return l(this._path,c.map(t))},f.prototype.mapFirst=function(t,r){return l(this._path,c.mapFirst(t,r))},f.prototype.filter=function(t){return l(this._path,c.filter(t))},f.prototype.remove=function(t){return l(this._path,c.remove(t))},f.prototype.removeFirst=function(t){return l(this._path,c.removeFirst(t))},f.prototype.removeLast=function(t){return l(this._path,c.removeLast(t))},f.prototype.clear=function(){return l(this._path,c.clear())},f);function f(t,r){this._state=t,this._path=r}var h={set:function(n,e){return function(t){var r;return o(o({},t),((r={})[n]=e,r))}},map:function(n,e){return function(t){var r;return o(o({},t),((r={})[n]=e(t[n]),r))}}},c={push:function(r){return function(t){return n(t,[r])}},pop:function(){return function(t){var r=n(t);return r.pop(),r}},map:function(r){return function(t){return t.map(r)}},mapFirst:function(n,e){return function(t){var r=t.findIndex(n);return-1===r?t:t.slice(0,r).concat(e(t[r],r),t.slice(r+1))}},filter:function(r){return function(t){return t.filter(r)}},remove:function(n){return function(t){return t.filter(function(t,r){return!n(t,r)})}},removeFirst:function(n){return function(t){var r=t.findIndex(n);return-1===r?t:0===r?t.slice(1):t.slice(0,r).concat(t.slice(r+1))}},removeLast:function(n){return function(t){for(var r=t.length-1;0<=r;--r)if(n(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 _(t,r,n,e){var o=null;if(t){for(var i=t,p=0;p<r.length;++p){if(!(i=i[r[p]]))break}Array.isArray(i)?o=new e(t,r):i&&"object"==typeof i&&(o=new n(t,r))}return o}function l(t,r){return{path:t,mapper:r}}function y(t,r){var e=m(t),u=1<r.length?{}:null;return r.forEach(function(t){var r=t.path,n=t.mapper,o="",i=e,p=null;r.forEach(function(t,r){var n,e;o=0===r?t:"@"+t,u&&(n=u,e=o,Object.prototype.hasOwnProperty.call(n,e))||(i[t]=m(i[t]),u&&(u[o]=!0)),i=(p=i)[t]}),p?p[r[r.length-1]]=n(i):e=n(i)}),e}function v(t,r,n){return y(t,[{path:r,mapper:n}])}function m(t){return Array.isArray(t)?n(t):t&&"object"==typeof t?o({},t):t}t.update=function t(n,r){if("string"==typeof r)return t(n).path(r);if("function"!=typeof r)return _(n,[],e,p);return y(n,Array.from(r(function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return _(n,t,a,s)})))},Object.defineProperty(t,"__esModule",{value:!0})});