Add original function
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by RichieAHB · committed by RichieAHB · parent 211f5cf · Browse files · 8 files·+74 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+74 added−5 removed
- tests:+52 added−2 removed
- other:+22 added−3 removed
Provenance
- Commit
590025f- Tree
757e787- Author
RichieAHB <richard.beddington@guardian.co.uk>- Committer
RichieAHB <richard.beddington@guardian.co.uk>- Parent
211f5cf
8 files changed+74 added−5 removed
modified:__tests__/flow/flow.js.flow+17 added-1 removed
1 // @flow2-import produce, {setAutoFreeze, setUseProxies, produce as produce2, applyPatches, Patch} from "../../src/immer"2+import produce, {setAutoFreeze, setUseProxies, produce as produce2, applyPatches, Patch, original} from "../../src/immer"3 4 setAutoFreeze(true)5 setUseProxies(true)6 85 unmodified lines92 produce({ }, d=> {}, (patches, inverse) => {93 p = patches94 })95 applyPatches({}, p)96+}97+98+{99+ produce({x: 3}, draftState => {100+ original(draftState).x101+ // $ExpectError102+ original(draftState).y103+ })104+}105+106+{107+ produce([1], draftState => {108+ const a: number = original(draftState)[0];109+ // $ExpectError110+ const b: string = original(draftState)[0];111+ })112 }modified:__tests__/flow/ts.ts+13 added-1 removed
11234570 unmodified lines76777879808182838485868788899091import produce, {setAutoFreeze, setUseProxies} from "../../src/immer"import produce, {setAutoFreeze, setUseProxies, original} from "../../src/immer"
setAutoFreeze(true)setUseProxies(true)
70 unmodified lines handlers[type] ? produce<any, any>(handlers[type])(state, payload) : state}
produce({x: 3}, draftState => { original(draftState).x // $ExpectError original(draftState).y})
produce([1], draftState => { // $ExpectError const a: string = original(draftState)[0]; const b: number = original(draftState)[0];})added:__tests__/original.js+22 added
12345678910111213141516171819202122"use strict"import produce, {original} from "../src/immer"
describe("original", () => { const baseState = { a: [], b: {} }
it("should return the original from the proxy", () => { const nextState = produce(baseState, draftState => { expect(original(draftState)).toBe(baseState) expect(original(draftState.a)).toBe(baseState.a) expect(original(draftState.b)).toBe(baseState.b) }) })
it("should return the same object from an object that is not proxied", () => { const a = [] expect(original(a)).toBe(a) })})modified:src/common.js+4 added
54 unmodified lines55 }56 return value57 }58 59+export function original(value) {60+ return value && value[PROXY_STATE] ? value[PROXY_STATE].base : value61+}62+63 const assign =64 Object.assign ||65 function assign(target, value) {66 for (let key in value) {106 unmodified linesmodified:src/immer.d.ts+3 added-1 removed
107 unmodified lines108 * By default done by using feature detection109 */110 export function setUseProxies(useProxies: boolean): void111 112-export function applyPatches<S>(state: S, patches: Patch[]): S112+export function applyPatches<S>(state: S, patches: Patch[]): S113+114+export function original<T>(value: T): Tmodified:src/immer.js+1 added-1 removed
11234564 unmodified linesexport {setAutoFreeze, setUseProxies} from "./common"export {setAutoFreeze, setUseProxies, original} from "./common"
import {applyPatches as applyPatchesImpl} from "./patches"import {isProxyable, getUseProxies} from "./common"import {produceProxy} from "./proxy"64 unmodified linesmodified:src/immer.js.flow+3 added-1 removed
81 unmodified lines82 * By default done by using feature detection83 */84 declare export function setUseProxies(useProxies: boolean): void85 86-declare export function applyPatches<S>(state: S, patches: Patch[]): S86+declare export function applyPatches<S>(state: S, patches: Patch[]): S87+88+declare export function original<S>(value: S): Smodified:readme.md+11 added
349 unmodified lines350 }351 })352 ```353 354+## Extracting the original object from a proxied instance355+356+Immer exposes a named export `original` that will get the original object from the proxied instance inside `produce`. A good example of when this can be useful is when searching for nodes in a tree-like state using strict equality.357+358+```js359+const baseState = { users: [{ name: "Richie" }] };360+const nextState = produce(baseState, draftState => {361+ original(draftState.users) // is === baseState.users362+})363+```364+365 ## Using `this`366 367 The recipe will be always invoked with the `draft` as `this` context.368 239 unmodified lines