You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

immer

Package Overview
Dependencies
Maintainers
2
Versions
173
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

immer - npm Package Compare versions

Comparing version

to
2.1.5

79

dist/immer.js

@@ -46,2 +46,3 @@ 'use strict';

var desc = Object.getOwnPropertyDescriptor(base, key);
var value = desc.value;

@@ -53,10 +54,10 @@ if (desc.get) {

desc.value = desc.get.call(base);
value = desc.get.call(base);
}
if (desc.enumerable) {
clone[key] = desc.value;
clone[key] = value;
} else {
Object.defineProperty(clone, key, {
value: desc.value,
value: value,
writable: true,

@@ -186,2 +187,16 @@ configurable: true

return state.copy || state.base;
} // Access a property without creating an Immer draft.
function peek(draft, prop) {
var state = draft[DRAFT_STATE];
if (state && !state.finalizing) {
state.finalizing = true;
var value = draft[prop];
state.finalizing = false;
return value;
}
return draft[prop];
}

@@ -191,5 +206,6 @@

assertUnrevoked(state);
var value = source(state)[prop]; // Drafts are only created for proxyable values that exist in the base state.
var value = peek(source(state), prop);
if (state.finalizing) { return value; } // Create a draft if the value is unmodified.
if (!state.finalizing && value === state.base[prop] && isDraftable(value)) {
if (value === peek(state.base, prop) && isDraftable(value)) {
prepareCopy(state);

@@ -207,3 +223,3 @@ return state.copy[prop] = createProxy(value, state);

if (!state.modified) {
if (is(source(state)[prop], value)) { return; }
if (is(value, peek(source(state), prop))) { return; }
markChanged(state);

@@ -428,7 +444,7 @@ prepareCopy(state);

has: function has$$1(target, prop) {
has: function has(target, prop) {
return prop in source$1(target);
},
ownKeys: function ownKeys$$1(target) {
ownKeys: function ownKeys(target) {
return Reflect.ownKeys(source$1(target));

@@ -481,2 +497,9 @@ },

return state.copy || state.base;
} // Access a property without creating an Immer draft.
function peek$1(draft, prop) {
var state = draft[DRAFT_STATE];
var desc = Reflect.getOwnPropertyDescriptor(state ? source$1(state) : draft, prop);
return desc && desc.value;
}

@@ -493,7 +516,11 @@

var value = source$1(state)[prop];
if (state.finalized || !isDraftable(value)) { return value; } // Check for existing draft in modified state.
if (state.finalized || !isDraftable(value)) {
return value;
} // Check for existing draft in modified state.
if (state.modified) {
// Assigned values are never drafted. This catches any drafts we created, too.
if (value !== state.base[prop]) { return value; } // Store drafts on the copy (when one exists).
if (value !== peek$1(state.base, prop)) { return value; } // Store drafts on the copy (when one exists).

@@ -508,6 +535,7 @@ drafts = state.copy;

if (!state.modified) {
// Optimize based on value's truthiness. Truthy values are guaranteed to
var baseValue = peek$1(state.base, prop); // Optimize based on value's truthiness. Truthy values are guaranteed to
// never be undefined, so we can avoid the `in` operator. Lastly, truthy
// values may be drafts, but falsy values are never drafts.
var isUnchanged = value ? is(state.base[prop], value) || value === state.drafts[prop] : is(state.base[prop], value) && prop in state.base;
var isUnchanged = value ? is(baseValue, value) || value === state.drafts[prop] : is(baseValue, value) && prop in state.base;
if (isUnchanged) { return true; }

@@ -524,3 +552,3 @@ markChanged$1(state);

// The `undefined` check is a fast path for pre-existing keys.
if (state.base[prop] !== undefined || prop in state.base) {
if (peek$1(state.base, prop) !== undefined || prop in state.base) {
state.assigned[prop] = false;

@@ -532,4 +560,6 @@ markChanged$1(state);

return true;
}
} // Note: We never coerce `desc.value` into an Immer draft, because we can't make
// the same guarantee in ES5 mode.
function getOwnPropertyDescriptor(state, prop) {

@@ -566,3 +596,3 @@ var owner = source$1(state);

function generateArrayPatches(state, basePath, patches, inversePatches) {
var assign$$1, assign$1;
var assign, assign$1;

@@ -574,3 +604,3 @@ var base = state.base;

if (copy.length < base.length) {
(assign$$1 = [copy, base], base = assign$$1[0], copy = assign$$1[1]);
(assign = [copy, base], base = assign[0], copy = assign[1]);
(assign$1 = [inversePatches, patches], patches = assign$1[0], inversePatches = assign$1[1]);

@@ -965,5 +995,4 @@ }

if (!this.useProxies) {
state.finalizing = true;
// Create the final copy, with added keys and without deleted keys.
state.copy = shallowCopy(state.draft, true);
state.finalizing = false;
}

@@ -1082,15 +1111,15 @@

exports.produce = produce;
exports.default = produce;
exports.setAutoFreeze = setAutoFreeze;
exports.setUseProxies = setUseProxies;
exports.Immer = Immer;
exports.applyPatches = applyPatches$1;
exports.createDraft = createDraft;
exports.default = produce;
exports.finishDraft = finishDraft;
exports.Immer = Immer;
exports.original = original;
exports.immerable = DRAFTABLE;
exports.isDraft = isDraft;
exports.isDraftable = isDraftable;
exports.nothing = NOTHING;
exports.immerable = DRAFTABLE;
exports.original = original;
exports.produce = produce;
exports.setAutoFreeze = setAutoFreeze;
exports.setUseProxies = setUseProxies;
//# sourceMappingURL=immer.js.map

@@ -42,2 +42,3 @@ var obj;

var desc = Object.getOwnPropertyDescriptor(base, key);
var value = desc.value;

@@ -49,10 +50,10 @@ if (desc.get) {

desc.value = desc.get.call(base);
value = desc.get.call(base);
}
if (desc.enumerable) {
clone[key] = desc.value;
clone[key] = value;
} else {
Object.defineProperty(clone, key, {
value: desc.value,
value: value,
writable: true,

@@ -182,2 +183,16 @@ configurable: true

return state.copy || state.base;
} // Access a property without creating an Immer draft.
function peek(draft, prop) {
var state = draft[DRAFT_STATE];
if (state && !state.finalizing) {
state.finalizing = true;
var value = draft[prop];
state.finalizing = false;
return value;
}
return draft[prop];
}

@@ -187,5 +202,6 @@

assertUnrevoked(state);
var value = source(state)[prop]; // Drafts are only created for proxyable values that exist in the base state.
var value = peek(source(state), prop);
if (state.finalizing) { return value; } // Create a draft if the value is unmodified.
if (!state.finalizing && value === state.base[prop] && isDraftable(value)) {
if (value === peek(state.base, prop) && isDraftable(value)) {
prepareCopy(state);

@@ -203,3 +219,3 @@ return state.copy[prop] = createProxy(value, state);

if (!state.modified) {
if (is(source(state)[prop], value)) { return; }
if (is(value, peek(source(state), prop))) { return; }
markChanged(state);

@@ -424,7 +440,7 @@ prepareCopy(state);

has: function has$$1(target, prop) {
has: function has(target, prop) {
return prop in source$1(target);
},
ownKeys: function ownKeys$$1(target) {
ownKeys: function ownKeys(target) {
return Reflect.ownKeys(source$1(target));

@@ -477,2 +493,9 @@ },

return state.copy || state.base;
} // Access a property without creating an Immer draft.
function peek$1(draft, prop) {
var state = draft[DRAFT_STATE];
var desc = Reflect.getOwnPropertyDescriptor(state ? source$1(state) : draft, prop);
return desc && desc.value;
}

@@ -489,7 +512,11 @@

var value = source$1(state)[prop];
if (state.finalized || !isDraftable(value)) { return value; } // Check for existing draft in modified state.
if (state.finalized || !isDraftable(value)) {
return value;
} // Check for existing draft in modified state.
if (state.modified) {
// Assigned values are never drafted. This catches any drafts we created, too.
if (value !== state.base[prop]) { return value; } // Store drafts on the copy (when one exists).
if (value !== peek$1(state.base, prop)) { return value; } // Store drafts on the copy (when one exists).

@@ -504,6 +531,7 @@ drafts = state.copy;

if (!state.modified) {
// Optimize based on value's truthiness. Truthy values are guaranteed to
var baseValue = peek$1(state.base, prop); // Optimize based on value's truthiness. Truthy values are guaranteed to
// never be undefined, so we can avoid the `in` operator. Lastly, truthy
// values may be drafts, but falsy values are never drafts.
var isUnchanged = value ? is(state.base[prop], value) || value === state.drafts[prop] : is(state.base[prop], value) && prop in state.base;
var isUnchanged = value ? is(baseValue, value) || value === state.drafts[prop] : is(baseValue, value) && prop in state.base;
if (isUnchanged) { return true; }

@@ -520,3 +548,3 @@ markChanged$1(state);

// The `undefined` check is a fast path for pre-existing keys.
if (state.base[prop] !== undefined || prop in state.base) {
if (peek$1(state.base, prop) !== undefined || prop in state.base) {
state.assigned[prop] = false;

@@ -528,4 +556,6 @@ markChanged$1(state);

return true;
}
} // Note: We never coerce `desc.value` into an Immer draft, because we can't make
// the same guarantee in ES5 mode.
function getOwnPropertyDescriptor(state, prop) {

@@ -562,3 +592,3 @@ var owner = source$1(state);

function generateArrayPatches(state, basePath, patches, inversePatches) {
var assign$$1, assign$1;
var assign, assign$1;

@@ -570,3 +600,3 @@ var base = state.base;

if (copy.length < base.length) {
(assign$$1 = [copy, base], base = assign$$1[0], copy = assign$$1[1]);
(assign = [copy, base], base = assign[0], copy = assign[1]);
(assign$1 = [inversePatches, patches], patches = assign$1[0], inversePatches = assign$1[1]);

@@ -961,5 +991,4 @@ }

if (!this.useProxies) {
state.finalizing = true;
// Create the final copy, with added keys and without deleted keys.
state.copy = shallowCopy(state.draft, true);
state.finalizing = false;
}

@@ -1079,3 +1108,3 @@

export default produce;
export { produce, setAutoFreeze, setUseProxies, applyPatches$1 as applyPatches, createDraft, finishDraft, Immer, original, isDraft, isDraftable, NOTHING as nothing, DRAFTABLE as immerable };
export { Immer, applyPatches$1 as applyPatches, createDraft, finishDraft, DRAFTABLE as immerable, isDraft, isDraftable, NOTHING as nothing, original, produce, setAutoFreeze, setUseProxies };
//# sourceMappingURL=immer.module.js.map

@@ -1,2 +0,2 @@

!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).immer={})}(this,function(e){"use strict";var r,t="undefined"!=typeof Symbol?Symbol("immer-nothing"):((r={})["immer-nothing"]=!0,r),n="undefined"!=typeof Symbol?Symbol.for("immer-draftable"):"__$immer_draftable",o="undefined"!=typeof Symbol?Symbol.for("immer-state"):"__$immer_state";function i(e){return!!e&&!!e[o]}function a(e){if(!e||"object"!=typeof e)return!1;if(Array.isArray(e))return!0;var r=Object.getPrototypeOf(e);return!r||r===Object.prototype||(!!e[n]||!!e.constructor[n])}var s=Object.assign||function(e,r){for(var t in r)l(r,t)&&(e[t]=r[t]);return e},f="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:Object.getOwnPropertyNames;function c(e,r){if(void 0===r&&(r=!1),Array.isArray(e))return e.slice();var t=Object.create(Object.getPrototypeOf(e));return f(e).forEach(function(n){if(n!==o){var i=Object.getOwnPropertyDescriptor(e,n);if(i.get){if(!r)throw new Error("Immer drafts cannot have computed properties");i.value=i.get.call(e)}i.enumerable?t[n]=i.value:Object.defineProperty(t,n,{value:i.value,writable:!0,configurable:!0})}}),t}function u(e,r){if(Array.isArray(e))for(var t=0;t<e.length;t++)r(t,e[t],e);else f(e).forEach(function(t){return r(t,e[t],e)})}function p(e,r){return Object.getOwnPropertyDescriptor(e,r).enumerable}function l(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function d(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var h=function(e){this.drafts=[],this.parent=e,this.canAutoFreeze=!0,this.patches=null};function y(e){e[o].revoke()}h.prototype.usePatches=function(e){e&&(this.patches=[],this.inversePatches=[],this.patchListener=e)},h.prototype.revoke=function(){this.leave(),this.drafts.forEach(y),this.drafts=null},h.prototype.leave=function(){this===h.current&&(h.current=this.parent)},h.current=null,h.enter=function(){return this.current=new h(this.current)};var v={};function b(e,r){var t=Array.isArray(e),n=O(e);u(n,function(r){!function(e,r,t){var n=v[r];n?n.enumerable=t:v[r]=n={configurable:!0,enumerable:t,get:function(){return function(e,r){j(e);var t=m(e)[r];if(!e.finalizing&&t===e.base[r]&&a(t))return P(e),e.copy[r]=b(t,e);return t}(this[o],r)},set:function(e){!function(e,r,t){if(j(e),e.assigned[r]=!0,!e.modified){if(d(m(e)[r],t))return;w(e),P(e)}e.copy[r]=t}(this[o],r,e)}};Object.defineProperty(e,r,n)}(n,r,t||p(e,r))});var i,s,f,c=r?r.scope:h.current;return i=n,s=o,f={scope:c,modified:!1,finalizing:!1,finalized:!1,assigned:{},parent:r,base:e,draft:n,copy:null,revoke:g,revoked:!1},Object.defineProperty(i,s,{value:f,enumerable:!1,writable:!0}),c.drafts.push(n),n}function g(){this.revoked=!0}function m(e){return e.copy||e.base}function w(e){e.modified||(e.modified=!0,e.parent&&w(e.parent))}function P(e){e.copy||(e.copy=O(e.base))}function O(e){var r=e&&e[o];if(r){r.finalizing=!0;var t=c(r.draft,!0);return r.finalizing=!1,t}return c(e)}function j(e){if(!0===e.revoked)throw new Error("Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? "+JSON.stringify(m(e)))}function z(e){for(var r=e.length-1;r>=0;r--){var t=e[r][o];t.modified||(Array.isArray(t.base)?E(t)&&w(t):A(t)&&w(t))}}function A(e){for(var r=e.base,t=e.draft,n=Object.keys(t),i=n.length-1;i>=0;i--){var a=n[i],s=r[a];if(void 0===s&&!l(r,a))return!0;var f=t[a],c=f&&f[o];if(c?c.base!==s:!d(f,s))return!0}return n.length!==Object.keys(r).length}function E(e){var r=e.draft;if(r.length!==e.base.length)return!0;var t=Object.getOwnPropertyDescriptor(r,r.length-1);return!(!t||t.get)}var D=Object.freeze({willFinalize:function(e,r,t){e.drafts.forEach(function(e){e[o].finalizing=!0}),t?i(r)&&r[o].scope===e&&z(e.drafts):(e.patches&&function e(r){if(r&&"object"==typeof r){var t=r[o];if(t){var n=t.base,i=t.draft,a=t.assigned;if(Array.isArray(r)){if(E(t)){if(w(t),a.length=!0,i.length<n.length)for(var s=i.length;s<n.length;s++)a[s]=!1;else for(var f=n.length;f<i.length;f++)a[f]=!0;for(var c=0;c<i.length;c++)void 0===a[c]&&e(i[c])}}else Object.keys(i).forEach(function(r){void 0!==n[r]||l(n,r)?a[r]||e(i[r]):(a[r]=!0,w(t))}),Object.keys(n).forEach(function(e){void 0!==i[e]||l(i,e)||(a[e]=!1,w(t))})}}}(e.drafts[0]),z(e.drafts))},createProxy:b});function k(e,r){var t=r?r.scope:h.current,n={scope:t,modified:!1,finalized:!1,assigned:{},parent:r,base:e,draft:null,drafts:{},copy:null,revoke:null},o=Array.isArray(e)?Proxy.revocable([n],F):Proxy.revocable(n,x),i=o.revoke,a=o.proxy;return n.draft=a,n.revoke=i,t.drafts.push(a),a}var x={get:function(e,r){if(r===o)return e;var t=e.drafts;if(!e.modified&&l(t,r))return t[r];var n=R(e)[r];if(e.finalized||!a(n))return n;if(e.modified){if(n!==e.base[r])return n;t=e.copy}return t[r]=k(n,e)},has:function(e,r){return r in R(e)},ownKeys:function(e){return Reflect.ownKeys(R(e))},set:function(e,r,t){if(!e.modified){var n=t?d(e.base[r],t)||t===e.drafts[r]:d(e.base[r],t)&&r in e.base;if(n)return!0;I(e)}return e.assigned[r]=!0,e.copy[r]=t,!0},deleteProperty:function(e,r){(void 0!==e.base[r]||r in e.base)&&(e.assigned[r]=!1,I(e));e.copy&&delete e.copy[r];return!0},getOwnPropertyDescriptor:function(e,r){var t=R(e),n=Reflect.getOwnPropertyDescriptor(t,r);n&&(n.writable=!0,n.configurable=!Array.isArray(t)||"length"!==r);return n},defineProperty:function(){throw new Error("Object.defineProperty() cannot be used on an Immer draft")},getPrototypeOf:function(e){return Object.getPrototypeOf(e.base)},setPrototypeOf:function(){throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft")}},F={};function R(e){return e.copy||e.base}function I(e){e.modified||(e.modified=!0,e.copy=s(c(e.base),e.drafts),e.drafts=null,e.parent&&I(e.parent))}u(x,function(e,r){F[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}}),F.deleteProperty=function(e,r){if(isNaN(parseInt(r)))throw new Error("Immer only supports deleting array indices");return x.deleteProperty.call(this,e[0],r)},F.set=function(e,r,t){if("length"!==r&&isNaN(parseInt(r)))throw new Error("Immer only supports setting array indices and the 'length' property");return x.set.call(this,e[0],r,t)};var N=Object.freeze({willFinalize:function(){},createProxy:k});function S(e,r,t,n){Array.isArray(e.base)?function(e,r,t,n){var o,i,a=e.base,s=e.copy,f=e.assigned;s.length<a.length&&(a=(o=[s,a])[0],s=o[1],t=(i=[n,t])[0],n=i[1]);var c=s.length-a.length,u=0;for(;a[u]===s[u]&&u<a.length;)++u;var p=a.length;for(;p>u&&a[p-1]===s[p+c-1];)--p;for(var l=u;l<p;++l)if(f[l]&&s[l]!==a[l]){var d=r.concat([l]);t.push({op:"replace",path:d,value:s[l]}),n.push({op:"replace",path:d,value:a[l]})}for(var h=p!=a.length,y=t.length,v=p+c-1;v>=p;--v){var b=r.concat([v]);t[y+v-p]={op:"add",path:b,value:s[v]},h&&n.push({op:"remove",path:b})}h||n.push({op:"replace",path:r.concat(["length"]),value:a.length})}(e,r,t,n):function(e,r,t,n){var o=e.base,i=e.copy;u(e.assigned,function(e,a){var s=o[e],f=i[e],c=a?e in o?"replace":"add":"remove";if(s!==f||"replace"!==c){var u=r.concat(e);t.push("remove"===c?{op:c,path:u}:{op:c,path:u,value:f}),n.push("add"===c?{op:"remove",path:u}:"remove"===c?{op:"add",path:u,value:s}:{op:"replace",path:u,value:s})}})}(e,r,t,n)}function _(e,r){for(var t=0;t<r.length;t++){var n=r[t],o=n.path;if(0===o.length&&"replace"===n.op)e=n.value;else{for(var i=e,a=0;a<o.length-1;a++)if(!(i=i[o[a]])||"object"!=typeof i)throw new Error("Cannot apply patch, path doesn't resolve: "+o.join("/"));var s=o[o.length-1];switch(n.op){case"replace":i[s]=n.value;break;case"add":Array.isArray(i)?i.splice(s,0,n.value):i[s]=n.value;break;case"remove":Array.isArray(i)?i.splice(s,1):delete i[s];break;default:throw new Error("Unsupported patch operation: "+n.op)}}}return e}var T={useProxies:"undefined"!=typeof Proxy&&"undefined"!=typeof Reflect,autoFreeze:"undefined"!=typeof process?"production"!==process.env.NODE_ENV:"verifyMinified"===function(){}.name,onAssign:null,onDelete:null,onCopy:null},C=function(e){s(this,T,e),this.setUseProxies(this.useProxies),this.produce=this.produce.bind(this)};C.prototype.produce=function(e,r,n){var o,i=this;if("function"==typeof e&&"function"!=typeof r){var s=r;return r=e,function(e){void 0===e&&(e=s);for(var t=[],n=arguments.length-1;n-- >0;)t[n]=arguments[n+1];return i.produce(e,function(e){return r.call.apply(r,[e,e].concat(t))})}}if("function"!=typeof r)throw new Error("The first or second argument to `produce` must be a function");if(void 0!==n&&"function"!=typeof n)throw new Error("The third argument to `produce` must be a function or undefined");if(a(e)){var f=h.enter(),c=this.createProxy(e),u=!0;try{o=r.call(c,c),u=!1}finally{u?f.revoke():f.leave()}return o instanceof Promise?o.then(function(e){return f.usePatches(n),i.processResult(e,f)},function(e){throw f.revoke(),e}):(f.usePatches(n),this.processResult(o,f))}return void 0===(o=r(e))?e:o!==t?o:void 0},C.prototype.createDraft=function(e){if(!a(e))throw new Error("First argument to `createDraft` must be a plain object, an array, or an immerable object");var r=h.enter(),t=this.createProxy(e);return t[o].isManual=!0,r.leave(),t},C.prototype.finishDraft=function(e,r){var t=e&&e[o];if(!t||!t.isManual)throw new Error("First argument to `finishDraft` must be a draft returned by `createDraft`");if(t.finalized)throw new Error("The given draft is already finalized");var n=t.scope;return n.usePatches(r),this.processResult(void 0,n)},C.prototype.setAutoFreeze=function(e){this.autoFreeze=e},C.prototype.setUseProxies=function(e){this.useProxies=e,s(this,e?N:D)},C.prototype.applyPatches=function(e,r){return i(e)?_(e,r):this.produce(e,function(e){return _(e,r)})},C.prototype.processResult=function(e,r){var n=r.drafts[0],i=void 0!==e&&e!==n;if(this.willFinalize(r,e,i),i){if(n[o].modified)throw r.revoke(),new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.");a(e)&&(e=this.finalize(e,null,r)),r.patches&&(r.patches.push({op:"replace",path:[],value:e}),r.inversePatches.push({op:"replace",path:[],value:n[o].base}))}else e=this.finalize(n,[],r);return r.revoke(),r.patches&&r.patchListener(r.patches,r.inversePatches),e!==t?e:void 0},C.prototype.finalize=function(e,r,t){var n=this,i=e[o];if(!i)return Object.isFrozen(e)?e:this.finalizeTree(e,null,t);if(i.scope!==t)return e;if(!i.modified)return i.base;if(!i.finalized){if(i.finalized=!0,this.finalizeTree(i.draft,r,t),this.onDelete)if(this.useProxies){var a=i.assigned;for(var s in a)a[s]||this.onDelete(i,s)}else{var f=i.base,c=i.copy;u(f,function(e){l(c,e)||n.onDelete(i,e)})}this.onCopy&&this.onCopy(i),this.autoFreeze&&t.canAutoFreeze&&Object.freeze(i.copy),r&&t.patches&&S(i,r,t.patches,t.inversePatches)}return i.copy},C.prototype.finalizeTree=function(e,r,t){var n=this,s=e[o];s&&(this.useProxies||(s.finalizing=!0,s.copy=c(s.draft,!0),s.finalizing=!1),e=s.copy);var f=!!r&&!!t.patches,l=function(o,c,h){if(c===h)throw Error("Immer forbids circular references");var y=!!s&&h===e;if(i(c)){var v=y&&f&&!s.assigned[o]?r.concat(o):null;if(i(c=n.finalize(c,v,t))&&(t.canAutoFreeze=!1),Array.isArray(h)||p(h,o)?h[o]=c:Object.defineProperty(h,o,{value:c}),y&&c===s.base[o])return}else{if(y&&d(c,s.base[o]))return;a(c)&&!Object.isFrozen(c)&&u(c,l)}y&&n.onAssign&&n.onAssign(s,o,c)};return u(e,l),e};var U=new C,K=U.produce,M=U.setAutoFreeze.bind(U),L=U.setUseProxies.bind(U),$=U.applyPatches.bind(U),J=U.createDraft.bind(U),V=U.finishDraft.bind(U);e.produce=K,e.default=K,e.setAutoFreeze=M,e.setUseProxies=L,e.applyPatches=$,e.createDraft=J,e.finishDraft=V,e.Immer=C,e.original=function(e){if(e&&e[o])return e[o].base},e.isDraft=i,e.isDraftable=a,e.nothing=t,e.immerable=n,Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).immer={})}(this,function(e){"use strict";var r,t="undefined"!=typeof Symbol?Symbol("immer-nothing"):((r={})["immer-nothing"]=!0,r),n="undefined"!=typeof Symbol?Symbol.for("immer-draftable"):"__$immer_draftable",o="undefined"!=typeof Symbol?Symbol.for("immer-state"):"__$immer_state";function i(e){return!!e&&!!e[o]}function a(e){if(!e||"object"!=typeof e)return!1;if(Array.isArray(e))return!0;var r=Object.getPrototypeOf(e);return!r||r===Object.prototype||(!!e[n]||!!e.constructor[n])}var s=Object.assign||function(e,r){for(var t in r)l(r,t)&&(e[t]=r[t]);return e},f="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:Object.getOwnPropertyNames;function c(e,r){if(void 0===r&&(r=!1),Array.isArray(e))return e.slice();var t=Object.create(Object.getPrototypeOf(e));return f(e).forEach(function(n){if(n!==o){var i=Object.getOwnPropertyDescriptor(e,n),a=i.value;if(i.get){if(!r)throw new Error("Immer drafts cannot have computed properties");a=i.get.call(e)}i.enumerable?t[n]=a:Object.defineProperty(t,n,{value:a,writable:!0,configurable:!0})}}),t}function u(e,r){if(Array.isArray(e))for(var t=0;t<e.length;t++)r(t,e[t],e);else f(e).forEach(function(t){return r(t,e[t],e)})}function p(e,r){return Object.getOwnPropertyDescriptor(e,r).enumerable}function l(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function d(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var h=function(e){this.drafts=[],this.parent=e,this.canAutoFreeze=!0,this.patches=null};function y(e){e[o].revoke()}h.prototype.usePatches=function(e){e&&(this.patches=[],this.inversePatches=[],this.patchListener=e)},h.prototype.revoke=function(){this.leave(),this.drafts.forEach(y),this.drafts=null},h.prototype.leave=function(){this===h.current&&(h.current=this.parent)},h.current=null,h.enter=function(){return this.current=new h(this.current)};var v={};function b(e,r){var t=Array.isArray(e),n=z(e);u(n,function(r){!function(e,r,t){var n=v[r];n?n.enumerable=t:v[r]=n={configurable:!0,enumerable:t,get:function(){return function(e,r){j(e);var t=w(m(e),r);if(e.finalizing)return t;if(t===w(e.base,r)&&a(t))return O(e),e.copy[r]=b(t,e);return t}(this[o],r)},set:function(e){!function(e,r,t){if(j(e),e.assigned[r]=!0,!e.modified){if(d(t,w(m(e),r)))return;P(e),O(e)}e.copy[r]=t}(this[o],r,e)}};Object.defineProperty(e,r,n)}(n,r,t||p(e,r))});var i,s,f,c=r?r.scope:h.current;return i=n,s=o,f={scope:c,modified:!1,finalizing:!1,finalized:!1,assigned:{},parent:r,base:e,draft:n,copy:null,revoke:g,revoked:!1},Object.defineProperty(i,s,{value:f,enumerable:!1,writable:!0}),c.drafts.push(n),n}function g(){this.revoked=!0}function m(e){return e.copy||e.base}function w(e,r){var t=e[o];if(t&&!t.finalizing){t.finalizing=!0;var n=e[r];return t.finalizing=!1,n}return e[r]}function P(e){e.modified||(e.modified=!0,e.parent&&P(e.parent))}function O(e){e.copy||(e.copy=z(e.base))}function z(e){var r=e&&e[o];if(r){r.finalizing=!0;var t=c(r.draft,!0);return r.finalizing=!1,t}return c(e)}function j(e){if(!0===e.revoked)throw new Error("Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? "+JSON.stringify(m(e)))}function A(e){for(var r=e.length-1;r>=0;r--){var t=e[r][o];t.modified||(Array.isArray(t.base)?D(t)&&P(t):E(t)&&P(t))}}function E(e){for(var r=e.base,t=e.draft,n=Object.keys(t),i=n.length-1;i>=0;i--){var a=n[i],s=r[a];if(void 0===s&&!l(r,a))return!0;var f=t[a],c=f&&f[o];if(c?c.base!==s:!d(f,s))return!0}return n.length!==Object.keys(r).length}function D(e){var r=e.draft;if(r.length!==e.base.length)return!0;var t=Object.getOwnPropertyDescriptor(r,r.length-1);return!(!t||t.get)}var k=Object.freeze({willFinalize:function(e,r,t){e.drafts.forEach(function(e){e[o].finalizing=!0}),t?i(r)&&r[o].scope===e&&A(e.drafts):(e.patches&&function e(r){if(r&&"object"==typeof r){var t=r[o];if(t){var n=t.base,i=t.draft,a=t.assigned;if(Array.isArray(r)){if(D(t)){if(P(t),a.length=!0,i.length<n.length)for(var s=i.length;s<n.length;s++)a[s]=!1;else for(var f=n.length;f<i.length;f++)a[f]=!0;for(var c=0;c<i.length;c++)void 0===a[c]&&e(i[c])}}else Object.keys(i).forEach(function(r){void 0!==n[r]||l(n,r)?a[r]||e(i[r]):(a[r]=!0,P(t))}),Object.keys(n).forEach(function(e){void 0!==i[e]||l(i,e)||(a[e]=!1,P(t))})}}}(e.drafts[0]),A(e.drafts))},createProxy:b});function x(e,r){var t=r?r.scope:h.current,n={scope:t,modified:!1,finalized:!1,assigned:{},parent:r,base:e,draft:null,drafts:{},copy:null,revoke:null},o=Array.isArray(e)?Proxy.revocable([n],R):Proxy.revocable(n,F),i=o.revoke,a=o.proxy;return n.draft=a,n.revoke=i,t.drafts.push(a),a}var F={get:function(e,r){if(r===o)return e;var t=e.drafts;if(!e.modified&&l(t,r))return t[r];var n=I(e)[r];if(e.finalized||!a(n))return n;if(e.modified){if(n!==N(e.base,r))return n;t=e.copy}return t[r]=x(n,e)},has:function(e,r){return r in I(e)},ownKeys:function(e){return Reflect.ownKeys(I(e))},set:function(e,r,t){if(!e.modified){var n=N(e.base,r),o=t?d(n,t)||t===e.drafts[r]:d(n,t)&&r in e.base;if(o)return!0;S(e)}return e.assigned[r]=!0,e.copy[r]=t,!0},deleteProperty:function(e,r){(void 0!==N(e.base,r)||r in e.base)&&(e.assigned[r]=!1,S(e));e.copy&&delete e.copy[r];return!0},getOwnPropertyDescriptor:function(e,r){var t=I(e),n=Reflect.getOwnPropertyDescriptor(t,r);n&&(n.writable=!0,n.configurable=!Array.isArray(t)||"length"!==r);return n},defineProperty:function(){throw new Error("Object.defineProperty() cannot be used on an Immer draft")},getPrototypeOf:function(e){return Object.getPrototypeOf(e.base)},setPrototypeOf:function(){throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft")}},R={};function I(e){return e.copy||e.base}function N(e,r){var t=e[o],n=Reflect.getOwnPropertyDescriptor(t?I(t):e,r);return n&&n.value}function S(e){e.modified||(e.modified=!0,e.copy=s(c(e.base),e.drafts),e.drafts=null,e.parent&&S(e.parent))}u(F,function(e,r){R[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}}),R.deleteProperty=function(e,r){if(isNaN(parseInt(r)))throw new Error("Immer only supports deleting array indices");return F.deleteProperty.call(this,e[0],r)},R.set=function(e,r,t){if("length"!==r&&isNaN(parseInt(r)))throw new Error("Immer only supports setting array indices and the 'length' property");return F.set.call(this,e[0],r,t)};var _=Object.freeze({willFinalize:function(){},createProxy:x});function T(e,r,t,n){Array.isArray(e.base)?function(e,r,t,n){var o,i,a=e.base,s=e.copy,f=e.assigned;s.length<a.length&&(a=(o=[s,a])[0],s=o[1],t=(i=[n,t])[0],n=i[1]);var c=s.length-a.length,u=0;for(;a[u]===s[u]&&u<a.length;)++u;var p=a.length;for(;p>u&&a[p-1]===s[p+c-1];)--p;for(var l=u;l<p;++l)if(f[l]&&s[l]!==a[l]){var d=r.concat([l]);t.push({op:"replace",path:d,value:s[l]}),n.push({op:"replace",path:d,value:a[l]})}for(var h=p!=a.length,y=t.length,v=p+c-1;v>=p;--v){var b=r.concat([v]);t[y+v-p]={op:"add",path:b,value:s[v]},h&&n.push({op:"remove",path:b})}h||n.push({op:"replace",path:r.concat(["length"]),value:a.length})}(e,r,t,n):function(e,r,t,n){var o=e.base,i=e.copy;u(e.assigned,function(e,a){var s=o[e],f=i[e],c=a?e in o?"replace":"add":"remove";if(s!==f||"replace"!==c){var u=r.concat(e);t.push("remove"===c?{op:c,path:u}:{op:c,path:u,value:f}),n.push("add"===c?{op:"remove",path:u}:"remove"===c?{op:"add",path:u,value:s}:{op:"replace",path:u,value:s})}})}(e,r,t,n)}function C(e,r){for(var t=0;t<r.length;t++){var n=r[t],o=n.path;if(0===o.length&&"replace"===n.op)e=n.value;else{for(var i=e,a=0;a<o.length-1;a++)if(!(i=i[o[a]])||"object"!=typeof i)throw new Error("Cannot apply patch, path doesn't resolve: "+o.join("/"));var s=o[o.length-1];switch(n.op){case"replace":i[s]=n.value;break;case"add":Array.isArray(i)?i.splice(s,0,n.value):i[s]=n.value;break;case"remove":Array.isArray(i)?i.splice(s,1):delete i[s];break;default:throw new Error("Unsupported patch operation: "+n.op)}}}return e}var U={useProxies:"undefined"!=typeof Proxy&&"undefined"!=typeof Reflect,autoFreeze:"undefined"!=typeof process?"production"!==process.env.NODE_ENV:"verifyMinified"===function(){}.name,onAssign:null,onDelete:null,onCopy:null},K=function(e){s(this,U,e),this.setUseProxies(this.useProxies),this.produce=this.produce.bind(this)};K.prototype.produce=function(e,r,n){var o,i=this;if("function"==typeof e&&"function"!=typeof r){var s=r;return r=e,function(e){void 0===e&&(e=s);for(var t=[],n=arguments.length-1;n-- >0;)t[n]=arguments[n+1];return i.produce(e,function(e){return r.call.apply(r,[e,e].concat(t))})}}if("function"!=typeof r)throw new Error("The first or second argument to `produce` must be a function");if(void 0!==n&&"function"!=typeof n)throw new Error("The third argument to `produce` must be a function or undefined");if(a(e)){var f=h.enter(),c=this.createProxy(e),u=!0;try{o=r.call(c,c),u=!1}finally{u?f.revoke():f.leave()}return o instanceof Promise?o.then(function(e){return f.usePatches(n),i.processResult(e,f)},function(e){throw f.revoke(),e}):(f.usePatches(n),this.processResult(o,f))}return void 0===(o=r(e))?e:o!==t?o:void 0},K.prototype.createDraft=function(e){if(!a(e))throw new Error("First argument to `createDraft` must be a plain object, an array, or an immerable object");var r=h.enter(),t=this.createProxy(e);return t[o].isManual=!0,r.leave(),t},K.prototype.finishDraft=function(e,r){var t=e&&e[o];if(!t||!t.isManual)throw new Error("First argument to `finishDraft` must be a draft returned by `createDraft`");if(t.finalized)throw new Error("The given draft is already finalized");var n=t.scope;return n.usePatches(r),this.processResult(void 0,n)},K.prototype.setAutoFreeze=function(e){this.autoFreeze=e},K.prototype.setUseProxies=function(e){this.useProxies=e,s(this,e?_:k)},K.prototype.applyPatches=function(e,r){return i(e)?C(e,r):this.produce(e,function(e){return C(e,r)})},K.prototype.processResult=function(e,r){var n=r.drafts[0],i=void 0!==e&&e!==n;if(this.willFinalize(r,e,i),i){if(n[o].modified)throw r.revoke(),new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.");a(e)&&(e=this.finalize(e,null,r)),r.patches&&(r.patches.push({op:"replace",path:[],value:e}),r.inversePatches.push({op:"replace",path:[],value:n[o].base}))}else e=this.finalize(n,[],r);return r.revoke(),r.patches&&r.patchListener(r.patches,r.inversePatches),e!==t?e:void 0},K.prototype.finalize=function(e,r,t){var n=this,i=e[o];if(!i)return Object.isFrozen(e)?e:this.finalizeTree(e,null,t);if(i.scope!==t)return e;if(!i.modified)return i.base;if(!i.finalized){if(i.finalized=!0,this.finalizeTree(i.draft,r,t),this.onDelete)if(this.useProxies){var a=i.assigned;for(var s in a)a[s]||this.onDelete(i,s)}else{var f=i.base,c=i.copy;u(f,function(e){l(c,e)||n.onDelete(i,e)})}this.onCopy&&this.onCopy(i),this.autoFreeze&&t.canAutoFreeze&&Object.freeze(i.copy),r&&t.patches&&T(i,r,t.patches,t.inversePatches)}return i.copy},K.prototype.finalizeTree=function(e,r,t){var n=this,s=e[o];s&&(this.useProxies||(s.copy=c(s.draft,!0)),e=s.copy);var f=!!r&&!!t.patches,l=function(o,c,h){if(c===h)throw Error("Immer forbids circular references");var y=!!s&&h===e;if(i(c)){var v=y&&f&&!s.assigned[o]?r.concat(o):null;if(i(c=n.finalize(c,v,t))&&(t.canAutoFreeze=!1),Array.isArray(h)||p(h,o)?h[o]=c:Object.defineProperty(h,o,{value:c}),y&&c===s.base[o])return}else{if(y&&d(c,s.base[o]))return;a(c)&&!Object.isFrozen(c)&&u(c,l)}y&&n.onAssign&&n.onAssign(s,o,c)};return u(e,l),e};var M=new K,L=M.produce,$=M.setAutoFreeze.bind(M),J=M.setUseProxies.bind(M),V=M.applyPatches.bind(M),q=M.createDraft.bind(M),B=M.finishDraft.bind(M);e.Immer=K,e.applyPatches=V,e.createDraft=q,e.default=L,e.finishDraft=B,e.immerable=n,e.isDraft=i,e.isDraftable=a,e.nothing=t,e.original=function(e){if(e&&e[o])return e[o].base},e.produce=L,e.setAutoFreeze=$,e.setUseProxies=J,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=immer.umd.js.map
{
"name": "immer",
"version": "2.1.4",
"version": "2.1.5",
"description": "Create your next immutable state by mutating the current one",

@@ -12,3 +12,2 @@ "main": "dist/immer.js",

"scripts": {
"watch": "jest --watch",
"test": "jest",

@@ -60,6 +59,7 @@ "test:perf": "NODE_ENV=production yarn-or-npm build && cd __performance_tests__ && babel-node add-data.js && babel-node todo.js && babel-node incremental.js",

"deep-freeze": "^0.0.1",
"expect": "^24.7.1",
"flow-bin": "^0.68.0",
"husky": "^1.2.0",
"immutable": "^3.8.2",
"jest": "^24.4.0",
"jest": "^24.7.1",
"lodash": "^4.17.4",

@@ -66,0 +66,0 @@ "lodash.clonedeep": "^4.5.0",

@@ -43,2 +43,3 @@ <img src="images/immer-logo.png" height="200px" align="right"/>

- [Talk](https://www.youtube.com/watch?v=-gJbS7YjcSo) + [slides](http://immer.surge.sh/) on Immer at React Finland 2018 by Michel Weststrate
- [ForwardJS 2019: Immutability is Changing - From Immutable.js to Immer](https://www.youtube.com/watch?v=bFuRvcAEiHg&feature=youtu.be) by [shawn swyx wang](https://twitter.com/swyx/)

@@ -529,4 +530,20 @@ ## API

Only plain objects and arrays are automatically drafted by Immer. This means other object types should never be mutated **unless** you added the exported `immerable` symbol to the object itself, its prototype, or its class constructor. In that case, the object is essentially an immutable plain object with a custom prototype.
Plain objects and arrays are always drafted by Immer.
Every other object must use the `immerable` symbol to mark itself as compatible with Immer. When one of these objects is mutated within a producer, its prototype is preserved between copies.
```js
import {immerable} from 'immer'
class Foo {
[immerable] = true // Option 1
constructor() {
this[immerable] = true // Option 2
}
}
Foo[immerable] = true // Option 3
```
For arrays, only numeric properties and the `length` property can be mutated. Custom properties are not preserved on arrays.

@@ -644,2 +661,3 @@

- [redux-ts-utils](https://github.com/knpwrs/redux-ts-utils) _Everything you need to create type-safe applications with Redux with a strong emphasis on simplicity_
- [react-state-tree](https://github.com/suchipi/react-state-tree) _Drop-in replacement for useState that persists your state into a redux-like state tree_
- ... and [many more](https://www.npmjs.com/browse/depended/immer)

@@ -646,0 +664,0 @@

@@ -62,2 +62,3 @@ export const NOTHING =

const desc = Object.getOwnPropertyDescriptor(base, key)
let {value} = desc
if (desc.get) {

@@ -67,9 +68,9 @@ if (!invokeGetters) {

}
desc.value = desc.get.call(base)
value = desc.get.call(base)
}
if (desc.enumerable) {
clone[key] = desc.value
clone[key] = value
} else {
Object.defineProperty(clone, key, {
value: desc.value,
value,
writable: true,

@@ -76,0 +77,0 @@ configurable: true

@@ -71,7 +71,20 @@ "use strict"

// Access a property without creating an Immer draft.
function peek(draft, prop) {
const state = draft[DRAFT_STATE]
if (state && !state.finalizing) {
state.finalizing = true
const value = draft[prop]
state.finalizing = false
return value
}
return draft[prop]
}
function get(state, prop) {
assertUnrevoked(state)
const value = source(state)[prop]
// Drafts are only created for proxyable values that exist in the base state.
if (!state.finalizing && value === state.base[prop] && isDraftable(value)) {
const value = peek(source(state), prop)
if (state.finalizing) return value
// Create a draft if the value is unmodified.
if (value === peek(state.base, prop) && isDraftable(value)) {
prepareCopy(state)

@@ -87,3 +100,3 @@ return (state.copy[prop] = createProxy(value, state))

if (!state.modified) {
if (is(source(state)[prop], value)) return
if (is(value, peek(source(state), prop))) return
markChanged(state)

@@ -90,0 +103,0 @@ prepareCopy(state)

@@ -231,5 +231,4 @@ import * as legacyProxy from "./es5"

if (!this.useProxies) {
state.finalizing = true
// Create the final copy, with added keys and without deleted keys.
state.copy = shallowCopy(state.draft, true)
state.finalizing = false
}

@@ -236,0 +235,0 @@ root = state.copy

@@ -102,2 +102,12 @@ "use strict"

// Access a property without creating an Immer draft.
function peek(draft, prop) {
const state = draft[DRAFT_STATE]
const desc = Reflect.getOwnPropertyDescriptor(
state ? source(state) : draft,
prop
)
return desc && desc.value
}
function get(state, prop) {

@@ -113,3 +123,5 @@ if (prop === DRAFT_STATE) return state

const value = source(state)[prop]
if (state.finalized || !isDraftable(value)) return value
if (state.finalized || !isDraftable(value)) {
return value
}

@@ -119,3 +131,3 @@ // Check for existing draft in modified state.

// Assigned values are never drafted. This catches any drafts we created, too.
if (value !== state.base[prop]) return value
if (value !== peek(state.base, prop)) return value
// Store drafts on the copy (when one exists).

@@ -130,2 +142,3 @@ drafts = state.copy

if (!state.modified) {
const baseValue = peek(state.base, prop)
// Optimize based on value's truthiness. Truthy values are guaranteed to

@@ -135,4 +148,4 @@ // never be undefined, so we can avoid the `in` operator. Lastly, truthy

const isUnchanged = value
? is(state.base[prop], value) || value === state.drafts[prop]
: is(state.base[prop], value) && prop in state.base
? is(baseValue, value) || value === state.drafts[prop]
: is(baseValue, value) && prop in state.base
if (isUnchanged) return true

@@ -148,3 +161,3 @@ markChanged(state)

// The `undefined` check is a fast path for pre-existing keys.
if (state.base[prop] !== undefined || prop in state.base) {
if (peek(state.base, prop) !== undefined || prop in state.base) {
state.assigned[prop] = false

@@ -157,2 +170,4 @@ markChanged(state)

// Note: We never coerce `desc.value` into an Immer draft, because we can't make
// the same guarantee in ES5 mode.
function getOwnPropertyDescriptor(state, prop) {

@@ -159,0 +174,0 @@ const owner = source(state)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet