immer
Version:
Create your next immutable state by mutating the current one
3 lines (2 loc) • 14.7 kB
JavaScript
function t(t){for(var n=arguments.length,r=Array(n>1?n-1:0),e=1;e<n;e++)r[e-1]=arguments[e];throw Error("[Immer] minified error nr: "+t+(r.length?" "+r.join(","):"")+". Find the full error at: https://bit.ly/3cXEKWf")}function n(t){return!!t&&!!t[X]}function r(t){return!!t&&(function(t){if(!t||"object"!=typeof t)return!1;var n=Object.getPrototypeOf(t);return!n||n===Object.prototype}(t)||Array.isArray(t)||!!t[W]||!!t.constructor[W]||a(t)||c(t))}function e(t,n){0===i(t)?B(t).forEach((function(r){return n(r,t[r],t)})):t.forEach((function(r,e){return n(e,r,t)}))}function i(t){var n=t[X];return n?n.t>3?n.t-4:n.t:Array.isArray(t)?1:a(t)?2:c(t)?3:0}function u(t,n){return 2===i(t)?t.has(n):Object.prototype.hasOwnProperty.call(t,n)}function o(t,n){return 2===i(t)?t.get(n):t[n]}function f(t,n){return t===n?0!==t||1/t==1/n:t!=t&&n!=n}function a(t){return N&&t instanceof Map}function c(t){return $&&t instanceof Set}function s(t){return t.i||t.u}function v(n,r){if(void 0===r&&(r=!1),Array.isArray(n))return n.slice();var i=Object.create(Object.getPrototypeOf(n));return e(n,(function(e){if(e!==X){var u=Object.getOwnPropertyDescriptor(n,e),o=u.value;u.get&&(r||t(1),o=u.get.call(n)),u.enumerable?i[e]=o:Object.defineProperty(i,e,{value:o,writable:!0,configurable:!0})}})),i}function p(t,u){n(t)||Object.isFrozen(t)||!r(t)||(i(t)>1&&(t.set=t.add=t.clear=t.delete=h),Object.freeze(t),u&&e(t,(function(t,n){return p(n,!0)})))}function h(){t(2)}function l(n){var r=G[n];return r||t(19,n),r}function d(t,n){G[t]=n}function _(){return J}function y(t,n){n&&(l("Patches"),t.o=[],t.s=[],t.v=n)}function b(t){m(t),t.p.forEach(j),t.p=null}function m(t){t===J&&(J=t.h)}function O(t){return J={p:[],h:J,l:t,_:!0,m:0}}function j(t){var n=t[X];0===n.t||1===n.t?n.O():n.j=!0}function x(n,e){e.m=e.p.length;var i=e.p[0],u=void 0!==n&&n!==i;return e.l.S||l("ES5").P(e,n,u),u?(i[X].M&&(b(e),t(4)),r(n)&&(n=w(e,n),e.h||P(e,n)),e.o&&l("Patches").g(i[X],n,e.o,e.s)):n=w(e,i,[]),b(e),e.o&&e.v(e.o,e.s),n!==I?n:void 0}function w(t,n,r){if(Object.isFrozen(n))return n;var i=n[X];if(!i)return e(n,(function(e,u){return S(t,i,n,e,u,r)})),n;if(i.A!==t)return n;if(!i.M)return P(t,i.u,!0),i.u;if(!i.R){i.R=!0,i.A.m--;var u=4===i.t||5===i.t?i.i=v(i.k,!0):i.i;e(u,(function(n,e){return S(t,i,u,n,e,r)})),P(t,u,!1),r&&t.o&&l("Patches").F(i,r,t.o,t.s)}return i.i}function S(t,e,a,c,s,v){if(n(s)){var p=w(t,s,v&&e&&3!==e.t&&!u(e.D,c)?v.concat(c):void 0);if(l=c,d=p,2===(_=i(h=a))?h.set(l,d):3===_?(h.delete(l),h.add(d)):h[l]=d,!n(p))return;t._=!1}var h,l,d,_;if((!e||!f(s,o(e.u,c)))&&r(s)){if(!t.l.J&&t.m<1)return;w(t,s),e&&e.A.h||P(t,s)}}function P(t,n,r){void 0===r&&(r=!1),t.l.J&&t._&&p(n,r)}function M(t,n){var r=t[X],e=Reflect.getOwnPropertyDescriptor(r?s(r):t,n);return e&&e.value}function g(t){if(!t.M){if(t.M=!0,0===t.t||1===t.t){var n=t.i=v(t.u);e(t.p,(function(t,r){n[t]=r})),t.p=void 0}t.h&&g(t.h)}}function A(t){t.i||(t.i=v(t.u))}function z(t,n,r){var e=a(n)?l("MapSet").K(n,r):c(n)?l("MapSet").N(n,r):t.S?function(t,n){var r=Array.isArray(t),e={t:r?1:0,A:n?n.A:_(),M:!1,R:!1,D:{},h:n,u:t,k:null,p:{},i:null,O:null,$:!1},i=e,u=H;r&&(i=[e],u=L);var o=Proxy.revocable(i,u),f=o.revoke,a=o.proxy;return e.k=a,e.O=f,a}(n,r):l("ES5").C(n,r);return(r?r.A:_()).p.push(e),e}function E(t,n){t.S?g(n):l("ES5").I(n)}function R(){function i(t,n){var r=t[X];if(r&&!r.W){r.W=!0;var e=t[n];return r.W=!1,e}return t[n]}function o(t){t.M||(t.M=!0,t.h&&o(t.h))}function a(t){t.i||(t.i=c(t.u))}function c(t){var n=t&&t[X];if(n){n.W=!0;var r=v(n.k,!0);return n.W=!1,r}return v(t)}function p(t){for(var n=t.length-1;n>=0;n--){var r=t[n][X];if(!r.M)switch(r.t){case 5:l(r)&&o(r);break;case 4:h(r)&&o(r)}}}function h(t){for(var n=t.u,r=t.k,e=Object.keys(r),i=e.length-1;i>=0;i--){var o=e[i],a=n[o];if(void 0===a&&!u(n,o))return!0;var c=r[o],s=c&&c[X];if(s?s.u!==a:!f(c,a))return!0}return e.length!==Object.keys(n).length}function l(t){var n=t.k;if(n.length!==t.u.length)return!0;var r=Object.getOwnPropertyDescriptor(n,n.length-1);return!(!r||r.get)}function y(n){n.j&&t(3,JSON.stringify(s(n)))}var b={};d("ES5",{C:function(t,n){var u=Array.isArray(t),v=c(t);e(v,(function(n){!function(t,n,e){var u=b[n];u?u.enumerable=e:b[n]=u={enumerable:e,get:function(){return function(t,n){y(t);var e=i(s(t),n);return t.W?e:e===i(t.u,n)&&r(e)?(a(t),t.i[n]=z(t.A.l,e,t)):e}(this[X],n)},set:function(t){!function(t,n,r){if(y(t),t.D[n]=!0,!t.M){if(f(r,i(s(t),n)))return;o(t),a(t)}t.i[n]=r}(this[X],n,t)}},Object.defineProperty(t,n,u)}(v,n,u||function(t,n){var r=Object.getOwnPropertyDescriptor(t,n);return!(!r||!r.enumerable)}(t,n))}));var p={t:u?5:4,A:n?n.A:_(),M:!1,W:!1,R:!1,D:{},h:n,u:t,k:v,i:null,j:!1,$:!1};return Object.defineProperty(v,X,{value:p,writable:!0}),v},I:o,P:function(t,r,i){t.p.forEach((function(t){t[X].W=!0})),i?n(r)&&r[X].A===t&&p(t.p):(t.o&&function t(n){if(n&&"object"==typeof n){var r=n[X];if(r){var i=r.u,f=r.k,a=r.D,c=r.t;if(4===c)e(f,(function(n){n!==X&&(void 0!==i[n]||u(i,n)?a[n]||t(f[n]):(a[n]=!0,o(r)))})),e(i,(function(t){void 0!==f[t]||u(f,t)||(a[t]=!1,o(r))}));else if(5===c){if(l(r)&&(o(r),a.length=!0),f.length<i.length)for(var s=f.length;s<i.length;s++)a[s]=!1;else for(var v=i.length;v<f.length;v++)a[v]=!0;for(var p=Math.min(f.length,i.length),h=0;h<p;h++)void 0===a[h]&&t(f[h])}}}}(t.p[0]),p(t.p))}})}function k(){function n(t){if(!t||"object"!=typeof t)return t;if(Array.isArray(t))return t.map(n);if(a(t))return new Map(Array.from(t.entries()).map((function(t){return[t[0],n(t[1])]})));if(c(t))return new Set(Array.from(t).map(n));var r=Object.create(Object.getPrototypeOf(t));for(var e in t)r[e]=n(t[e]);return r}var r="add";d("Patches",{X:function(e,u){return u.forEach((function(u){for(var f=u.path,a=u.op,c=e,s=0;s<f.length-1;s++)"object"!=typeof(c=o(c,f[s]))&&t(15,f.join("/"));var v=i(c),p=n(u.value),h=f[f.length-1];switch(a){case"replace":switch(v){case 2:return c.set(h,p);case 3:t(16);default:return c[h]=p}case r:switch(v){case 1:return c.splice(h,0,p);case 2:return c.set(h,p);case 3:return c.add(p);default:return c[h]=p}case"remove":switch(v){case 1:return c.splice(h,1);case 2:return c.delete(h);case 3:return c.delete(u.value);default:return delete c[h]}default:t(17,a)}})),e},F:function(t,n,i,f){switch(t.t){case 0:case 4:case 2:return function(t,n,i,f){var a=t.u,c=t.i;e(t.D,(function(t,e){var s=o(a,t),v=o(c,t),p=e?u(a,t)?"replace":r:"remove";if(s!==v||"replace"!==p){var h=n.concat(t);i.push("remove"===p?{op:p,path:h}:{op:p,path:h,value:v}),f.push(p===r?{op:"remove",path:h}:"remove"===p?{op:r,path:h,value:s}:{op:"replace",path:h,value:s})}}))}(t,n,i,f);case 5:case 1:return function(t,n,e,i){var u=t.u,o=t.D,f=t.i;if(f.length<u.length){var a=[f,u];u=a[0],f=a[1];var c=[i,e];e=c[0],i=c[1]}for(var s=f.length-u.length,v=0;u[v]===f[v]&&v<u.length;)++v;for(var p=u.length;p>v&&u[p-1]===f[p+s-1];)--p;for(var h=v;h<p;++h)if(o[h]&&f[h]!==u[h]){var l=n.concat([h]);e.push({op:"replace",path:l,value:f[h]}),i.push({op:"replace",path:l,value:u[h]})}for(var d=e.length,_=p+s-1;_>=p;--_){var y=n.concat([_]);e[d+_-p]={op:r,path:y,value:f[_]},i.push({op:"remove",path:y})}}(t,n,i,f);case 3:return function(t,n,e,i){var u=t.u,o=t.i,f=0;u.forEach((function(t){if(!o.has(t)){var u=n.concat([f]);e.push({op:"remove",path:u,value:t}),i.unshift({op:r,path:u,value:t})}f++})),f=0,o.forEach((function(t){if(!u.has(t)){var o=n.concat([f]);e.push({op:r,path:o,value:t}),i.unshift({op:"remove",path:o,value:t})}f++}))}(t,n,i,f)}},g:function(t,n,r,e){r.push({op:"replace",path:[],value:n}),e.push({op:"replace",path:[],value:t.u})}})}function F(){function n(t,n){function r(){this.constructor=t}o(t,n),t.prototype=(r.prototype=n.prototype,new r)}function e(t){t.i||(t.D=new Map,t.i=new Map(t.u))}function i(t){t.i||(t.i=new Set,t.u.forEach((function(n){if(r(n)){var e=z(t.A.l,n,t);t.p.set(n,e),t.i.add(e)}else t.i.add(n)})))}function u(n){n.j&&t(3,JSON.stringify(s(n)))}var o=function(t,n){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var r in n)n.hasOwnProperty(r)&&(t[r]=n[r])})(t,n)},f=function(){function t(t,n){return this[X]={t:2,h:n,A:n?n.A:_(),M:!1,R:!1,i:void 0,D:void 0,u:t,k:this,$:!1,j:!1},this}n(t,Map);var i=t.prototype;return Object.defineProperty(i,"size",{get:function(){return s(this[X]).size}}),i.has=function(t){return s(this[X]).has(t)},i.set=function(t,n){var r=this[X];return u(r),s(r).get(t)!==n&&(e(r),E(r.A.l,r),r.D.set(t,!0),r.i.set(t,n),r.D.set(t,!0)),this},i.delete=function(t){if(!this.has(t))return!1;var n=this[X];return u(n),e(n),E(n.A.l,n),n.D.set(t,!1),n.i.delete(t),!0},i.clear=function(){var t=this[X];return u(t),e(t),E(t.A.l,t),t.D=new Map,t.i.clear()},i.forEach=function(t,n){var r=this;s(this[X]).forEach((function(e,i){t.call(n,r.get(i),i,r)}))},i.get=function(t){var n=this[X];u(n);var i=s(n).get(t);if(n.R||!r(i))return i;if(i!==n.u.get(t))return i;var o=z(n.A.l,i,n);return e(n),n.i.set(t,o),o},i.keys=function(){return s(this[X]).keys()},i.values=function(){var t,n=this,r=this.keys();return(t={})[q]=function(){return n.values()},t.next=function(){var t=r.next();return t.done?t:{done:!1,value:n.get(t.value)}},t},i.entries=function(){var t,n=this,r=this.keys();return(t={})[q]=function(){return n.entries()},t.next=function(){var t=r.next();if(t.done)return t;var e=n.get(t.value);return{done:!1,value:[t.value,e]}},t},i[q]=function(){return this.entries()},t}(),a=function(){function t(t,n){return this[X]={t:3,h:n,A:n?n.A:_(),M:!1,R:!1,i:void 0,u:t,k:this,p:new Map,j:!1,$:!1},this}n(t,Set);var r=t.prototype;return Object.defineProperty(r,"size",{get:function(){return s(this[X]).size}}),r.has=function(t){var n=this[X];return u(n),n.i?!!n.i.has(t)||!(!n.p.has(t)||!n.i.has(n.p.get(t))):n.u.has(t)},r.add=function(t){var n=this[X];return u(n),n.i?n.i.add(t):n.u.has(t)||(i(n),E(n.A.l,n),n.i.add(t)),this},r.delete=function(t){if(!this.has(t))return!1;var n=this[X];return u(n),i(n),E(n.A.l,n),n.i.delete(t)||!!n.p.has(t)&&n.i.delete(n.p.get(t))},r.clear=function(){var t=this[X];return u(t),i(t),E(t.A.l,t),t.i.clear()},r.values=function(){var t=this[X];return u(t),i(t),t.i.values()},r.entries=function(){var t=this[X];return u(t),i(t),t.i.entries()},r.keys=function(){return this.values()},r[q]=function(){return this.values()},r.forEach=function(t,n){for(var r=this.values(),e=r.next();!e.done;)t.call(n,e.value,e.value,this),e=r.next()},t}();d("MapSet",{K:function(t,n){return new f(t,n)},N:function(t,n){return new a(t,n)}})}var D;Object.defineProperty(exports,"__esModule",{value:!0});var J,K="undefined"!=typeof Symbol,N="undefined"!=typeof Map,$="undefined"!=typeof Set,C="undefined"!=typeof Proxy&&void 0!==Proxy.revocable&&"undefined"!=typeof Reflect,I=K?Symbol("immer-nothing"):((D={})["immer-nothing"]=!0,D),W=K?Symbol("immer-draftable"):"__$immer_draftable",X=K?Symbol("immer-state"):"__$immer_state",q=K?Symbol.iterator:"@@iterator",B="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?function(t){return Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t))}:Object.getOwnPropertyNames,G={},H={get:function(t,n){if(n===X)return t;var e=t.p;if(!t.M&&u(e,n))return e[n];var i=s(t)[n];if(t.R||!r(i))return i;if(t.M){if(i!==M(t.u,n))return i;e=t.i}return e[n]=z(t.A.l,i,t)},has:function(t,n){return n in s(t)},ownKeys:function(t){return Reflect.ownKeys(s(t))},set:function(t,n,r){if(!t.M){var e=M(t.u,n);if(r?f(e,r)||r===t.p[n]:f(e,r)&&n in t.u)return!0;A(t),g(t)}return t.D[n]=!0,t.i[n]=r,!0},deleteProperty:function(t,n){return void 0!==M(t.u,n)||n in t.u?(t.D[n]=!1,A(t),g(t)):t.D[n]&&delete t.D[n],t.i&&delete t.i[n],!0},getOwnPropertyDescriptor:function(t,n){var r=s(t),e=Reflect.getOwnPropertyDescriptor(r,n);return e&&(e.writable=!0,e.configurable=1!==t.t||"length"!==n),e},defineProperty:function(){t(11)},getPrototypeOf:function(t){return Object.getPrototypeOf(t.u)},setPrototypeOf:function(){t(12)}},L={};e(H,(function(t,n){L[t]=function(){return arguments[0]=arguments[0][0],n.apply(this,arguments)}})),L.deleteProperty=function(t,n){return H.deleteProperty.call(this,t[0],n)},L.set=function(t,n,r){return H.set.call(this,t[0],n,r,t[0])};var Q=function(){function e(t){this.S=C,this.J=!1,"boolean"==typeof(null==t?void 0:t.useProxies)&&this.setUseProxies(t.useProxies),"boolean"==typeof(null==t?void 0:t.autoFreeze)&&this.setAutoFreeze(t.autoFreeze),this.produce=this.produce.bind(this),this.produceWithPatches=this.produceWithPatches.bind(this)}var i=e.prototype;return i.produce=function(n,e,i){if("function"==typeof n&&"function"!=typeof e){var u=e;e=n;var o=this;return function(t){var n=this;void 0===t&&(t=u);for(var r=arguments.length,i=Array(r>1?r-1:0),f=1;f<r;f++)i[f-1]=arguments[f];return o.produce(t,(function(t){var r;return(r=e).call.apply(r,[n,t].concat(i))}))}}var f;if("function"!=typeof e&&t(6),void 0!==i&&"function"!=typeof i&&t(7),r(n)){var a=O(this),c=z(this,n,void 0),s=!0;try{f=e(c),s=!1}finally{s?b(a):m(a)}return"undefined"!=typeof Promise&&f instanceof Promise?f.then((function(t){return y(a,i),x(t,a)}),(function(t){throw b(a),t})):(y(a,i),x(f,a))}if((f=e(n))!==I)return void 0===f&&(f=n),this.J&&p(f,!0),f},i.produceWithPatches=function(t,n){var r,e,i=this;return"function"==typeof t?function(n){for(var r=arguments.length,e=Array(r>1?r-1:0),u=1;u<r;u++)e[u-1]=arguments[u];return i.produceWithPatches(n,(function(n){return t.apply(void 0,[n].concat(e))}))}:[this.produce(t,n,(function(t,n){r=t,e=n})),r,e]},i.createDraft=function(n){r(n)||t(8);var e=O(this),i=z(this,n,void 0);return i[X].$=!0,m(e),i},i.finishDraft=function(t,n){var r=(t&&t[X]).A;return y(r,n),x(void 0,r)},i.setAutoFreeze=function(t){this.J=t},i.setUseProxies=function(n){C||t(20),this.S=n},i.applyPatches=function(t,r){var e;for(e=r.length-1;e>=0;e--){var i=r[e];if(0===i.path.length&&"replace"===i.op){t=i.value;break}}var u=l("Patches").X;return n(t)?u(t,r):this.produce(t,(function(t){return u(t,r.slice(e+1))}))},e}(),T=new Q,U=T.produce,V=T.produceWithPatches.bind(T),Y=T.setAutoFreeze.bind(T),Z=T.setUseProxies.bind(T),tt=T.applyPatches.bind(T),nt=T.createDraft.bind(T),rt=T.finishDraft.bind(T);exports.Immer=Q,exports.applyPatches=tt,exports.castDraft=function(t){return t},exports.castImmutable=function(t){return t},exports.createDraft=nt,exports.default=U,exports.enableAllPlugins=function(){R(),F(),k()},exports.enableES5=R,exports.enableMapSet=F,exports.enablePatches=k,exports.finishDraft=rt,exports.immerable=W,exports.isDraft=n,exports.isDraftable=r,exports.nothing=I,exports.original=function(t){if(t&&t[X])return t[X].u},exports.produce=U,exports.produceWithPatches=V,exports.setAutoFreeze=Y,exports.setUseProxies=Z;
//# sourceMappingURL=immer.cjs.production.min.js.map