UNPKG

immer

Version:

Create your next immutable state by mutating the current one

2 lines 16.1 kB
var Y=Symbol.for("immer-nothing"),W=Symbol.for("immer-draftable"),l=Symbol.for("immer-state");function D(e,...t){throw new Error(`[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`)}var R=Object,L=R.getPrototypeOf,ne="constructor",oe="prototype",xe="configurable",le="enumerable",fe="writable",ae="value",F=e=>!!e&&!!e[l];function E(e){return e?Ue(e)||J(e)||!!e[W]||!!e[ne]?.[W]||Q(e)||X(e):!1}var Je=R[oe][ne].toString(),ke=new WeakMap;function Ue(e){if(!e||!$(e))return!1;let t=L(e);if(t===null||t===R[oe])return!0;let r=R.hasOwnProperty.call(t,ne)&&t[ne];if(r===Object)return!0;if(!V(r))return!1;let n=ke.get(r);return n===void 0&&(n=Function.toString.call(r),ke.set(r,n)),n===Je}function Qe(e){return F(e)||D(15,e),e[l].t}function U(e,t,r=!0){G(e)===0?(r?Reflect.ownKeys(e):R.keys(e)).forEach(i=>{t(i,e[i],e)}):e.forEach((n,i)=>t(i,n,e))}function G(e){let t=e[l];return t?t.r:J(e)?1:Q(e)?2:X(e)?3:0}var v=(e,t,r=G(e))=>r===2?e.has(t):R[oe].hasOwnProperty.call(e,t),k=(e,t,r=G(e))=>r===2?e.get(t):e[t],ie=(e,t,r,n=G(e))=>{n===2?e.set(t,r):n===3?e.add(r):e[t]=r};function je(e,t){return e===t?e!==0||1/e===1/t:e!==e&&t!==t}var J=Array.isArray,Q=e=>e instanceof Map,X=e=>e instanceof Set,$=e=>typeof e=="object",V=e=>typeof e=="function",Ae=e=>typeof e=="boolean";function Le(e){let t=+e;return Number.isInteger(t)&&String(t)===e}var Ee=e=>$(e)?e?.[l]:null,M=e=>e.e||e.t,Ve=e=>{let t=Ee(e);return t?de(t):e},de=e=>e.c?e.e:e.t;function pe(e,t){if(Q(e))return new Map(e);if(X(e))return new Set(e);if(J(e))return Array[oe].slice.call(e);let r=Ue(e);if(t===!0||t==="class_only"&&!r){let n=R.getOwnPropertyDescriptors(e);delete n[l];let i=Reflect.ownKeys(n);for(let s=0;s<i.length;s++){let S=i[s],d=n[S];d[fe]===!1&&(d[fe]=!0,d[xe]=!0),(d.get||d.set)&&(n[S]={[xe]:!0,[fe]:!0,[le]:d[le],[ae]:e[S]})}return R.create(L(e),n)}else{let n=L(e);if(n!==null&&r)return{...e};let i=R.create(n);return R.assign(i,e)}}function se(e,t=!1){return ce(e)||F(e)||!E(e)||(G(e)>1&&R.defineProperties(e,{set:ge,add:ge,clear:ge,delete:ge}),R.freeze(e),t&&U(e,(r,n)=>{se(n,!0)},!1)),e}function Xe(){D(2)}var ge={[ae]:Xe};function ce(e){return e===null||!$(e)?!0:R.isFrozen(e)}var q="MapSet",Z="Patches",he="ArrayMethods",Ie={};function B(e){let t=Ie[e];return t||D(0,e),t}var we=e=>!!Ie[e];function ue(e,t){Ie[e]||(Ie[e]=t)}var me,ee=()=>me,Ze=(e,t)=>({o:[],i:e,l:t,F:!0,m:0,P:new Set,I:new Set,b:we(q)?B(q):void 0,C:we(he)?B(he):void 0});function Ce(e,t){t&&(e.x=B(Z),e.y=[],e.d=[],e.E=t)}function Se(e){Te(e),e.o.forEach(et),e.o=null}function Te(e){e===me&&(me=e.i)}var Fe=e=>me=Ze(me,e);function et(e){let t=e[l];t.r===0||t.r===1?t.M():t.g=!0}function Ne(e,t){t.m=t.o.length;let r=t.o[0];if(e!==void 0&&e!==r){r[l].c&&(Se(t),D(4)),E(e)&&(e=Be(t,e));let{x:i}=t;i&&i.D(r[l].t,e,t)}else e=Be(t,r);return tt(t,e,!0),Se(t),t.y&&t.E(t.y,t.d),e!==Y?e:void 0}function Be(e,t){if(ce(t))return t;let r=t[l];if(!r)return Me(t,e.P,e);if(!be(r,e))return t;if(!r.c)return r.t;if(!r.u){let{f:n}=r;if(n)for(;n.length>0;)n.pop()(e);Ge(r,e)}return r.e}function tt(e,t,r=!1){!e.i&&e.l.h&&e.F&&se(t,r)}function Ke(e){e.u=!0,e.a.m--}var be=(e,t)=>e.a===t,rt=[];function He(e,t,r,n){let i=M(e),s=e.r;if(n!==void 0&&k(i,n,s)===t){ie(i,n,r,s);return}if(!e.R){let d=e.R=new Map;U(i,(C,y)=>{if(F(y)){let O=d.get(y)||[];O.push(C),d.set(y,O)}})}let S=e.R.get(t)??rt;for(let d of S)ie(i,d,r,s)}function We(e,t,r){e.f.push(function(i){let s=t;if(!s||!be(s,i))return;i.b?.fixSetContents(s);let S=de(s);He(e,s.s??s,S,r),Ge(s,i)})}function Ge(e,t){if(e.c&&!e.u&&(e.r===3||e.r===1&&e.T||(e.n?.size??0)>0)){let{x:n}=t;if(n){let i=n.getPath(e);i&&n.O(e,i,t)}Ke(e)}}function te(e,t,r){let{a:n}=e;if(F(r)){let i=r[l];be(i,n)&&i.f.push(function(){K(e);let S=de(i);He(e,r,S,t)})}else E(r)&&e.f.push(function(){let s=M(e);e.r===3?s.has(r)&&Me(r,n.P,n):k(s,t,e.r)===r&&n.o.length>1&&(e.n.get(t)??!1)===!0&&e.e&&Me(k(e.e,t,e.r),n.P,n)})}function Me(e,t,r){return!r.l.h&&r.m<1||F(e)||t.has(e)||!E(e)||ce(e)||(t.add(e),U(e,(n,i)=>{if(F(i)){let s=i[l];if(be(s,r)){let S=de(s);ie(e,n,S,e.r),Ke(s)}}else E(i)&&Me(i,t,r)})),e}function ve(e,t){let r=J(e),n={r:r?1:0,a:t?t.a:ee(),c:!1,u:!1,n:void 0,i:t,t:e,s:null,e:null,M:null,S:!1,f:void 0},i=n,s=De;r&&(i=[n],s=Pe);let{revoke:S,proxy:d}=Proxy.revocable(i,s);return n.s=d,n.M=S,[d,n]}var De={get(e,t){if(t===l)return e;let r=e.a.C,n=e.r===1&&typeof t=="string";if(n&&r?.isArrayOperationMethod(t))return r.createMethodInterceptor(e,t);let i=M(e);if(!v(i,t,e.r))return ot(e,i,t);let s=i[t];if(e.u||!E(s)||n&&e.operationMethod&&r?.isMutatingArrayMethod(e.operationMethod)&&Le(t))return s;if(s===Re(e.t,t)||nt(e,t,s)){K(e);let S=e.r===1?+t:t,d=re(e.a,s,e,S);return e.e[S]=d}return s},has(e,t){return t in M(e)},ownKeys(e){return Reflect.ownKeys(M(e))},set(e,t,r){let n=$e(M(e),t);if(n?.set)return n.set.call(e.s,r),!0;if(!e.c){let i=Re(M(e),t),s=i?.[l];if(s&&s.t===r)return e.e[t]=r,e.n.set(t,!1),!0;if(je(r,i)&&(r!==void 0||v(e.t,t,e.r)))return!0;K(e),z(e)}return e.e[t]===r&&(r!==void 0||v(e.e,t,e.r))||Number.isNaN(r)&&Number.isNaN(e.e[t])||(e.e[t]=r,e.n.set(t,!0),te(e,t,r)),!0},deleteProperty(e,t){return K(e),Re(e.t,t)!==void 0||t in e.t?(e.n.set(t,!1),z(e)):e.n.delete(t),e.e&&delete e.e[t],!0},getOwnPropertyDescriptor(e,t){let r=M(e),n=Reflect.getOwnPropertyDescriptor(r,t);return n&&{[fe]:!0,[xe]:e.r!==1||t!=="length",[le]:n[le],[ae]:r[t]}},defineProperty(){D(11)},getPrototypeOf(e){return L(e.t)},setPrototypeOf(){D(12)}},Pe={};for(let e in De){let t=De[e];Pe[e]=function(){let r=arguments;return r[0]=r[0][0],t.apply(this,r)}}Pe.deleteProperty=function(e,t){return Pe.set.call(this,e,t,void 0)};Pe.set=function(e,t,r){return De.set.call(this,e[0],t,r,e[0])};function Re(e,t){let r=e[l];return(r?M(r):e)[t]}function nt(e,t,r){return e.r!==1||!e.T||e.n?.get(t)||!E(r)||r[l]?!1:e.N.has(r)}function ot(e,t,r){let n=$e(t,r);return n?ae in n?n[ae]:n.get?.call(e.s):void 0}function $e(e,t){if(!(t in e))return;let r=L(e);for(;r;){let n=Object.getOwnPropertyDescriptor(r,t);if(n)return n;r=L(r)}}function z(e){e.c||(e.c=!0,e.i&&z(e.i))}function K(e){e.e||(e.n=new Map,e.e=pe(e.t,e.a.l.A))}var Oe=class{constructor(t){this.h=!0;this.A=!1;this._=!1;this.produce=(t,r,n)=>{if(V(t)&&!V(r)){let s=r;r=t;let S=this;return function(C=s,...y){return S.produce(C,O=>r.call(this,O,...y))}}V(r)||D(6),n!==void 0&&!V(n)&&D(7);let i;if(E(t)){let s=Fe(this),S=re(s,t,void 0),d=!0;try{i=r(S),d=!1}finally{d?Se(s):Te(s)}return Ce(s,n),Ne(i,s)}else if(!t||!$(t)){if(i=r(t),i===void 0&&(i=t),i===Y&&(i=void 0),this.h&&se(i,!0),n){let s=[],S=[];B(Z).D(t,i,{y:s,d:S}),n(s,S)}return i}else D(1,t)};this.produceWithPatches=(t,r)=>{if(V(t))return(S,...d)=>this.produceWithPatches(S,C=>t(C,...d));let n,i;return[this.produce(t,r,(S,d)=>{n=S,i=d}),n,i]};Ae(t?.autoFreeze)&&this.setAutoFreeze(t.autoFreeze),Ae(t?.useStrictShallowCopy)&&this.setUseStrictShallowCopy(t.useStrictShallowCopy),Ae(t?.useStrictIteration)&&this.setUseStrictIteration(t.useStrictIteration)}createDraft(t){E(t)||D(8),F(t)&&(t=ze(t));let r=Fe(this),n=re(r,t,void 0);return n[l].S=!0,Te(r),n}finishDraft(t,r){let n=t&&t[l];(!n||!n.S)&&D(9);let{a:i}=n;return Ce(i,r),Ne(void 0,i)}setAutoFreeze(t){this.h=t}setUseStrictShallowCopy(t){this.A=t}setUseStrictIteration(t){this._=t}shouldUseStrictIteration(){return this._}applyPatches(t,r){let n;for(n=r.length-1;n>=0;n--){let s=r[n];if(s.path.length===0&&s.op==="replace"){t=s.value;break}}n>-1&&(r=r.slice(n+1));let i=B(Z).w;return F(t)?i(t,r):this.produce(t,s=>i(s,r))}};function re(e,t,r,n){let[i,s]=Q(t)?B(q).V(t,r):X(t)?B(q).k(t,r):ve(t,r);return(r?.a??ee()).o.push(i),s.f=r?.f??[],s.p=n,r&&n!==void 0?We(r,s,n):s.f.push(function(C){C.b?.fixSetContents(s);let{x:y}=C;s.c&&y&&y.O(s,[],C)}),i}function ze(e){return F(e)||D(10,e),qe(e)}function qe(e){if(!E(e)||ce(e))return e;let t=e[l],r,n=!0;if(t){if(!t.c)return t.t;t.u=!0,r=pe(e,t.a.l.A),n=t.a.l.shouldUseStrictIteration()}else r=pe(e,!0);return U(r,(i,s)=>{ie(r,i,qe(s))},n),t&&(t.u=!1),r}function at(){function t(u,h=[]){if(u.p!==void 0){let m=u.i.e??u.i.t,A=Ee(k(m,u.p)),I=k(m,u.p);if(I===void 0||I!==u.s&&I!==u.t&&I!==u.e||A!=null&&A.t!==u.t)return null;let T=u.i.r===3,b;if(T){let o=u.i;b=Array.from(o.o.keys()).indexOf(u.p)}else b=u.p;if(!(T&&m.size>b||v(m,b)))return null;h.push(b)}if(u.i)return t(u.i,h);h.reverse();try{r(u.e,h)}catch{return null}return h}function r(u,h){let m=u;for(let A=0;A<h.length-1;A++){let I=h[A];if(m=k(m,I),!$(m)||m===null)throw new Error(`Cannot resolve path at '${h.join("/")}'`)}return m}let n="replace",i="add",s="remove";function S(u,h,m){if(u.a.I.has(u))return;u.a.I.add(u);let{y:A,d:I}=m;switch(u.r){case 0:case 2:return C(u,h,A,I);case 1:return d(u,h,A,I);case 3:return y(u,h,A,I)}}function d(u,h,m,A){let{t:I,n:T}=u,b=u.e;b.length<I.length&&([I,b]=[b,I],[m,A]=[A,m]);let o=u.T===!0;for(let f=0;f<I.length;f++){let x=b[f],p=I[f];if((o||T?.get(f.toString()))&&x!==p){let _=x?.[l];if(_&&_.c)continue;let w=h.concat([f]);m.push({op:n,path:w,value:g(x)}),A.push({op:n,path:w,value:g(p)})}}for(let f=I.length;f<b.length;f++){let x=h.concat([f]);m.push({op:i,path:x,value:g(b[f])})}for(let f=b.length-1;I.length<=f;--f){let x=h.concat([f]);A.push({op:s,path:x})}}function C(u,h,m,A){let{t:I,e:T,r:b}=u;U(u.n,(o,f)=>{let x=k(I,o,b),p=k(T,o,b),P=f?v(I,o)?n:i:s;if(x===p&&P===n)return;let _=h.concat(o);m.push(P===s?{op:P,path:_}:{op:P,path:_,value:g(p)}),A.push(P===i?{op:s,path:_}:P===s?{op:i,path:_,value:g(x)}:{op:n,path:_,value:g(x)})})}function y(u,h,m,A){let{t:I,e:T}=u,b=0;I.forEach(o=>{if(!T.has(o)){let f=h.concat([b]);m.push({op:s,path:f,value:o}),A.unshift({op:i,path:f,value:o})}b++}),b=0,T.forEach(o=>{if(!I.has(o)){let f=h.concat([b]);m.push({op:i,path:f,value:o}),A.unshift({op:s,path:f,value:o})}b++})}function O(u,h,m){let{y:A,d:I}=m;A.push({op:n,path:[],value:h===Y?void 0:h}),I.push({op:n,path:[],value:u})}function a(u,h){return h.forEach(m=>{let{path:A,op:I}=m,T=u;for(let x=0;x<A.length-1;x++){let p=G(T),P=A[x];typeof P!="string"&&typeof P!="number"&&(P=""+P),(p===0||p===1)&&(P==="__proto__"||P===ne)&&D(16+3),V(T)&&P===oe&&D(16+3),T=k(T,P),(T===null||!$(T))&&D(16+2,A.join("/"))}let b=G(T),o=c(m.value),f=A[A.length-1];switch(I){case n:switch(b){case 2:return T.set(f,o);case 3:D(16);default:return T[f]=o}case i:switch(b){case 1:return f==="-"?T.push(o):T.splice(f,0,o);case 2:return T.set(f,o);case 3:return T.add(o);default:return T[f]=o}case s:switch(b){case 1:return T.splice(f,1);case 2:return T.delete(f);case 3:return T.delete(m.value);default:return delete T[f]}default:D(16+1,I)}}),u}function c(u){if(!E(u))return u;if(J(u))return u.map(c);if(Q(u))return new Map(Array.from(u.entries()).map(([m,A])=>[m,c(A)]));if(X(u))return new Set(Array.from(u).map(c));let h=Object.create(L(u));for(let m in u)h[m]=c(u[m]);return v(u,W)&&(h[W]=u[W]),h}function g(u){return F(u)?c(u):u}ue(Z,{w:a,O:S,D:O,getPath:t})}var Ye=globalThis.Iterator,it=typeof Ye?.from=="function";function st(){class e extends Map{constructor(a,c){super();this[l]={r:2,i:c,a:c?c.a:ee(),c:!1,u:!1,e:void 0,n:void 0,t:a,s:this,S:!1,g:!1,f:[]}}get size(){return M(this[l]).size}has(a){return M(this[l]).has(a)}set(a,c){let g=this[l];return d(g),(!M(g).has(a)||M(g).get(a)!==c)&&(n(g),z(g),g.n.set(a,!0),g.e.set(a,c),g.n.set(a,!0),te(g,a,c)),this}delete(a){if(!this.has(a))return!1;let c=this[l];return d(c),n(c),z(c),c.t.has(a)?c.n.set(a,!1):c.n.delete(a),c.e.delete(a),!0}clear(){let a=this[l];d(a),M(a).size&&(n(a),z(a),a.n=new Map,U(a.t,c=>{a.n.set(c,!1)}),a.e.clear())}forEach(a,c){let g=this[l];M(g).forEach((u,h,m)=>{a.call(c,this.get(h),h,this)})}get(a){let c=this[l];d(c);let g=M(c).get(a);if(c.u||!E(g)||g!==c.t.get(a))return g;let u=re(c.a,g,c,a);return n(c),c.e.set(a,u),u}keys(){return M(this[l]).keys()}values(){let a=this.keys();return t({next:()=>{let c=a.next();return c.done?c:{done:!1,value:this.get(c.value)}}})}entries(){let a=this.keys();return t({next:()=>{let c=a.next();if(c.done)return c;let g=this.get(c.value);return{done:!1,value:[c.value,g]}}})}[(l,Symbol.iterator)](){return this.entries()}}function t(y){if(it)return Ye.from(y);let O={...y,[Symbol.iterator]:()=>O};return O}function r(y,O){let a=new e(y,O);return[a,a[l]]}function n(y){y.e||(y.n=new Map,y.e=new Map(y.t))}class i extends Set{constructor(a,c){super();this[l]={r:3,i:c,a:c?c.a:ee(),c:!1,u:!1,e:void 0,t:a,s:this,o:new Map,g:!1,S:!1,n:void 0,f:[]}}get size(){return M(this[l]).size}has(a){let c=this[l];return d(c),c.e?!!(c.e.has(a)||c.o.has(a)&&c.e.has(c.o.get(a))):c.t.has(a)}add(a){let c=this[l];return d(c),this.has(a)||(S(c),z(c),c.e.add(a),te(c,a,a)),this}delete(a){if(!this.has(a))return!1;let c=this[l];return d(c),S(c),z(c),c.e.delete(a)||(c.o.has(a)?c.e.delete(c.o.get(a)):!1)}clear(){let a=this[l];d(a),M(a).size&&(S(a),z(a),a.e.clear())}values(){let a=this[l];return d(a),S(a),a.e.values()}entries(){let a=this[l];return d(a),S(a),a.e.entries()}keys(){return this.values()}[(l,Symbol.iterator)](){return this.values()}forEach(a,c){let g=this.values(),u=g.next();for(;!u.done;)a.call(c,u.value,u.value,this),u=g.next()}}function s(y,O){let a=new i(y,O);return[a,a[l]]}function S(y){y.e||(y.e=new Set,y.t.forEach(O=>{if(E(O)){let a=re(y.a,O,y,O);y.o.set(O,a),y.e.add(a)}else y.e.add(O)}))}function d(y){y.g&&D(3,JSON.stringify(M(y)))}function C(y){if(y.r===3&&y.e){let O=new Set(y.e);y.e.clear(),O.forEach(a=>{y.e.add(Ve(a))})}}ue(q,{V:r,k:s,fixSetContents:C})}function ct(){let e=new Set(["shift","unshift"]),t=new Set(["push","pop"]),r=new Set([...t,...e]),n=new Set(["reverse","sort"]),i=new Set([...r,...n,"splice"]),s=new Set(["find","findLast"]),S=new Set(["filter","slice","concat","flat",...s,"findIndex","findLastIndex","some","every","indexOf","lastIndexOf","includes","join","toString","toLocaleString"]);function d(o){return i.has(o)}function C(o){return S.has(o)}function y(o){return d(o)||C(o)}function O(o,f){o.operationMethod=f}function a(o){o.operationMethod=void 0}function c(o,f,x=!0){K(o);let p=f();return g(o,x),p}function g(o,f=!0){z(o),f&&o.n.set("length",!0)}function u(o){o.T=!0,o.N=new Set(o.t)}function h(o,f){return o<0?Math.max(f+o,0):Math.min(o,f)}function m(o,f,x){for(let p=0;p<x.length;p++){let P=""+(f+p);o.n.set(P,!0),te(o,P,x[p])}}function A(o,f,x){let p=f==="push"||f==="unshift";if(p?x.length===0:M(o).length===0)return p?M(o).length:void 0;K(o);let P=o.e.length,_=o.e[f](...x);return g(o),e.has(f)&&u(o),f==="push"?m(o,P,x):f==="unshift"&&m(o,0,x),r.has(f)?_:o.s}function I(o,f,x){return M(o).length<=1?o.s:c(o,()=>(o.e[f](...x),u(o),o.s),!1)}function T(o,f){return function(...p){let P=f;O(o,P);try{if(d(P)){if(r.has(P))return A(o,P,p);if(n.has(P))return I(o,P,p);if(P==="splice"){let _=p.length>2?p.length-2:0;if(_===0){let H=M(o).length;if(p.length===0||h(p[0]??0,H)===H||p.length>1&&!(p[1]>=1))return[]}K(o);let w=o.e.splice(...p);if(w.length===0&&_===0)return w;if(g(o),u(o),_>0){let H=h(p[0]??0,o.e.length);m(o,H,p.slice(2))}return w}}else return b(o,P,p)}finally{a(o)}}}function b(o,f,x){let p=M(o);if(f==="filter"){let P=x[0],_=[];for(let w=0;w<p.length;w++)P(p[w],w,p)&&_.push(o.s[w]);return _}if(s.has(f)){let P=x[0],_=f==="find",w=_?1:-1,H=_?0:p.length-1;for(let j=H;j>=0&&j<p.length;j+=w)if(P(p[j],j,p))return o.s[j];return}if(f==="slice"){let P=x[0]??0,_=x[1]??p.length,w=h(P,p.length),H=h(_,p.length),j=[];for(let _e=w;_e<H;_e++)j.push(o.s[_e]);return j}return p[f](...x)}ue(he,{createMethodInterceptor:T,isArrayOperationMethod:y,isMutatingArrayMethod:d})}var N=new Oe,Qr=N.produce,Xr=N.produceWithPatches.bind(N),Zr=N.setAutoFreeze.bind(N),en=N.setUseStrictShallowCopy.bind(N),tn=N.setUseStrictIteration.bind(N),rn=N.applyPatches.bind(N),nn=N.createDraft.bind(N),on=N.finishDraft.bind(N),an=e=>e,sn=e=>e;export{Oe as Immer,rn as applyPatches,an as castDraft,sn as castImmutable,nn as createDraft,ze as current,ct as enableArrayMethods,st as enableMapSet,at as enablePatches,on as finishDraft,se as freeze,W as immerable,F as isDraft,E as isDraftable,Y as nothing,Qe as original,Qr as produce,Xr as produceWithPatches,Zr as setAutoFreeze,tn as setUseStrictIteration,en as setUseStrictShallowCopy}; //# sourceMappingURL=immer.production.mjs.map