Socket
Socket
Sign inDemoInstall

immer

Package Overview
Dependencies
Maintainers
1
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 1.6.0 to 1.7.0

4

changelog.md
# Changelog
### 1.7.0
* Introduced the [`nothing`](https://github.com/mweststrate/immer#producing-undefined-using-nothing) token to be able to produce the `undefined` value (which would otherwise be indistinguishable from not updating the draft at all).
### 1.6.0

@@ -4,0 +8,0 @@

@@ -98,2 +98,4 @@ // Mapped type to remove readonly modifiers from state

export const nothing: undefined
/**

@@ -100,0 +102,0 @@ * Automatically freezes any state trees generated by immer.

@@ -97,2 +97,37 @@ 'use strict';

var defineProperty = function (obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
};
var NOTHING = typeof Symbol !== "undefined" ? Symbol("immer-nothing") : defineProperty({}, "immer-nothing", true);
var PROXY_STATE = typeof Symbol !== "undefined" ? Symbol("immer-proxy-state") : "__$immer_state";

@@ -700,14 +735,21 @@

var returnValue = producer(baseState);
return returnValue === undefined ? baseState : returnValue;
return returnValue === undefined ? baseState : normalizeResult(returnValue);
}
if (!isProxyable(baseState)) throw new Error("the first argument to an immer producer should be a primitive, plain object or array, got " + (typeof baseState === "undefined" ? "undefined" : _typeof(baseState)) + ": \"" + baseState + "\"");
return getUseProxies() ? produceProxy(baseState, producer, patchListener) : produceEs5(baseState, producer, patchListener);
return normalizeResult(getUseProxies() ? produceProxy(baseState, producer, patchListener) : produceEs5(baseState, producer, patchListener));
}
function normalizeResult(result) {
return result === NOTHING ? undefined : result;
}
var applyPatches$1 = produce(applyPatches);
var nothing = NOTHING;
exports.produce = produce;
exports['default'] = produce;
exports.applyPatches = applyPatches$1;
exports.nothing = nothing;
exports.setAutoFreeze = setAutoFreeze;

@@ -714,0 +756,0 @@ exports.setUseProxies = setUseProxies;

@@ -93,2 +93,37 @@ function generatePatches(state, basepath, patches, inversePatches, baseValue, resultValue) {

var defineProperty = function (obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
};
var NOTHING = typeof Symbol !== "undefined" ? Symbol("immer-nothing") : defineProperty({}, "immer-nothing", true);
var PROXY_STATE = typeof Symbol !== "undefined" ? Symbol("immer-proxy-state") : "__$immer_state";

@@ -696,13 +731,19 @@

var returnValue = producer(baseState);
return returnValue === undefined ? baseState : returnValue;
return returnValue === undefined ? baseState : normalizeResult(returnValue);
}
if (!isProxyable(baseState)) throw new Error("the first argument to an immer producer should be a primitive, plain object or array, got " + (typeof baseState === "undefined" ? "undefined" : _typeof(baseState)) + ": \"" + baseState + "\"");
return getUseProxies() ? produceProxy(baseState, producer, patchListener) : produceEs5(baseState, producer, patchListener);
return normalizeResult(getUseProxies() ? produceProxy(baseState, producer, patchListener) : produceEs5(baseState, producer, patchListener));
}
function normalizeResult(result) {
return result === NOTHING ? undefined : result;
}
var applyPatches$1 = produce(applyPatches);
export { produce, applyPatches$1 as applyPatches, setAutoFreeze, setUseProxies, original };
var nothing = NOTHING;
export { produce, applyPatches$1 as applyPatches, nothing, setAutoFreeze, setUseProxies, original };
export default produce;
//# sourceMappingURL=immer.module.js.map

2

dist/immer.umd.js

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

var e,r;e=this,r=function(e){"use strict";function r(e,r,n,t,o,i){var a,f,u,c,p;n&&(Array.isArray(o)?function(e,r,n,t,o,i){for(var a=Math.min(o.length,i.length),f=0;f<a;f++)if(e.assigned[f]&&o[f]!==i[f]){var u=r.concat(f);n.push({op:"replace",path:u,value:i[f]}),t.push({op:"replace",path:u,value:o[f]})}if(a<i.length){for(var c=a;c<i.length;c++){var p=r.concat(c);n.push({op:"add",path:p,value:i[c]})}t.push({op:"replace",path:r.concat("length"),value:o.length})}else if(a<o.length){n.push({op:"replace",path:r.concat("length"),value:i.length});for(var s=a;s<o.length;s++){var d=r.concat(s);t.push({op:"add",path:d,value:o[s]})}}}(e,r,n,t,o,i):(a=r,f=n,u=t,c=o,p=i,d(e.assigned,function(e,r){var n=c[e],t=p[e],o=r?e in c?"replace":"add":"remove";if(n!==c||"replace"!==o){var i=a.concat(e);f.push("remove"===o?{op:o,path:i}:{op:o,path:i,value:t}),u.push("add"===o?{op:"remove",path:i}:"remove"===o?{op:"add",path:i,value:n}:{op:"replace",path:i,value:n})}})))}var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t="undefined"!=typeof Symbol?Symbol("immer-proxy-state"):"__$immer_state",o="An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.";var i=!("undefined"!=typeof process&&"production"===process.env.NODE_ENV||"verifyMinified"!==function(){}.name),a="undefined"!=typeof Proxy;function f(e){return!!e&&!!e[t]}function u(e){if(!e)return!1;if("object"!==(void 0===e?"undefined":n(e)))return!1;if(Array.isArray(e))return!0;var r=Object.getPrototypeOf(e);return null===r||r===Object.prototype}function c(e){return i&&Object.freeze(e),e}var p=Object.assign||function(e,r){for(var n in r)l(r,n)&&(e[n]=r[n]);return e};function s(e){if(Array.isArray(e))return e.slice();var r=void 0===e.__proto__?Object.create(null):{};return p(r,e)}function d(e,r){if(Array.isArray(e))for(var n=0;n<e.length;n++)r(n,e[n]);else for(var t in e)r(t,e[t])}function l(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function h(e,n,o,i){if(f(e)){var p=e[t];if(!0===p.modified){if(!0===p.finalized)return p.copy;p.finalized=!0;var y=(v=a?p.copy:p.copy=s(e),m=n,b=o,w=i,O=(g=p).base,d(v,function(e,r){if(r!==O[e]){var n=b&&!l(g.assigned,e);v[e]=h(r,n&&m.concat(e),n&&b,w)}}),c(v));return r(p,n,o,i,p.base,y),y}return p.base}var v,g,m,b,w,O;return function e(r){if(!u(r))return;if(Object.isFrozen(r))return;d(r,function(n,t){f(t)?r[n]=h(t):e(t)});c(r)}(e),e}function y(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var v=null,g={get:function(e,r){if(r===t)return e;if(e.modified){var n=e.copy[r];return n===e.base[r]&&u(n)?e.copy[r]=O(e,n):n}if(l(e.proxies,r))return e.proxies[r];var o=e.base[r];return!f(o)&&u(o)?e.proxies[r]=O(e,o):o},has:function(e,r){return r in b(e)},ownKeys:function(e){return Reflect.ownKeys(b(e))},set:function(e,r,n){if(e.assigned[r]=!0,!e.modified){if(r in e.base&&y(e.base[r],n)||l(e.proxies,r)&&e.proxies[r]===n)return!0;w(e)}return e.copy[r]=n,!0},deleteProperty:function(e,r){return e.assigned[r]=!1,w(e),delete e.copy[r],!0},getOwnPropertyDescriptor:function(e,r){var n=e.modified?e.copy:l(e.proxies,r)?e.proxies:e.base,t=Reflect.getOwnPropertyDescriptor(n,r);!t||Array.isArray(n)&&"length"===r||(t.configurable=!0);return t},defineProperty:function(){throw new Error("Immer does not support defining properties on draft objects.")},setPrototypeOf:function(){throw new Error("Immer does not support `setPrototypeOf()`.")}},m={};function b(e){return!0===e.modified?e.copy:e.base}function w(e){e.modified||(e.modified=!0,e.copy=s(e.base),Object.assign(e.copy,e.proxies),e.parent&&w(e.parent))}function O(e,r,n){if(f(r))throw new Error("Immer bug. Plz report.");var t={modified:!1,assigned:{},finalized:!1,parent:e,base:r,copy:void 0,proxies:{}},o=Array.isArray(r)?Proxy.revocable([t],m):Proxy.revocable(t,g);return v.push(o),o.proxy}d(g,function(e,r){m[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}}),m.deleteProperty=function(e,r){if(isNaN(parseInt(r)))throw new Error("Immer does not support deleting properties from arrays: "+r);return g.deleteProperty.call(this,e[0],r)},m.set=function(e,r,n){if("length"!==r&&isNaN(parseInt(r)))throw new Error("Immer does not support setting non-numeric properties on arrays: "+r);return g.set.call(this,e[0],r,n)};var j={},x=null;function P(e){return e.hasCopy?e.copy:e.base}function A(e){e.modified||(e.modified=!0,e.parent&&A(e.parent))}function E(e){e.hasCopy||(e.hasCopy=!0,e.copy=s(e.base))}function k(e,r){var n=s(r);d(r,function(e){var r;Object.defineProperty(n,""+e,j[r=""+e]||(j[r]={configurable:!0,enumerable:!0,get:function(){return function(e,r){z(e);var n=P(e)[r];return!e.finalizing&&n===e.base[r]&&u(n)?(E(e),e.copy[r]=k(e,n)):n}(this[t],r)},set:function(e){!function(e,r,n){if(z(e),e.assigned[r]=!0,!e.modified){if(y(P(e)[r],n))return;A(e),E(e)}e.copy[r]=n}(this[t],r,e)}}))});var o,i,a,f={modified:!1,assigned:{},hasCopy:!1,parent:e,base:r,proxy:n,copy:void 0,finished:!1,finalizing:!1,finalized:!1};return o=n,i=t,a=f,Object.defineProperty(o,i,{value:a,enumerable:!1,writable:!0}),x.push(f),n}function z(e){if(!0===e.finished)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(e.copy||e.base))}function _(e){if(e&&"object"===(void 0===e?"undefined":n(e))){var r=e[t];if(r){var o,i,a,f,u=r.proxy,c=r.base;if(Array.isArray(e)){if(S(r)){if(A(r),r.assigned.length=!0,u.length<c.length)for(var p=u.length;p<c.length;p++)r.assigned[p]=!1;else for(var s=c.length;s<u.length;s++)r.assigned[s]=!0;d(u,function(e,n){r.assigned[e]||_(n)})}}else{var l=(o=c,i=u,a=Object.keys(o),{added:(f=Object.keys(i)).filter(function(e){return-1===a.indexOf(e)}),removed:a.filter(function(e){return-1===f.indexOf(e)})}),h=l.added,y=l.removed;(h.length>0||y.length>0)&&A(r),d(h,function(e,n){r.assigned[n]=!0}),d(y,function(e,n){r.assigned[n]=!1}),d(u,function(e,n){r.assigned[e]||_(n)})}}}}function S(e){var r=e.proxy;if(r.length!==e.base.length)return!0;var n=Object.getOwnPropertyDescriptor(r,r.length-1);return!(!n||n.get)}function C(e,r,i){if(f(e)){var a=r.call(e,e);return void 0===a?e:a}var u=x;x=[];var c=i&&[],p=i&&[];try{var s=k(void 0,e),l=r.call(s,s);d(x,function(e,r){r.finalizing=!0});var v=void 0;if(void 0!==l&&l!==s){if(s[t].modified)throw new Error(o);v=h(l),c&&(c.push({op:"replace",path:[],value:v}),p.push({op:"replace",path:[],value:e}))}else i&&_(s),function(){for(var e=x.length-1;e>=0;e--){var r=x[e];!1===r.modified&&(Array.isArray(r.base)?S(r)&&A(r):(t=r,o=Object.keys(t.base),i=Object.keys(t.proxy),function(e,r){if(y(e,r))return!0;if("object"!==(void 0===e?"undefined":n(e))||null===e||"object"!==(void 0===r?"undefined":n(r))||null===r)return!1;var t=Object.keys(e),o=Object.keys(r);if(t.length!==o.length)return!1;for(var i=0;i<t.length;i++)if(!hasOwnProperty.call(r,t[i])||!y(e[t[i]],r[t[i]]))return!1;return!0}(o,i)||A(r)))}var t,o,i}(),v=h(s,[],c,p);return d(x,function(e,r){r.finished=!0}),i&&i(c,p),v}finally{x=u}}function I(e,r,i){if(arguments.length<1||arguments.length>3)throw new Error("produce expects 1 to 3 arguments, got "+arguments.length);if("function"==typeof e){if("function"==typeof r)throw new Error("if first argument is a function (curried invocation), the second argument to produce cannot be a function");var c=r,p=e;return function(){var e=arguments;return I(void 0===e[0]&&void 0!==c?c:e[0],function(r){return e[0]=r,p.apply(r,e)})}}if("function"!=typeof r)throw new Error("if first argument is not a function, the second argument to produce should be a function");if(void 0!==i&&"function"!=typeof i)throw new Error("the third argument of a producer should not be set or a function");if("object"!==(void 0===e?"undefined":n(e))||null===e){var s=r(e);return void 0===s?e:s}if(!u(e))throw new Error("the first argument to an immer producer should be a primitive, plain object or array, got "+(void 0===e?"undefined":n(e))+': "'+e+'"');return a?function(e,r,n){if(f(e)){var i=r.call(e,e);return void 0===i?e:i}var a=v;v=[];var u=n&&[],c=n&&[];try{var p=O(void 0,e),s=r.call(p,p),l=void 0;if(void 0!==s&&s!==p){if(p[t].modified)throw new Error(o);l=h(s),u&&(u.push({op:"replace",path:[],value:l}),c.push({op:"replace",path:[],value:e}))}else l=h(p,[],u,c);return d(v,function(e,r){return r.revoke()}),n&&n(u,c),l}finally{v=a}}(e,r,i):C(e,r,i)}var N=I(function(e,r){for(var n=function(n){var t=r[n];if(0===t.path.length&&"replace"===t.op)e=t.value;else{var o=t.path.slice(),i=o.pop(),a=o.reduce(function(e,r){if(!e)throw new Error("Cannot apply patch, path doesn't resolve: "+t.path.join("/"));return e[r]},e);if(!a)throw new Error("Cannot apply patch, path doesn't resolve: "+t.path.join("/"));switch(t.op){case"replace":case"add":a[i]=t.value;break;case"remove":if(Array.isArray(a)){if(i!==a.length-1)throw new Error("Remove can only remove the last key of an array, index: "+i+", length: "+a.length);a.length-=1}else delete a[i];break;default:throw new Error("Unsupported patch operation: "+t.op)}}},t=0;t<r.length;t++)n(t);return e});e.produce=I,e.default=I,e.applyPatches=N,e.setAutoFreeze=function(e){i=e},e.setUseProxies=function(e){a=e},e.original=function(e){if(e&&e[t])return e[t].base},Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(e.immer={});
var e,r;e=this,r=function(e){"use strict";function r(e,r,n,t,o,i){var a,f,u,c,p;n&&(Array.isArray(o)?function(e,r,n,t,o,i){for(var a=Math.min(o.length,i.length),f=0;f<a;f++)if(e.assigned[f]&&o[f]!==i[f]){var u=r.concat(f);n.push({op:"replace",path:u,value:i[f]}),t.push({op:"replace",path:u,value:o[f]})}if(a<i.length){for(var c=a;c<i.length;c++){var p=r.concat(c);n.push({op:"add",path:p,value:i[c]})}t.push({op:"replace",path:r.concat("length"),value:o.length})}else if(a<o.length){n.push({op:"replace",path:r.concat("length"),value:i.length});for(var s=a;s<o.length;s++){var d=r.concat(s);t.push({op:"add",path:d,value:o[s]})}}}(e,r,n,t,o,i):(a=r,f=n,u=t,c=o,p=i,v(e.assigned,function(e,r){var n=c[e],t=p[e],o=r?e in c?"replace":"add":"remove";if(n!==c||"replace"!==o){var i=a.concat(e);f.push("remove"===o?{op:o,path:i}:{op:o,path:i,value:t}),u.push("add"===o?{op:"remove",path:i}:"remove"===o?{op:"add",path:i,value:n}:{op:"replace",path:i,value:n})}})))}var n,t,o,i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},a="undefined"!=typeof Symbol?Symbol("immer-nothing"):(o=!0,(t="immer-nothing")in(n={})?Object.defineProperty(n,t,{value:o,enumerable:!0,configurable:!0,writable:!0}):n[t]=o,n),f="undefined"!=typeof Symbol?Symbol("immer-proxy-state"):"__$immer_state",u="An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.";var c=!("undefined"!=typeof process&&"production"===process.env.NODE_ENV||"verifyMinified"!==function(){}.name),p="undefined"!=typeof Proxy;function s(e){return!!e&&!!e[f]}function d(e){if(!e)return!1;if("object"!==(void 0===e?"undefined":i(e)))return!1;if(Array.isArray(e))return!0;var r=Object.getPrototypeOf(e);return null===r||r===Object.prototype}function l(e){return c&&Object.freeze(e),e}var h=Object.assign||function(e,r){for(var n in r)g(r,n)&&(e[n]=r[n]);return e};function y(e){if(Array.isArray(e))return e.slice();var r=void 0===e.__proto__?Object.create(null):{};return h(r,e)}function v(e,r){if(Array.isArray(e))for(var n=0;n<e.length;n++)r(n,e[n]);else for(var t in e)r(t,e[t])}function g(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function m(e,n,t,o){if(s(e)){var i=e[f];if(!0===i.modified){if(!0===i.finalized)return i.copy;i.finalized=!0;var a=(u=p?i.copy:i.copy=y(e),h=n,b=t,w=o,O=(c=i).base,v(u,function(e,r){if(r!==O[e]){var n=b&&!g(c.assigned,e);u[e]=m(r,n&&h.concat(e),n&&b,w)}}),l(u));return r(i,n,t,o,i.base,a),a}return i.base}var u,c,h,b,w,O;return function e(r){if(!d(r))return;if(Object.isFrozen(r))return;v(r,function(n,t){s(t)?r[n]=m(t):e(t)});l(r)}(e),e}function b(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var w=null,O={get:function(e,r){if(r===f)return e;if(e.modified){var n=e.copy[r];return n===e.base[r]&&d(n)?e.copy[r]=A(e,n):n}if(g(e.proxies,r))return e.proxies[r];var t=e.base[r];return!s(t)&&d(t)?e.proxies[r]=A(e,t):t},has:function(e,r){return r in x(e)},ownKeys:function(e){return Reflect.ownKeys(x(e))},set:function(e,r,n){if(e.assigned[r]=!0,!e.modified){if(r in e.base&&b(e.base[r],n)||g(e.proxies,r)&&e.proxies[r]===n)return!0;P(e)}return e.copy[r]=n,!0},deleteProperty:function(e,r){return e.assigned[r]=!1,P(e),delete e.copy[r],!0},getOwnPropertyDescriptor:function(e,r){var n=e.modified?e.copy:g(e.proxies,r)?e.proxies:e.base,t=Reflect.getOwnPropertyDescriptor(n,r);!t||Array.isArray(n)&&"length"===r||(t.configurable=!0);return t},defineProperty:function(){throw new Error("Immer does not support defining properties on draft objects.")},setPrototypeOf:function(){throw new Error("Immer does not support `setPrototypeOf()`.")}},j={};function x(e){return!0===e.modified?e.copy:e.base}function P(e){e.modified||(e.modified=!0,e.copy=y(e.base),Object.assign(e.copy,e.proxies),e.parent&&P(e.parent))}function A(e,r,n){if(s(r))throw new Error("Immer bug. Plz report.");var t={modified:!1,assigned:{},finalized:!1,parent:e,base:r,copy:void 0,proxies:{}},o=Array.isArray(r)?Proxy.revocable([t],j):Proxy.revocable(t,O);return w.push(o),o.proxy}v(O,function(e,r){j[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}}),j.deleteProperty=function(e,r){if(isNaN(parseInt(r)))throw new Error("Immer does not support deleting properties from arrays: "+r);return O.deleteProperty.call(this,e[0],r)},j.set=function(e,r,n){if("length"!==r&&isNaN(parseInt(r)))throw new Error("Immer does not support setting non-numeric properties on arrays: "+r);return O.set.call(this,e[0],r,n)};var E={},k=null;function z(e){return e.hasCopy?e.copy:e.base}function S(e){e.modified||(e.modified=!0,e.parent&&S(e.parent))}function _(e){e.hasCopy||(e.hasCopy=!0,e.copy=y(e.base))}function C(e,r){var n=y(r);v(r,function(e){var r;Object.defineProperty(n,""+e,E[r=""+e]||(E[r]={configurable:!0,enumerable:!0,get:function(){return function(e,r){I(e);var n=z(e)[r];return!e.finalizing&&n===e.base[r]&&d(n)?(_(e),e.copy[r]=C(e,n)):n}(this[f],r)},set:function(e){!function(e,r,n){if(I(e),e.assigned[r]=!0,!e.modified){if(b(z(e)[r],n))return;S(e),_(e)}e.copy[r]=n}(this[f],r,e)}}))});var t,o,i,a={modified:!1,assigned:{},hasCopy:!1,parent:e,base:r,proxy:n,copy:void 0,finished:!1,finalizing:!1,finalized:!1};return t=n,o=f,i=a,Object.defineProperty(t,o,{value:i,enumerable:!1,writable:!0}),k.push(a),n}function I(e){if(!0===e.finished)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(e.copy||e.base))}function N(e){if(e&&"object"===(void 0===e?"undefined":i(e))){var r=e[f];if(r){var n,t,o,a,u=r.proxy,c=r.base;if(Array.isArray(e)){if(D(r)){if(S(r),r.assigned.length=!0,u.length<c.length)for(var p=u.length;p<c.length;p++)r.assigned[p]=!1;else for(var s=c.length;s<u.length;s++)r.assigned[s]=!0;v(u,function(e,n){r.assigned[e]||N(n)})}}else{var d=(n=c,t=u,o=Object.keys(n),{added:(a=Object.keys(t)).filter(function(e){return-1===o.indexOf(e)}),removed:o.filter(function(e){return-1===a.indexOf(e)})}),l=d.added,h=d.removed;(l.length>0||h.length>0)&&S(r),v(l,function(e,n){r.assigned[n]=!0}),v(h,function(e,n){r.assigned[n]=!1}),v(u,function(e,n){r.assigned[e]||N(n)})}}}}function D(e){var r=e.proxy;if(r.length!==e.base.length)return!0;var n=Object.getOwnPropertyDescriptor(r,r.length-1);return!(!n||n.get)}function M(e,r,n){if(s(e)){var t=r.call(e,e);return void 0===t?e:t}var o=k;k=[];var a=n&&[],c=n&&[];try{var p=C(void 0,e),d=r.call(p,p);v(k,function(e,r){r.finalizing=!0});var l=void 0;if(void 0!==d&&d!==p){if(p[f].modified)throw new Error(u);l=m(d),a&&(a.push({op:"replace",path:[],value:l}),c.push({op:"replace",path:[],value:e}))}else n&&N(p),function(){for(var e=k.length-1;e>=0;e--){var r=k[e];!1===r.modified&&(Array.isArray(r.base)?D(r)&&S(r):(n=r,t=Object.keys(n.base),o=Object.keys(n.proxy),function(e,r){if(b(e,r))return!0;if("object"!==(void 0===e?"undefined":i(e))||null===e||"object"!==(void 0===r?"undefined":i(r))||null===r)return!1;var n=Object.keys(e),t=Object.keys(r);if(n.length!==t.length)return!1;for(var o=0;o<n.length;o++)if(!hasOwnProperty.call(r,n[o])||!b(e[n[o]],r[n[o]]))return!1;return!0}(t,o)||S(r)))}var n,t,o}(),l=m(p,[],a,c);return v(k,function(e,r){r.finished=!0}),n&&n(a,c),l}finally{k=o}}function R(e,r,n){if(arguments.length<1||arguments.length>3)throw new Error("produce expects 1 to 3 arguments, got "+arguments.length);if("function"==typeof e){if("function"==typeof r)throw new Error("if first argument is a function (curried invocation), the second argument to produce cannot be a function");var t=r,o=e;return function(){var e=arguments;return R(void 0===e[0]&&void 0!==t?t:e[0],function(r){return e[0]=r,o.apply(r,e)})}}if("function"!=typeof r)throw new Error("if first argument is not a function, the second argument to produce should be a function");if(void 0!==n&&"function"!=typeof n)throw new Error("the third argument of a producer should not be set or a function");if("object"!==(void 0===e?"undefined":i(e))||null===e){var a=r(e);return void 0===a?e:F(a)}if(!d(e))throw new Error("the first argument to an immer producer should be a primitive, plain object or array, got "+(void 0===e?"undefined":i(e))+': "'+e+'"');return F(p?function(e,r,n){if(s(e)){var t=r.call(e,e);return void 0===t?e:t}var o=w;w=[];var i=n&&[],a=n&&[];try{var c=A(void 0,e),p=r.call(c,c),d=void 0;if(void 0!==p&&p!==c){if(c[f].modified)throw new Error(u);d=m(p),i&&(i.push({op:"replace",path:[],value:d}),a.push({op:"replace",path:[],value:e}))}else d=m(c,[],i,a);return v(w,function(e,r){return r.revoke()}),n&&n(i,a),d}finally{w=o}}(e,r,n):M(e,r,n))}function F(e){return e===a?void 0:e}var K=R(function(e,r){for(var n=function(n){var t=r[n];if(0===t.path.length&&"replace"===t.op)e=t.value;else{var o=t.path.slice(),i=o.pop(),a=o.reduce(function(e,r){if(!e)throw new Error("Cannot apply patch, path doesn't resolve: "+t.path.join("/"));return e[r]},e);if(!a)throw new Error("Cannot apply patch, path doesn't resolve: "+t.path.join("/"));switch(t.op){case"replace":case"add":a[i]=t.value;break;case"remove":if(Array.isArray(a)){if(i!==a.length-1)throw new Error("Remove can only remove the last key of an array, index: "+i+", length: "+a.length);a.length-=1}else delete a[i];break;default:throw new Error("Unsupported patch operation: "+t.op)}}},t=0;t<r.length;t++)n(t);return e}),U=a;e.produce=R,e.default=R,e.applyPatches=K,e.nothing=U,e.setAutoFreeze=function(e){c=e},e.setUseProxies=function(e){p=e},e.original=function(e){if(e&&e[f])return e[f].base},Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(e.immer={});
//# sourceMappingURL=immer.umd.js.map
{
"name": "immer",
"version": "1.6.0",
"version": "1.7.0",
"description": "Create your next immutable state by mutating the current one",

@@ -5,0 +5,0 @@ "main": "dist/immer.js",

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

_Create the next immutable state tree by simply modifying the current tree_
Did Immer make a difference to your project? Consider buying me a coffee!<br/><a href="https://www.buymeacoffee.com/mweststrate" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
_Create the next immutable state tree by simply modifying the current tree_
---

@@ -25,3 +25,5 @@

* [Talk](https://www.youtube.com/watch?v=-gJbS7YjcSo) + [slides](http://immer.surge.sh/) on Immer at React Finland 2018 by Michel Weststrate
* Blog by Workday Prism on why they picked Immer to manage immutable state [The Search for a Strongly-Typed, Immutable State](https://medium.com/workday-engineering/workday-prism-analytics-the-search-for-a-strongly-typed-immutable-state-a09f6768b2b5)
* Blog: by Workday Prism on why they picked Immer to manage immutable state [The Search for a Strongly-Typed, Immutable State](https://medium.com/workday-engineering/workday-prism-analytics-the-search-for-a-strongly-typed-immutable-state-a09f6768b2b5)
* Blog: [The Rise of Immer in React](https://www.netlify.com/blog/2018/09/12/the-rise-of-immer-in-react/)
* Blog: [Immutability in React and Redux: The Complete Guide](https://daveceddia.com/react-redux-immutability-guide/)

@@ -356,2 +358,56 @@ Immer (German for: always) is a tiny package that allows you to work with immutable state in a more convenient way. It is based on the [_copy-on-write_](https://en.wikipedia.org/wiki/Copy-on-write) mechanism.

_Note: It is not possible to return `undefined` this way, as it is indistiguishable from *not* updating the draft! Read on..._
## Producing `undefined` using `nothing`
So, in general one can replace the current state by just `return`ing a new value from the producer, rather than modifying the draft.
There is a subtle edge case however: if you try to write a producer that wants to replace the current state with `undefined`:
```javascript
produce({}, draft => {
// don't do anything
})
```
Versus:
```javascript
produce({}, draft => {
// Try to return undefined from the producer
return undefined
})
```
The problem is that in JavaScript a function that doesn't return anything, also returns `undefined`!
So immer cannot differentiate between those different cases.
So, by default, Immer will assume that any producer that returns `undefined` just tried to modify the draft.
However, to make it clear to Immer that you intentionally want to produce the value `undefined`, you can return the built-in token `nothing`:
```javascript
import produce, { nothing } from "immer"
const state = {
hello: "world"
}
produce(state, (draft) => {
// no-op
})
// Returns the original state: { hello: "world"}
produce(state, (draft) => undefined)
// Returns the original state: { hello: "world"}
produce(state, (draft) => {
return nothing
})
// Produces a new state, 'undefined'
produce(state, (draft) => nothing)
// Produces a new state, 'undefined'
```
N.B. Note that this problem is specific for the `undefined` value, any other value, including `null`, doesn't suffer from this issue.
## Extracting the original object from a proxied instance

@@ -403,3 +459,3 @@

Code style is highly personal, but for code bases that are to be understood by many, we recommend to stick to the classic `draft = { draft.user.age += 1}` to avoid cognitive overhead.
Code style is highly personal, but for code bases that are to be understood by many, we recommend to stick to the classic `draft => { draft.user.age += 1}` to avoid cognitive overhead.

@@ -413,6 +469,6 @@ ## TypeScript or Flow

```ts
import produce from 'immer';
import produce from 'immer'
interface State {
readonly x: number;
readonly x: number
}

@@ -422,9 +478,9 @@

const state: State = {
x: 0;
};
x: 0
}
const newState = produce<State>(state, draft => {
// `x` can be modified here
draft.x++;
});
draft.x++
})

@@ -456,13 +512,22 @@ // `newState.x` cannot be modified here

## Limitations / pitfalls
## Limitations
Immer supports the following types of data:
1. All kinds of primitive values
1. `Date` instances, but: only if not mutated, see below
1. Arrays, but: non-standard attributes are not supported (like: `array.test = "Test"`)
1. Plain objects (objects that have as prototype either `null` or `Object`)
1. Functions, assuming they aren't mutated
1. Other value types (like class instances) can be stored in the tree, but note that Immer won't work _inside_ those objects. In other words, if you modify a class instance, this will _not_ result in clone and unmodified original, but just in a modified original.
# Pitfalls
1. Don't redefine draft like, `draft = myCoolNewState`. Instead, either modify the `draft` or return a new state. See [Returning data from producers](#returning-data-from-producers).
1. Immer only supports plain objects and arrays. For arrays it is assumed only numeric properties (and length) are used.
1. Immer assumes your state to be a unidirectional tree. That is, no object should appear twice and there should be no circular references.
1. Class instances are not, and will not be supported. Read here more on why that would be a [confusing, conceptual mismatch]
1. Immer only processes native arrays and plain objects (with a prototype of `null` or `Object`). Any other type of value will be treated verbatim! So if you modify a `Map` or `Buffer` (or whatever complex object from the draft state), the changes will be persisted. But, both in your new and old state! So, in such cases, make sure to always produce fresh instances if you want to keep your state truly immutable.
1. Immer assumes your state to be a unidirectional tree. That is, no object should appear twice in the tree, and there should be no circular references.
1. Class instances are not, and will not supported first-class supported. Read [here](https://github.com/mweststrate/immer/issues/155#issuecomment-407725592) why classes are a conceptual mismatch (and technically extremely challenging)
1. For example, working with `Date` objects is no problem, just make sure you never modify them (by using methods like `setYear` on an existing instance). Instead, always create fresh `Date` instances. Which is probably what you were unconsciously doing already.
1. Since Immer uses proxies, reading huge amounts of data from state comes with an overhead (especially in the ES5 implementation). If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the producer function or read from the `currentState` rather than the `draftState`. Also realize that immer is opt-in everywhere, so it is perfectly fine to manually write super performance critical reducers, and use immer for all the normal ones.
1. Since Immer uses proxies, reading huge amounts of data from state comes with an overhead (especially in the ES5 implementation). If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the producer function or read from the `currentState` rather than the `draftState`. Also realize that immer is opt-in everywhere, so it is perfectly fine to manually write super performance critical reducers, and use immer for all the normal ones. Also note that `original` can be used to get the original state of an object, which is cheaper to read.
1. Some debuggers (at least Node 6 is known) have trouble debugging when Proxies are in play. Node 8 is known to work correctly.
1. Always try to pull `produce` 'up', for example `for (let x of y) produce(base, d => d.push(x))` is exponentially slower then `produce(base, d => { for (let x of y) d.push(x)})`
1. Always try to pull `produce` 'up', for example `for (let x of y) produce(base, d => d.push(x))` is exponentially slower than `produce(base, d => { for (let x of y) d.push(x)})`
1. It is possible to return values from producers, except, it is not possible to return `undefined` that way, as it is indistiguishable from not updating the draft at all! If you want to replace the draft with `undefined`, just return `nothing` from the producer.
1. Immer does not support built in data-structures like `Map` and `Set`. However, it is fine to just immutably "update" them yourself but still leverage immer wherever possible:

@@ -509,3 +574,2 @@

## Cool things built with immer

@@ -520,2 +584,3 @@

* [immer-wieder](https://github.com/drcmda/immer-wieder#readme) _State management lib that combines React 16 Context and immer for Redux semantics_
* [robodux](https://github.com/neurosnap/robodux) _flexible way to reduce redux boilerplate
* ... and [many more](https://www.npmjs.com/browse/depended/immer)

@@ -589,3 +654,3 @@

* Immer with proxies is roughly speaking twice to three times slower as a hand written reducer (the above test case is worst case, se `yarn test:perf` for more tests). This is in practice negligible.
* Immer with proxies is roughly speaking twice to three times slower as a hand written reducer (the above test case is worst case, see `yarn test:perf` for more tests). This is in practice negligible.
* Immer is roughly as fast as ImmutableJS. However, the _immutableJS + toJS_ makes clear the cost that often needs to be paid later; converting the immutableJS objects back to plain objects, to be able to pass them to components, over the network etc... (And there is also the upfront cost of converting data received from e.g. the server to immutable JS)

@@ -592,0 +657,0 @@ * Generating patches doesn't significantly slow immer down

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc