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.3.1 to 1.4.0

6

changelog.md
# Changelog
### 1.4.0
* Improved TypeScript typings; if the base state is declared as `readonly`, the `draft` object will be upcasted to a writeable version. Fixes [#97](https://github.com/mweststrate/immer/issues/97) through [#161](https://github.com/mweststrate/immer/pull/161) by [@knpwrs](https://github.com/knpwrs)
* It is now possible to use both `import produce from "immer"` and `import {produce} from "immer"`. Implements [#136](https://github.com/mweststrate/immer/issues/136)
* Added several performance tests to the repository
### 1.3.1

@@ -4,0 +10,0 @@

154

dist/immer.d.ts

@@ -1,71 +0,89 @@

/**
* Immer takes a state, and runs a function against it.
* That function can freely mutate the state, as it will create copies-on-write.
* This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.
*
* If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe
* any time it is called with the current state.
*
* @param currentState - the state to start with
* @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified
* @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefined
* @returns The next state: a new state, or the current state if nothing was modified
*/
export default function<S = any>(
currentState: S,
recipe: (this: S, draftState: S) => void | S
): S
// Mapped type to remove readonly modifiers from state
// Based on https://github.com/Microsoft/TypeScript/blob/d4dc67aab233f5a8834dff16531baf99b16fea78/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L120-L129
export type DraftObject<T> = {
-readonly [P in keyof T]: Draft<T[P]>;
};
export interface DraftArray<T> extends Array<Draft<T>> { }
export type Draft<T> =
T extends any[] ? DraftArray<T[number]> :
T extends ReadonlyArray<any> ? DraftArray<T[number]> :
T extends object ? DraftObject<T> :
T;
// curried invocations with default initial state
// 0 additional arguments
export default function<S = any>(
recipe: (this: S, draftState: S) => void | S,
initialState: S
): (currentState: S | undefined) => S
// 1 additional argument of type A
export default function<S = any, A = any>(
recipe: (this: S, draftState: S, a: A) => void | S,
initialState: S
): (currentState: S | undefined, a: A) => S
// 2 additional arguments of types A and B
export default function<S = any, A = any, B = any>(
recipe: (this: S, draftState: S, a: A, b: B) => void | S,
initialState: S
): (currentState: S | undefined, a: A, b: B) => S
// 3 additional arguments of types A, B and C
export default function<S = any, A = any, B = any, C = any>(
recipe: (this: S, draftState: S, a: A, b: B, c: C) => void | S,
initialState: S
): (currentState: S | undefined, a: A, b: B, c: C) => S
// any number of additional arguments, but with loss of type safety
// this may be alleviated if "variadic kinds" makes it into Typescript:
// https://github.com/Microsoft/TypeScript/issues/5453
export default function<S = any>(
recipe: (this: S, draftState: S, ...extraArgs: any[]) => void | S,
initialState: S
): (currentState: S | undefined, ...extraArgs: any[]) => S
export interface IProduce {
/**
* Immer takes a state, and runs a function against it.
* That function can freely mutate the state, as it will create copies-on-write.
* This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.
*
* If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe
* any time it is called with the current state.
*
* @param currentState - the state to start with
* @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified
* @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefined
* @returns The next state: a new state, or the current state if nothing was modified
*/
<S = any>(
currentState: S,
recipe: (this: Draft<S>, draftState: Draft<S>) => void | S
): S
// curried invocations without default initial state
// 0 additional arguments
export default function<S = any>(
recipe: (this: S, draftState: S) => void | S
): (currentState: S) => S
// 1 additional argument of type A
export default function<S = any, A = any>(
recipe: (this: S, draftState: S, a: A) => void | S
): (currentState: S, a: A) => S
// 2 additional arguments of types A and B
export default function<S = any, A = any, B = any>(
recipe: (this: S, draftState: S, a: A, b: B) => void | S
): (currentState: S, a: A, b: B) => S
// 3 additional arguments of types A, B and C
export default function<S = any, A = any, B = any, C = any>(
recipe: (this: S, draftState: S, a: A, b: B, c: C) => void | S
): (currentState: S, a: A, b: B, c: C) => S
// any number of additional arguments, but with loss of type safety
// this may be alleviated if "variadic kinds" makes it into Typescript:
// https://github.com/Microsoft/TypeScript/issues/5453
export default function<S = any>(
recipe: (this: S, draftState: S, ...extraArgs: any[]) => void | S
): (currentState: S, ...extraArgs: any[]) => S
// curried invocations with default initial state
// 0 additional arguments
<S = any>(
recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,
initialState: S
): (currentState: S | undefined) => S
// 1 additional argument of type A
<S = any, A = any>(
recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S,
initialState: S
): (currentState: S | undefined, a: A) => S
// 2 additional arguments of types A and B
<S = any, A = any, B = any>(
recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S,
initialState: S
): (currentState: S | undefined, a: A, b: B) => S
// 3 additional arguments of types A, B and C
<S = any, A = any, B = any, C = any>(
recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S,
initialState: S
): (currentState: S | undefined, a: A, b: B, c: C) => S
// any number of additional arguments, but with loss of type safety
// this may be alleviated if "variadic kinds" makes it into Typescript:
// https://github.com/Microsoft/TypeScript/issues/5453
<S = any>(
recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S,
initialState: S
): (currentState: S | undefined, ...extraArgs: any[]) => S
// curried invocations without default initial state
// 0 additional arguments
<S = any>(
recipe: (this: Draft<S>, draftState: Draft<S>) => void | S
): (currentState: S) => S
// 1 additional argument of type A
<S = any, A = any>(
recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S
): (currentState: S, a: A) => S
// 2 additional arguments of types A and B
<S = any, A = any, B = any>(
recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S
): (currentState: S, a: A, b: B) => S
// 3 additional arguments of types A, B and C
<S = any, A = any, B = any, C = any>(
recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S
): (currentState: S, a: A, b: B, c: C) => S
// any number of additional arguments, but with loss of type safety
// this may be alleviated if "variadic kinds" makes it into Typescript:
// https://github.com/Microsoft/TypeScript/issues/5453
<S = any>(
recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S
): (currentState: S, ...extraArgs: any[]) => S
}
export const produce: IProduce
export default produce
/**

@@ -72,0 +90,0 @@ * Automatically freezes any state trees generated by immer.

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

exports.produce = produce;
exports['default'] = produce;

@@ -526,0 +527,0 @@ exports.setAutoFreeze = setAutoFreeze;

@@ -520,4 +520,4 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {

export { setAutoFreeze, setUseProxies };
export { produce, setAutoFreeze, setUseProxies };
export default produce;
//# sourceMappingURL=immer.module.js.map

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

var e,r;e=this,r=function(e){"use strict";var r="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},n="undefined"!=typeof Symbol?Symbol("immer-proxy-state"):"__$immer_state",t="An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.";var o=!("undefined"!=typeof process&&"production"===process.env.NODE_ENV||"verifyMinified"!==function(){}.name),i="undefined"!=typeof Proxy;function f(e){return!!e&&!!e[n]}function u(e){if(!e)return!1;if("object"!==(void 0===e?"undefined":r(e)))return!1;if(Array.isArray(e))return!0;var n=Object.getPrototypeOf(e);return null===n||n===Object.prototype}function a(e){return o&&Object.freeze(e),e}var c=Object.assign||function(e,r){for(var n in r)p(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 c(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 p(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function y(e){if(f(e)){var r=e[n];return!0===r.modified?!0===r.finalized?r.copy:(r.finalized=!0,t=i?r.copy:r.copy=s(e),o=r.base,d(t,function(e,r){r!==o[e]&&(t[e]=y(r))}),a(t)):r.base}var t,o;return function e(r){if(!u(r))return;if(Object.isFrozen(r))return;d(r,function(n,t){f(t)?r[n]=y(t):e(t)});a(r)}(e),e}function l(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var v=null,b={get:function(e,r){if(r===n)return e;if(e.modified){var t=e.copy[r];return t===e.base[r]&&u(t)?e.copy[r]=w(e,t):t}if(p(e.proxies,r))return e.proxies[r];var o=e.base[r];return!f(o)&&u(o)?e.proxies[r]=w(e,o):o},has:function(e,r){return r in h(e)},ownKeys:function(e){return Reflect.ownKeys(h(e))},set:function(e,r,n){if(!e.modified){if(r in e.base&&l(e.base[r],n)||p(e.proxies,r)&&e.proxies[r]===n)return!0;g(e)}return e.copy[r]=n,!0},deleteProperty:function(e,r){return g(e),delete e.copy[r],!0},getOwnPropertyDescriptor:function(e,r){var n=e.modified?e.copy:p(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 h(e){return!0===e.modified?e.copy:e.base}function g(e){e.modified||(e.modified=!0,e.copy=s(e.base),Object.assign(e.copy,e.proxies),e.parent&&g(e.parent))}function w(e,r){if(f(r))throw new Error("Immer bug. Plz report.");var n={modified:!1,finalized:!1,parent:e,base:r,copy:void 0,proxies:{}},t=Array.isArray(r)?Proxy.revocable([n],m):Proxy.revocable(n,b);return v.push(t),t.proxy}d(b,function(e,r){m[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}});var O={},j=null;function x(e){return e.hasCopy?e.copy:e.base}function P(e){e.modified||(e.modified=!0,e.parent&&P(e.parent))}function A(e){e.hasCopy||(e.hasCopy=!0,e.copy=s(e.base))}function E(e,r){var t=s(r);d(r,function(e){var r;Object.defineProperty(t,""+e,O[r=""+e]||(O[r]={configurable:!0,enumerable:!0,get:function(){return function(e,r){z(e);var n=x(e)[r];return!e.finalizing&&n===e.base[r]&&u(n)?(A(e),e.copy[r]=E(e,n)):n}(this[n],r)},set:function(e){!function(e,r,n){if(z(e),!e.modified){if(l(x(e)[r],n))return;P(e),A(e)}e.copy[r]=n}(this[n],r,e)}}))});var o,i,f,a={modified:!1,hasCopy:!1,parent:e,base:r,proxy:t,copy:void 0,finished:!1,finalizing:!1,finalized:!1};return o=t,i=n,f=a,Object.defineProperty(o,i,{value:f,enumerable:!1,writable:!0}),j.push(a),t}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){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 S(e,o){if(f(e)){var i=o.call(e,e);return void 0===i?e:i}var u=j;j=[];try{var a=E(void 0,e),c=o.call(a,a);d(j,function(e,r){r.finalizing=!0}),function(){for(var e=j.length-1;e>=0;e--){var n=j[e];!1===n.modified&&(Array.isArray(n.base)?_(n)&&P(n):(t=n,o=Object.keys(t.base),i=Object.keys(t.proxy),function(e,n){if(l(e,n))return!0;if("object"!==(void 0===e?"undefined":r(e))||null===e||"object"!==(void 0===n?"undefined":r(n))||null===n)return!1;var t=Object.keys(e),o=Object.keys(n);if(t.length!==o.length)return!1;for(var i=0;i<t.length;i++)if(!hasOwnProperty.call(n,t[i])||!l(e[t[i]],n[t[i]]))return!1;return!0}(o,i)||P(n)))}var t,o,i}();var s=void 0;if(void 0!==c&&c!==a){if(a[n].modified)throw new Error(t);s=y(c)}else s=y(a);return d(j,function(e,r){r.finished=!0}),s}finally{j=u}}e.default=function e(o,a){if(1!==arguments.length&&2!==arguments.length)throw new Error("produce expects 1 or 2 arguments, got "+arguments.length);if("function"==typeof o){if("function"==typeof a)throw new Error("if first argument is a function (curried invocation), the second argument to produce cannot be a function");var c=a,s=o;return function(){var r=arguments;return e(void 0===r[0]&&void 0!==c?c:r[0],function(e){return r[0]=e,s.apply(e,r)})}}if("function"!=typeof a)throw new Error("if first argument is not a function, the second argument to produce should be a function");if("object"!==(void 0===o?"undefined":r(o))||null===o){var p=a(o);return void 0===p?o:p}if(!u(o))throw new Error("the first argument to an immer producer should be a primitive, plain object or array, got "+(void 0===o?"undefined":r(o))+': "'+o+'"');return i?function(e,r){if(f(e)){var o=r.call(e,e);return void 0===o?e:o}var i=v;v=[];try{var u=w(void 0,e),a=r.call(u,u),c=void 0;if(void 0!==a&&a!==u){if(u[n].modified)throw new Error(t);c=y(a)}else c=y(u);return d(v,function(e,r){return r.revoke()}),c}finally{v=i}}(o,a):S(o,a)},e.setAutoFreeze=function(e){o=e},e.setUseProxies=function(e){i=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";var r="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},n="undefined"!=typeof Symbol?Symbol("immer-proxy-state"):"__$immer_state",t="An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.";var o=!("undefined"!=typeof process&&"production"===process.env.NODE_ENV||"verifyMinified"!==function(){}.name),i="undefined"!=typeof Proxy;function f(e){return!!e&&!!e[n]}function u(e){if(!e)return!1;if("object"!==(void 0===e?"undefined":r(e)))return!1;if(Array.isArray(e))return!0;var n=Object.getPrototypeOf(e);return null===n||n===Object.prototype}function a(e){return o&&Object.freeze(e),e}var c=Object.assign||function(e,r){for(var n in r)p(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 c(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 p(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function y(e){if(f(e)){var r=e[n];return!0===r.modified?!0===r.finalized?r.copy:(r.finalized=!0,t=i?r.copy:r.copy=s(e),o=r.base,d(t,function(e,r){r!==o[e]&&(t[e]=y(r))}),a(t)):r.base}var t,o;return function e(r){if(!u(r))return;if(Object.isFrozen(r))return;d(r,function(n,t){f(t)?r[n]=y(t):e(t)});a(r)}(e),e}function l(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var v=null,b={get:function(e,r){if(r===n)return e;if(e.modified){var t=e.copy[r];return t===e.base[r]&&u(t)?e.copy[r]=w(e,t):t}if(p(e.proxies,r))return e.proxies[r];var o=e.base[r];return!f(o)&&u(o)?e.proxies[r]=w(e,o):o},has:function(e,r){return r in h(e)},ownKeys:function(e){return Reflect.ownKeys(h(e))},set:function(e,r,n){if(!e.modified){if(r in e.base&&l(e.base[r],n)||p(e.proxies,r)&&e.proxies[r]===n)return!0;g(e)}return e.copy[r]=n,!0},deleteProperty:function(e,r){return g(e),delete e.copy[r],!0},getOwnPropertyDescriptor:function(e,r){var n=e.modified?e.copy:p(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 h(e){return!0===e.modified?e.copy:e.base}function g(e){e.modified||(e.modified=!0,e.copy=s(e.base),Object.assign(e.copy,e.proxies),e.parent&&g(e.parent))}function w(e,r){if(f(r))throw new Error("Immer bug. Plz report.");var n={modified:!1,finalized:!1,parent:e,base:r,copy:void 0,proxies:{}},t=Array.isArray(r)?Proxy.revocable([n],m):Proxy.revocable(n,b);return v.push(t),t.proxy}d(b,function(e,r){m[e]=function(){return arguments[0]=arguments[0][0],r.apply(this,arguments)}});var O={},j=null;function x(e){return e.hasCopy?e.copy:e.base}function P(e){e.modified||(e.modified=!0,e.parent&&P(e.parent))}function A(e){e.hasCopy||(e.hasCopy=!0,e.copy=s(e.base))}function E(e,r){var t=s(r);d(r,function(e){var r;Object.defineProperty(t,""+e,O[r=""+e]||(O[r]={configurable:!0,enumerable:!0,get:function(){return function(e,r){z(e);var n=x(e)[r];return!e.finalizing&&n===e.base[r]&&u(n)?(A(e),e.copy[r]=E(e,n)):n}(this[n],r)},set:function(e){!function(e,r,n){if(z(e),!e.modified){if(l(x(e)[r],n))return;P(e),A(e)}e.copy[r]=n}(this[n],r,e)}}))});var o,i,f,a={modified:!1,hasCopy:!1,parent:e,base:r,proxy:t,copy:void 0,finished:!1,finalizing:!1,finalized:!1};return o=t,i=n,f=a,Object.defineProperty(o,i,{value:f,enumerable:!1,writable:!0}),j.push(a),t}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){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 S(e,o){if(f(e)){var i=o.call(e,e);return void 0===i?e:i}var u=j;j=[];try{var a=E(void 0,e),c=o.call(a,a);d(j,function(e,r){r.finalizing=!0}),function(){for(var e=j.length-1;e>=0;e--){var n=j[e];!1===n.modified&&(Array.isArray(n.base)?_(n)&&P(n):(t=n,o=Object.keys(t.base),i=Object.keys(t.proxy),function(e,n){if(l(e,n))return!0;if("object"!==(void 0===e?"undefined":r(e))||null===e||"object"!==(void 0===n?"undefined":r(n))||null===n)return!1;var t=Object.keys(e),o=Object.keys(n);if(t.length!==o.length)return!1;for(var i=0;i<t.length;i++)if(!hasOwnProperty.call(n,t[i])||!l(e[t[i]],n[t[i]]))return!1;return!0}(o,i)||P(n)))}var t,o,i}();var s=void 0;if(void 0!==c&&c!==a){if(a[n].modified)throw new Error(t);s=y(c)}else s=y(a);return d(j,function(e,r){r.finished=!0}),s}finally{j=u}}function k(e,o){if(1!==arguments.length&&2!==arguments.length)throw new Error("produce expects 1 or 2 arguments, got "+arguments.length);if("function"==typeof e){if("function"==typeof o)throw new Error("if first argument is a function (curried invocation), the second argument to produce cannot be a function");var a=o,c=e;return function(){var e=arguments;return k(void 0===e[0]&&void 0!==a?a:e[0],function(r){return e[0]=r,c.apply(r,e)})}}if("function"!=typeof o)throw new Error("if first argument is not a function, the second argument to produce should be a function");if("object"!==(void 0===e?"undefined":r(e))||null===e){var s=o(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":r(e))+': "'+e+'"');return i?function(e,r){if(f(e)){var o=r.call(e,e);return void 0===o?e:o}var i=v;v=[];try{var u=w(void 0,e),a=r.call(u,u),c=void 0;if(void 0!==a&&a!==u){if(u[n].modified)throw new Error(t);c=y(a)}else c=y(u);return d(v,function(e,r){return r.revoke()}),c}finally{v=i}}(e,o):S(e,o)}e.produce=k,e.default=k,e.setAutoFreeze=function(e){o=e},e.setUseProxies=function(e){i=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={});
//# sourceMappingURL=immer.umd.js.map
{
"name": "immer",
"version": "1.3.1",
"version": "1.4.0",
"description": "Create your next immutable state by mutating the current one",

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

"test": "jest",
"test:perf": "yarn-or-npm build && node --expose-gc node_modules/jest-cli/bin/jest.js --verbose --testRegex '__performance_tests__/.*?js$'",
"test:perf": "NODE_ENV=production yarn-or-npm build && cd __preformance_tests__ && babel-node add-data.js && babel-node todo.js && babel-node incremental.js",
"test:flow": "yarn-or-npm flow check",

@@ -43,3 +43,3 @@ "coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",

"devDependencies": {
"@types/jest": "^22.0.0",
"@types/jest": "^22.2.3",
"babel-cli": "^6.26.0",

@@ -73,3 +73,5 @@ "babel-core": "^6.26.0",

"rollup-plugin-uglify": "^2.0.1",
"typescript": "^2.6.2",
"seamless-immutable": "^7.1.3",
"ts-jest": "^22.4.6",
"typescript": "^2.9.1",
"uglify-es": "^3.3.6",

@@ -86,5 +88,21 @@ "yarn-or-npm": "^2.0.4"

"transform": {
"^.+\\.jsx?$": "babel-jest"
}
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "/__tests__/[^/]*[jt]sx?$",
"globals": {
"ts-jest": {
"enableTsDiagnostics": true,
"tsConfigFile": "__tests__/test.tsconfig.json"
}
},
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
}

@@ -19,2 +19,3 @@ # Immer

* Introduction blogpost: [Immer: Immutability the easy way](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3)
* [Talk](https://www.youtube.com/watch?v=-gJbS7YjcSo) + [slides](http://immer.surge.sh/) on Immer at React Finland 2018 by Michel Weststrate

@@ -293,2 +294,26 @@ 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.

The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:
```ts
import produce from 'immer';
interface State {
readonly x: number;
}
// `x` cannot be modified here
const state: State = {
x: 0;
};
const newState = produce<State>(state, draft => {
// `x` can be modified here
draft.x++;
});
// `newState.x` cannot be modified here
```
This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!
## Immer on older JavaScript environments?

@@ -298,2 +323,18 @@

## Importing immer
`produce` is exposed as the default export, but optionally it can be used as name import as well, as this benefits some older project setups. So the following imports are all correct, where the first is recommend:
```javascript
import produce from "immer"
import { produce } from "immer"
const { produce } = require("immer")
const produce = require("immer").produce
const produce = require("immer").default
import unleashTheMagic from "immer"
import { produce as unleashTheMagic } from "immer"
```
## Pitfalls

@@ -307,2 +348,3 @@

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)})`

@@ -309,0 +351,0 @@ ## Cool things built with immer

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