mutative
Version:
A JavaScript library for efficient immutable updates
3 lines (2 loc) • 20.4 kB
JavaScript
;const e={Remove:"remove",Replace:"replace",Add:"add"},t=Symbol.for("__MUTATIVE_PROXY_DRAFT__"),r=Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__"),n=Symbol.iterator,o={mutable:"mutable",immutable:"immutable"},a={};function i(e,t){return e instanceof Map?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function s(e,t){if(t in e){let r=Reflect.getPrototypeOf(e);for(;r;){const e=Reflect.getOwnPropertyDescriptor(r,t);if(e)return e;r=Reflect.getPrototypeOf(r)}}}function c(e){return Object.getPrototypeOf(e)===Set.prototype}function l(e){return Object.getPrototypeOf(e)===Map.prototype}function p(e){var t;return null!==(t=e.copy)&&void 0!==t?t:e.original}function u(e){return!!f(e)}function f(e){return"object"!=typeof e?null:null==e?void 0:e[t]}function d(e){var t;const r=f(e);return r?null!==(t=r.copy)&&void 0!==t?t:r.original:e}function y(e,t){if(!e||"object"!=typeof e)return!1;let r;return Object.getPrototypeOf(e)===Object.prototype||Array.isArray(e)||e instanceof Map||e instanceof Set||!!(null==t?void 0:t.mark)&&((r=t.mark(e,o))===o.immutable||"function"==typeof r)}function h(e,t=[]){if(Object.hasOwnProperty.call(e,"key")){const r=e.parent.copy,n=f(v(r,e.key));if(null!==n&&(null==n?void 0:n.original)!==e.original)return null;const o=3===e.parent.type,a=o?Array.from(e.parent.setMap.keys()).indexOf(e.key):e.key;if(!(o&&r.size>a||i(r,a)))return null;t.push(a)}if(e.parent)return h(e.parent,t);t.reverse();try{!function(e,t){for(let r=0;r<t.length-1;r+=1){const n=t[r];if("object"!=typeof(e=v(3===g(e)?Array.from(e):e,n)))throw new Error(`Cannot resolve patch at '${t.join("/")}'.`)}}(e.copy,t)}catch(e){return null}return t}function g(e){return Array.isArray(e)?1:e instanceof Map?2:e instanceof Set?3:0}function v(e,t){return 2===g(e)?e.get(t):e[t]}function b(e,t,r){2===g(e)?e.set(t,r):e[t]=r}function w(e,t){const r=f(e);return(r?p(r):e)[t]}function m(e,t){return e===t?0!==e||1/e==1/t:e!=e&&t!=t}function O(e){if(e)for(;e.finalities.revoke.length>0;){e.finalities.revoke.pop()()}}function M(e,t){return t?e:[""].concat(e).map((e=>{const t=`${e}`;return-1===t.indexOf("/")&&-1===t.indexOf("~")?t:t.replace(/~/g,"~0").replace(/\//g,"~1")})).join("/")}const k=Object.prototype.propertyIsEnumerable;function j(e,t){let r;if(Array.isArray(e))return Array.prototype.concat.call(e);if(e instanceof Set){if(!c(e)){return new(0,Object.getPrototypeOf(e).constructor)(e.values())}return Set.prototype.difference?Set.prototype.difference.call(e,new Set):new Set(e.values())}if(e instanceof Map){if(!l(e)){return new(0,Object.getPrototypeOf(e).constructor)(e)}return new Map(e)}if((null==t?void 0:t.mark)&&(r=t.mark(e,o),void 0!==r)&&r!==o.mutable){if(r===o.immutable)return function(e){const t=Object.create(Object.getPrototypeOf(e));return Reflect.ownKeys(e).forEach((r=>{let n=Reflect.getOwnPropertyDescriptor(e,r);n.enumerable&&n.configurable&&n.writable?t[r]=e[r]:(n.writable||(n.writable=!0,n.configurable=!0),(n.get||n.set)&&(n={configurable:!0,writable:!0,enumerable:n.enumerable,value:e[r]}),Reflect.defineProperty(t,r,n))})),t}(e);if("function"==typeof r)return r();throw new Error(`Unsupported mark result: ${r}`)}if("object"==typeof e&&Object.getPrototypeOf(e)===Object.prototype){const t={};return Object.keys(e).forEach((r=>{t[r]=e[r]})),Object.getOwnPropertySymbols(e).forEach((r=>{k.call(e,r)&&(t[r]=e[r])})),t}throw new Error("Please check mark() to ensure that it is a stable marker draftable function.")}function S(e){e.copy||(e.copy=j(e.original,e.options))}function R(e){if(!y(e))return d(e);if(Array.isArray(e))return e.map(R);if(e instanceof Map){const t=Array.from(e.entries()).map((([e,t])=>[e,R(t)]));if(!l(e)){return new(0,Object.getPrototypeOf(e).constructor)(t)}return new Map(t)}if(e instanceof Set){const t=Array.from(e).map(R);if(!c(e)){return new(0,Object.getPrototypeOf(e).constructor)(t)}return new Set(t)}const t=Object.create(Object.getPrototypeOf(e));for(const r in e)t[r]=R(e[r]);return t}function P(e){return u(e)?R(e):e}function E(e){var t;e.assignedMap=null!==(t=e.assignedMap)&&void 0!==t?t:new Map,e.operated||(e.operated=!0,e.parent&&E(e.parent))}function A(){throw new Error("Cannot modify frozen object")}function x(e){return e&&"object"==typeof e&&!Object.isFrozen(e)}function z(e,t,r,n,o){if(Object.isFrozen(e)||u(e))return;switch(g(e)){case 2:for(const[t,r]of e)x(t)&&z(t),x(r)&&z(r);e.set=e.clear=e.delete=A;break;case 3:for(const t of e)x(t)&&z(t);e.add=e.clear=e.delete=A;break;case 1:Object.freeze(e);for(const t of e)x(t)&&z(t);break;default:Object.freeze(e),Object.keys(e).forEach((t=>{const r=e[t];x(r)&&z(r)}))}}function D(e,t){const r=g(e);if(0===r)Reflect.ownKeys(e).forEach((r=>{t(r,e[r],e)}));else if(1===r){let r=0;for(const n of e)t(r,n,e),r+=1}else e.forEach(((r,n)=>t(n,r,e)))}function _(e,t,r){if(u(e)||!y(e,r)||t.has(e)||Object.isFrozen(e))return;const n=e instanceof Set,o=n?new Map:void 0;if(t.add(e),D(e,((a,i)=>{var s;if(u(i)){const t=f(i);S(t);const r=(null===(s=t.assignedMap)||void 0===s?void 0:s.size)||t.operated?t.copy:t.original;b(n?o:e,a,r)}else _(i,t,r)})),o){const t=e,r=Array.from(t);t.clear(),r.forEach((e=>{t.add(o.has(e)?o.get(e):e)}))}}function C(e,t){const r=3===e.type?e.setMap:e.copy;e.finalities.revoke.length>1&&e.assignedMap.get(t)&&r&&_(v(r,t),e.finalities.handledSet,e.options)}function F(e){3===e.type&&e.copy&&(e.copy.clear(),e.setMap.forEach((t=>{e.copy.add(d(t))})))}function I(e,t,r,n){if(e.operated&&e.assignedMap&&e.assignedMap.size>0&&!e.finalized){if(r&&n){const o=h(e);o&&t(e,o,r,n)}e.finalized=!0}}function $(e,t,r,n){const o=f(r);o&&(o.callbacks||(o.callbacks=[]),o.callbacks.push(((a,i)=>{const s=3===e.type?e.setMap:e.copy;if(m(v(s,t),r)){let r=o.original;o.copy&&(r=o.copy),F(e),I(e,n,a,i),b(s,t,r)}})),e.options.enableAutoFreeze&&o.finalities!==e.finalities&&(e.options.enableAutoFreeze=!1)),y(r,e.options)&&e.finalities.draft.push((()=>{m(v(3===e.type?e.setMap:e.copy,t),r)&&C(e,t)}))}function K(t,r,n,o){const{pathAsArray:a=!0}=t.options.enablePatches;switch(t.type){case 0:case 2:return function({original:t,copy:r,assignedMap:n},o,a,s,c){n.forEach(((n,l)=>{const p=v(t,l),u=P(v(r,l)),f=n?i(t,l)?e.Replace:e.Add:e.Remove;if(m(p,u)&&f===e.Replace)return;const d=M(o.concat(l),c);a.push(f===e.Remove?{op:f,path:d}:{op:f,path:d,value:u}),s.push(f===e.Add?{op:e.Remove,path:d}:f===e.Remove?{op:e.Add,path:d,value:p}:{op:e.Replace,path:d,value:p})}))}(t,r,n,o,a);case 1:return function(t,r,n,o,a){let{original:i,assignedMap:s,options:c}=t,l=t.copy;l.length<i.length&&([i,l]=[l,i],[n,o]=[o,n]);for(let t=0;t<i.length;t+=1)if(s.get(t.toString())&&l[t]!==i[t]){const s=M(r.concat([t]),a);n.push({op:e.Replace,path:s,value:P(l[t])}),o.push({op:e.Replace,path:s,value:P(i[t])})}for(let t=i.length;t<l.length;t+=1){const o=M(r.concat([t]),a);n.push({op:e.Add,path:o,value:P(l[t])})}if(i.length<l.length){const{arrayLengthAssignment:t=!0}=c.enablePatches;if(t){const t=M(r.concat(["length"]),a);o.push({op:e.Replace,path:t,value:i.length})}else for(let t=l.length;i.length<t;t-=1){const n=M(r.concat([t-1]),a);o.push({op:e.Remove,path:n})}}}(t,r,n,o,a);case 3:return function({original:t,copy:r},n,o,a,i){let s=0;t.forEach((t=>{if(!r.has(t)){const r=M(n.concat([s]),i);o.push({op:e.Remove,path:r,value:t}),a.unshift({op:e.Add,path:r,value:t})}s+=1})),s=0,r.forEach((r=>{if(!t.has(r)){const t=M(n.concat([s]),i);o.push({op:e.Add,path:t,value:r}),a.unshift({op:e.Remove,path:t,value:r})}s+=1}))}(t,r,n,o,a)}}let T=!1;const V=(e,t,r=!1)=>{if("object"==typeof e&&null!==e&&(!y(e,t)||r)&&!T)throw new Error("Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.")};const U={get size(){return p(f(this)).size},has(e){return p(f(this)).has(e)},set(e,t){const r=f(this),n=p(r);return n.has(e)&&m(n.get(e),t)||(S(r),E(r),r.assignedMap.set(e,!0),r.copy.set(e,t),$(r,e,t,K)),this},delete(e){if(!this.has(e))return!1;const t=f(this);return S(t),E(t),t.original.has(e)?t.assignedMap.set(e,!1):t.assignedMap.delete(e),t.copy.delete(e),!0},clear(){const e=f(this);if(this.size){S(e),E(e),e.assignedMap=new Map;for(const[t]of e.original)e.assignedMap.set(t,!1);e.copy.clear()}},forEach(e,t){p(f(this)).forEach(((r,n)=>{e.call(t,this.get(n),n,this)}))},get(e){var t,r;const n=f(this),i=p(n).get(e),s=(null===(r=(t=n.options).mark)||void 0===r?void 0:r.call(t,i,o))===o.mutable;if(n.options.strict&&V(i,n.options,s),s)return i;if(n.finalized||!y(i,n.options))return i;if(i!==n.original.get(e))return i;const c=a.createDraft({original:i,parentDraft:n,key:e,finalities:n.finalities,options:n.options});return S(n),n.copy.set(e,c),c},keys(){return p(f(this)).keys()},values(){const e=this.keys();return{[n]:()=>this.values(),next:()=>{const t=e.next();if(t.done)return t;return{done:!1,value:this.get(t.value)}}}},entries(){const e=this.keys();return{[n]:()=>this.entries(),next:()=>{const t=e.next();if(t.done)return t;const r=this.get(t.value);return{done:!1,value:[t.value,r]}}}},[n](){return this.entries()}},N=Reflect.ownKeys(U),W=(e,t,{isValuesIterator:r})=>()=>{var n,i;const s=t.next();if(s.done)return s;const c=s.value;let l=e.setMap.get(c);const p=f(l),u=(null===(i=(n=e.options).mark)||void 0===i?void 0:i.call(n,l,o))===o.mutable;if(e.options.strict&&V(c,e.options,u),u||p||!y(c,e.options)||e.finalized||!e.original.has(c))p&&(l=p.proxy);else{const t=a.createDraft({original:c,parentDraft:e,key:c,finalities:e.finalities,options:e.options});e.setMap.set(c,t),l=t}return{done:!1,value:r?l:[l,l]}},L={get size(){return f(this).setMap.size},has(e){const t=f(this);if(t.setMap.has(e))return!0;S(t);const r=f(e);return!(!r||!t.setMap.has(r.original))},add(e){const t=f(this);return this.has(e)||(S(t),E(t),t.assignedMap.set(e,!0),t.setMap.set(e,e),$(t,e,e,K)),this},delete(e){if(!this.has(e))return!1;const t=f(this);S(t),E(t);const r=f(e);return r&&t.setMap.has(r.original)?(t.assignedMap.set(r.original,!1),t.setMap.delete(r.original)):(!r&&t.setMap.has(e)?t.assignedMap.set(e,!1):t.assignedMap.delete(e),t.setMap.delete(e))},clear(){if(!this.size)return;const e=f(this);S(e),E(e);for(const t of e.original)e.assignedMap.set(t,!1);e.setMap.clear()},values(){const e=f(this);S(e);const t=e.setMap.keys();return{[Symbol.iterator]:()=>this.values(),next:W(e,t,{isValuesIterator:!0})}},entries(){const e=f(this);S(e);const t=e.setMap.keys();return{[Symbol.iterator]:()=>this.entries(),next:W(e,t,{isValuesIterator:!1})}},keys(){return this.values()},[n](){return this.values()},forEach(e,t){const r=this.values();let n=r.next();for(;!n.done;)e.call(t,n.value,n.value,this),n=r.next()}};Set.prototype.difference&&Object.assign(L,{intersection(e){return Set.prototype.intersection.call(new Set(this.values()),e)},union(e){return Set.prototype.union.call(new Set(this.values()),e)},difference(e){return Set.prototype.difference.call(new Set(this.values()),e)},symmetricDifference(e){return Set.prototype.symmetricDifference.call(new Set(this.values()),e)},isSubsetOf(e){return Set.prototype.isSubsetOf.call(new Set(this.values()),e)},isSupersetOf(e){return Set.prototype.isSupersetOf.call(new Set(this.values()),e)},isDisjointFrom(e){return Set.prototype.isDisjointFrom.call(new Set(this.values()),e)}});const Y=Reflect.ownKeys(L),B={get(e,r,n){var a,c;const l=null===(a=e.copy)||void 0===a?void 0:a[r];if(l&&e.finalities.draftsCache.has(l))return l;if(r===t)return e;let d;if(e.options.mark){const t="size"===r&&(e.original instanceof Map||e.original instanceof Set)?Reflect.get(e.original,r):Reflect.get(e.original,r,n);if(d=e.options.mark(t,o),d===o.mutable)return e.options.strict&&V(t,e.options,!0),t}const h=p(e);if(h instanceof Map&&N.includes(r)){if("size"===r)return Object.getOwnPropertyDescriptor(U,"size").get.call(e.proxy);return U[r].bind(e.proxy)}if(h instanceof Set&&Y.includes(r)){if("size"===r)return Object.getOwnPropertyDescriptor(L,"size").get.call(e.proxy);return L[r].bind(e.proxy)}if(!i(h,r)){const t=s(h,r);return t?"value"in t?t.value:null===(c=t.get)||void 0===c?void 0:c.call(e.proxy):void 0}const g=h[r];if(e.options.strict&&V(g,e.options),e.finalized||!y(g,e.options))return g;if(g===w(e.original,r)){if(S(e),e.copy[r]=X({original:e.original[r],parentDraft:e,key:1===e.type?Number(r):r,finalities:e.finalities,options:e.options}),"function"==typeof d){const t=f(e.copy[r]);return S(t),E(t),t.copy}return e.copy[r]}return u(g)&&e.finalities.draftsCache.add(g),g},set(e,t,r){var n;if(3===e.type||2===e.type)throw new Error("Map/Set draft does not support any property assignment.");let o;if(1===e.type&&"length"!==t&&(!(Number.isInteger(o=Number(t))&&o>=0)||0!==t&&0!==o&&String(o)!==String(t)))throw new Error("Only supports setting array indices and the 'length' property.");const a=s(p(e),t);if(null==a?void 0:a.set)return a.set.call(e.proxy,r),!0;const c=w(p(e),t),l=f(c);return l&&m(l.original,r)?(e.copy[t]=r,e.assignedMap=null!==(n=e.assignedMap)&&void 0!==n?n:new Map,e.assignedMap.set(t,!1),!0):(m(r,c)&&(void 0!==r||i(e.original,t))||(S(e),E(e),i(e.original,t)&&m(r,e.original[t])?e.assignedMap.delete(t):e.assignedMap.set(t,!0),e.copy[t]=r,$(e,t,r,K)),!0)},has:(e,t)=>t in p(e),ownKeys:e=>Reflect.ownKeys(p(e)),getOwnPropertyDescriptor(e,t){const r=p(e),n=Reflect.getOwnPropertyDescriptor(r,t);return n?{writable:!0,configurable:1!==e.type||"length"!==t,enumerable:n.enumerable,value:r[t]}:n},getPrototypeOf:e=>Reflect.getPrototypeOf(e.original),setPrototypeOf(){throw new Error("Cannot call 'setPrototypeOf()' on drafts")},defineProperty(){throw new Error("Cannot call 'defineProperty()' on drafts")},deleteProperty(e,t){var r;return 1===e.type?B.set.call(this,e,t,void 0,e.proxy):(void 0!==w(e.original,t)||t in e.original?(S(e),E(e),e.assignedMap.set(t,!1)):(e.assignedMap=null!==(r=e.assignedMap)&&void 0!==r?r:new Map,e.assignedMap.delete(t)),e.copy&&delete e.copy[t],!0)}};function X(e){const{original:t,parentDraft:r,key:n,finalities:o,options:a}=e,i=g(t),s={type:i,finalized:!1,parent:r,original:t,copy:null,proxy:null,finalities:o,options:a,setMap:3===i?new Map(t.entries()):void 0};(n||"key"in e)&&(s.key=n);const{proxy:c,revoke:l}=Proxy.revocable(1===i?Object.assign([],s):s,B);if(o.revoke.push(l),s.proxy=c,r){const e=r;e.finalities.draft.push(((t,r)=>{var o;const a=f(c);let i=3===e.type?e.setMap:e.copy;const s=v(i,n),l=f(s);if(l){let e=l.original;l.operated&&(e=d(s)),F(l),I(l,K,t,r),b(i,n,e)}null===(o=a.callbacks)||void 0===o||o.forEach((e=>{e(t,r)}))}))}else{const e=f(c);e.finalities.draft.push(((t,r)=>{F(e),I(e,K,t,r)}))}return c}function q(t,r,n,o,a){var i;const s=f(t),c=null!==(i=null==s?void 0:s.original)&&void 0!==i?i:t,l=!!r.length;if(null==s?void 0:s.operated)for(;s.finalities.draft.length>0;){s.finalities.draft.pop()(n,o)}const p=l?r[0]:s?s.operated?s.copy:s.original:t;return s&&O(s),a&&z(p,0,null==s||s.options.updatedValues),[p,n&&l?[{op:e.Replace,path:[],value:r[0]}]:n,o&&l?[{op:e.Replace,path:[],value:c}]:o]}function G(e){const{rootDraft:t,value:r,useRawReturn:n=!1,isRoot:o=!0}=e;D(r,((r,n,o)=>{const a=f(n);if(a&&t&&a.finalities===t.finalities){e.isContainDraft=!0;const t=a.original;if(o instanceof Set){const e=Array.from(o);o.clear(),e.forEach((e=>o.add(r===e?t:e)))}else b(o,r,t)}else"object"==typeof n&&null!==n&&(e.value=n,e.isRoot=!1,G(e))}))}function H(e){var t;const r=f(e);if(!y(e,null==r?void 0:r.options))return e;const n=g(e);if(r&&!r.operated)return r.original;let o;function a(){o=2===n?l(e)?new Map(e):new(Object.getPrototypeOf(e).constructor)(e):3===n?Array.from(r.setMap.values()):j(e,null==r?void 0:r.options)}if(r){r.finalized=!0;try{a()}finally{r.finalized=!1}}else o=e;if(D(o,((t,n)=>{if(r&&m(v(r.original,t),n))return;const i=H(n);i!==n&&(o===e&&a(),b(o,t,i))})),3===n){const e=null!==(t=null==r?void 0:r.original)&&void 0!==t?t:o;return c(e)?new Set(o):new(Object.getPrototypeOf(e).constructor)(o)}return o}function J(e){if(!u(e))throw new Error(`current() is only used for Draft, parameter: ${e}`);return H(e)}a.createDraft=X;const Q=e=>function t(n,a,i){var s,c,l;if("function"==typeof n&&"function"!=typeof a)return function(e,...r){return t(e,(e=>n.call(this,e,...r)),a)};const p=n,d=a;let h=i;"function"!=typeof a&&(h=a),h=Object.assign(Object.assign({},e),h);const g=u(p)?J(p):p,v=Array.isArray(h.mark)?(e,t)=>{for(const r of h.mark){const n=r(e,t);if(n)return n}}:h.mark,b=null!==(s=h.enablePatches)&&void 0!==s&&s,w=null!==(c=h.strict)&&void 0!==c&&c,M={enableAutoFreeze:null!==(l=h.enableAutoFreeze)&&void 0!==l&&l,mark:v,strict:w,enablePatches:b};if(!y(g,M)&&"object"==typeof g&&null!==g)throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");const[k,j]=function(e,t){var r;const n={draft:[],revoke:[],handledSet:new WeakSet,draftsCache:new WeakSet};let a,i;t.enablePatches&&(a=[],i=[]);const s=(null===(r=t.mark)||void 0===r?void 0:r.call(t,e,o))!==o.mutable&&y(e,t)?X({original:e,parentDraft:null,finalities:n,options:t}):e;return[s,(e=[])=>{const[r,n,o]=q(s,e,a,i,t.enableAutoFreeze);return t.enablePatches?[r,n,o]:r}]}(g,M);if("function"!=typeof a){if(!y(g,M))throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");return[k,j]}let S;try{S=d(k)}catch(e){throw O(f(k)),e}const R=e=>{const t=f(k);if(!u(e)){if(void 0!==e&&!m(e,k)&&(null==t?void 0:t.operated))throw new Error("Either the value is returned as a new non-draft value, or only the draft is modified without returning any value.");const n=null==e?void 0:e[r];if(n){const r=n[0];return M.strict&&"object"==typeof e&&null!==e&&G({rootDraft:t,value:e,useRawReturn:!0}),j([r])}if(void 0!==e)return"object"==typeof e&&null!==e&&G({rootDraft:t,value:e}),j([e])}if(e===k||void 0===e)return j([]);const n=f(e);if(M===n.options){if(n.operated)throw new Error("Cannot return a modified child draft.");return j([J(e)])}return j([e])};return S instanceof Promise?S.then(R,(e=>{throw O(f(k)),e})):R(S)},Z=Q();const ee=Object.prototype.constructor.toString();exports.apply=function(t,r,n){let o;for(o=r.length-1;o>=0;o-=1){const{value:n,op:a,path:i}=r[o];if(!i.length&&a===e.Replace||""===i&&a===e.Add){t=n;break}}o>-1&&(r=r.slice(o+1));const a=t=>{r.forEach((r=>{const{path:n,op:o}=r,a=function(e){return Array.isArray(e)?e:e.split("/").map((e=>e.replace(/~1/g,"/").replace(/~0/g,"~"))).slice(1)}(n);let i=t;for(let e=0;e<a.length-1;e+=1){const t=g(i);let r=a[e];if("string"!=typeof r&&"number"!=typeof r&&(r=String(r)),(0===t||1===t)&&("__proto__"===r||"constructor"===r)||"function"==typeof i&&"prototype"===r)throw new Error("Patching reserved attributes like __proto__ and constructor is not allowed.");if(i=v(3===t?Array.from(i):i,r),"object"!=typeof i)throw new Error(`Cannot apply patch at '${a.join("/")}'.`)}const s=g(i),c=R(r.value),l=a[a.length-1];switch(o){case e.Replace:switch(s){case 2:return i.set(l,c);case 3:throw new Error("Cannot apply replace patch to set.");default:return i[l]=c}case e.Add:switch(s){case 1:return"-"===l?i.push(c):i.splice(l,0,c);case 2:return i.set(l,c);case 3:return i.add(c);default:return i[l]=c}case e.Remove:switch(s){case 1:return i.splice(l,1);case 2:return i.delete(l);case 3:return i.delete(r.value);default:return delete i[l]}default:throw new Error(`Unsupported patch operation: ${o}.`)}}))};if(!(null==n?void 0:n.mutable)){if(u(t)){if(void 0!==n)throw new Error("Cannot apply patches with options to a draft.");return a(t),t}return Z(t,a,Object.assign(Object.assign({},n),{enablePatches:!1}))}a(t)},exports.castDraft=function(e){return e},exports.castImmutable=function(e){return e},exports.castMutable=function(e){return e},exports.create=Z,exports.current=J,exports.isDraft=u,exports.isDraftable=y,exports.makeCreator=Q,exports.markSimpleObject=e=>{if((e=>{if(!e||"object"!=typeof e)return!1;const t=Object.getPrototypeOf(e);if(null===t)return!0;const r=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return r===Object||"function"==typeof r&&Function.toString.call(r)===ee})(e))return o.immutable},exports.original=function(e){const t=f(e);if(!t)throw new Error(`original() is only used for a draft, parameter: ${e}`);return t.original},exports.rawReturn=function(e){if(0===arguments.length)throw new Error("rawReturn() must be called with a value.");if(arguments.length>1)throw new Error("rawReturn() must be called with one argument.");return{[r]:[e]}},exports.unsafe=function(e){let t;T=!0;try{t=e()}finally{T=!1}return t};
//# sourceMappingURL=mutative.cjs.production.min.js.map