Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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.5.0 to 1.6.0

5

changelog.md
# Changelog
### 1.6.0
* Introduced the `original(draft)` function, that, given an object from the draft, returns the original object from the base state. This can be useful if you need to do reference equality checks, or comparisons with the base state. See [#179](https://github.com/mweststrate/immer/pull/179) by [@RichieAHB](https://github.com/RichieAHB).
* Writing or removing non-numeric properties on arrays is now strictly forbidden (Immer didn't throw before, but correct behavior wasn't guaranteed either)
### 1.5.0

@@ -4,0 +9,0 @@

4

dist/immer.d.ts

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

export function applyPatches<S>(state: S, patches: Patch[]): S
export function applyPatches<S>(state: S, patches: Patch[]): S
export function original<T>(value: T): T | void

@@ -147,2 +147,9 @@ 'use strict';

function original(value) {
if (value && value[PROXY_STATE]) {
return value[PROXY_STATE].base;
}
// otherwise return undefined
}
var assign = Object.assign || function assign(target, value) {

@@ -264,2 +271,10 @@ for (var key in value) {

});
arrayTraps.deleteProperty = function (state, prop) {
if (isNaN(parseInt(prop))) throw new Error("Immer does not support deleting properties from arrays: " + prop);
return objectTraps.deleteProperty.call(this, state[0], prop);
};
arrayTraps.set = function (state, prop, value) {
if (prop !== "length" && isNaN(parseInt(prop))) throw new Error("Immer does not support setting non-numeric properties on arrays: " + prop);
return objectTraps.set.call(this, state[0], prop, value);
};

@@ -650,3 +665,3 @@ function createState(parent, base) {

* @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
* @param {Function} patchListener - optional function that will be called with all the patches produces here
* @param {Function} patchListener - optional function that will be called with all the patches produced here
* @returns {any} a new state, or the base state if nothing was modified

@@ -701,2 +716,3 @@ */

exports.setUseProxies = setUseProxies;
exports.original = original;
//# sourceMappingURL=immer.js.map

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

function original(value) {
if (value && value[PROXY_STATE]) {
return value[PROXY_STATE].base;
}
// otherwise return undefined
}
var assign = Object.assign || function assign(target, value) {

@@ -260,2 +267,10 @@ for (var key in value) {

});
arrayTraps.deleteProperty = function (state, prop) {
if (isNaN(parseInt(prop))) throw new Error("Immer does not support deleting properties from arrays: " + prop);
return objectTraps.deleteProperty.call(this, state[0], prop);
};
arrayTraps.set = function (state, prop, value) {
if (prop !== "length" && isNaN(parseInt(prop))) throw new Error("Immer does not support setting non-numeric properties on arrays: " + prop);
return objectTraps.set.call(this, state[0], prop, value);
};

@@ -646,3 +661,3 @@ function createState(parent, base) {

* @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
* @param {Function} patchListener - optional function that will be called with all the patches produces here
* @param {Function} patchListener - optional function that will be called with all the patches produced here
* @returns {any} a new state, or the base state if nothing was modified

@@ -692,4 +707,4 @@ */

export { produce, applyPatches$1 as applyPatches, setAutoFreeze, setUseProxies };
export { produce, applyPatches$1 as applyPatches, setAutoFreeze, setUseProxies, original };
export default produce;
//# sourceMappingURL=immer.module.js.map

@@ -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 v=(y=a?p.copy:p.copy=s(e),b=n,m=o,w=i,O=(g=p).base,d(y,function(e,r){if(r!==O[e]){var n=m&&!l(g.assigned,e);y[e]=h(r,n&&b.concat(e),n&&m,w)}}),c(y));return r(p,n,o,i,p.base,v),v}return p.base}var y,g,b,m,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 v(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var y=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 m(e)},ownKeys:function(e){return Reflect.ownKeys(m(e))},set:function(e,r,n){if(e.assigned[r]=!0,!e.modified){if(r in e.base&&v(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()`.")}},b={};function m(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],b):Proxy.revocable(t,g);return y.push(o),o.proxy}d(g,function(e,r){b[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}});var j={},x=null;function A(e){return e.hasCopy?e.copy:e.base}function P(e){e.modified||(e.modified=!0,e.parent&&P(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=A(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(v(A(e)[r],n))return;P(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(P(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,v=l.removed;(h.length>0||v.length>0)&&P(r),d(h,function(e,n){r.assigned[n]=!0}),d(v,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 y=void 0;if(void 0!==l&&l!==s){if(s[t].modified)throw new Error(o);y=h(l),c&&(c.push({op:"replace",path:[],value:y}),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)&&P(r):(t=r,o=Object.keys(t.base),i=Object.keys(t.proxy),function(e,r){if(v(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])||!v(e[t[i]],r[t[i]]))return!1;return!0}(o,i)||P(r)))}var t,o,i}(),y=h(s,[],c,p);return d(x,function(e,r){r.finished=!0}),i&&i(c,p),y}finally{x=u}}function D(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 D(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=y;y=[];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(y,function(e,r){return r.revoke()}),n&&n(u,c),l}finally{y=a}}(e,r,i):C(e,r,i)}var I=D(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=D,e.default=D,e.applyPatches=I,e.setAutoFreeze=function(e){i=e},e.setUseProxies=function(e){a=e},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,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={});
//# sourceMappingURL=immer.umd.js.map
{
"name": "immer",
"version": "1.5.0",
"version": "1.6.0",
"description": "Create your next immutable state by mutating the current one",

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

"types": "./dist/immer.d.ts",
"sideEffects": false,
"scripts": {

@@ -13,0 +14,0 @@ "watch": "jest --watch",

@@ -0,1 +1,3 @@

<img src="images/immer-logo.png" height="200px" align="right"/>
# Immer

@@ -5,2 +7,4 @@

_Create the next immutable state tree by simply modifying the current tree_

@@ -21,2 +25,3 @@

* [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)

@@ -351,2 +356,13 @@ 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.

## Extracting the original object from a proxied instance
Immer exposes a named export `original` that will get the original object from the proxied instance inside `produce` (or return `undefined` for unproxied values). A good example of when this can be useful is when searching for nodes in a tree-like state using strict equality.
```js
const baseState = { users: [{ name: "Richie" }] };
const nextState = produce(baseState, draftState => {
original(draftState.users) // is === baseState.users
})
```
## Using `this`

@@ -373,2 +389,18 @@

## Inline shortcuts using `void`
Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with.
In such cases you can use javascripts [`void`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void) operator, which evaluates expressions and returns `undefined`.
```javascript
// Single mutation
produce(draft => void (draft.user.age += 1))
// Multiple mutations
produce(draft => void (draft.user.age += 1, draft.user.height = 186))
```
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.
## TypeScript or Flow

@@ -422,12 +454,55 @@

## Pitfalls
## Limitations / 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. Currently, Immer only supports plain objects and arrays. PRs are welcome for more language built-in types like `Map` and `Set`.
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. 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`
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. 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. 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:
```javascript
const state = {
title: "hello",
tokenSet: new Set()
}
const nextState = produce(state, draft => {
draft.title = draft.title.toUpperCase() // let immer do it's job
// don't use the operations onSet, as that mutates the instance!
// draft.tokenSet.add("c1342")
// instead: clone the set (once!)
const newSet = new Set(draft.tokenSet)
// mutate the clone (just in this producer)
newSet.add("c1342")
// update the draft with the new set
draft.tokenSet = newSet
})
```
Or a deep update in maps (well, don't use maps for this use case, but as example):
```javascript
const state = {
users: new Map(["michel", { name: "miche" }])
}
const nextState = produce(state, draft => {
const newUsers = new Map(draft.users)
// mutate the new map and set a _new_ user object
// but leverage produce again to base the new user object on the original one
newUsers.set("michel", produce(draft.users.get("michel"), draft => {
draft.name = "michel"
}))
draft.users = newUsers
})
```
## Cool things built with immer

@@ -434,0 +509,0 @@

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