feat: export `isDraft` function (#220)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alec Larson · committed by aleclarson · parent 2dcede1 · Browse files · 6 files·+76 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+76 added−3 removed
- tests:+52 added−1 removed
- other:+24 added−2 removed
Provenance
- Commit
172d76a- Tree
57d92ad- Author
Alec Larson <aleclarson@users.noreply.github.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
2dcede1
6 files changed+76 added−3 removed
modified:__tests__/base.js+52 added-1 removed
1 "use strict"2-import produce, {setAutoFreeze, setUseProxies, nothing} from "../src/immer"2+import produce, {3+ setAutoFreeze,4+ setUseProxies,5+ nothing,6+ isDraft7+} from "../src/immer"8 import deepFreeze from "deep-freeze"9 import cloneDeep from "lodash.clonedeep"10 import * as lodash from "lodash"11 1303 unmodified lines1315 }1316 return freeze ? deepFreeze(data) : data1317 }1318 })1319+1320+ describe(`isDraft - ${name}`, () => {1321+ beforeAll(() => {1322+ setAutoFreeze(freeze)1323+ setUseProxies(useProxies)1324+ })1325+1326+ it("returns true for object drafts", () => {1327+ produce({}, state => {1328+ expect(isDraft(state)).toBeTruthy()1329+ })1330+ })1331+ it("returns true for array drafts", () => {1332+ produce([], state => {1333+ expect(isDraft(state)).toBeTruthy()1334+ })1335+ })1336+ it("returns true for objects nested in object drafts", () => {1337+ produce({a: {b: {}}}, state => {1338+ expect(isDraft(state.a)).toBeTruthy()1339+ expect(isDraft(state.a.b)).toBeTruthy()1340+ })1341+ })1342+ it("returns false for new objects added to a draft", () => {1343+ produce({}, state => {1344+ state.a = {}1345+ expect(isDraft(state.a)).toBeFalsy()1346+ })1347+ })1348+ it("returns false for objects returned by the producer", () => {1349+ const object = produce(null, Object.create)1350+ expect(isDraft(object)).toBeFalsy()1351+ })1352+ it("returns false for arrays returned by the producer", () => {1353+ const array = produce(null, _ => [])1354+ expect(isDraft(array)).toBeFalsy()1355+ })1356+ it("returns false for object drafts returned by the producer", () => {1357+ const object = produce({}, state => state)1358+ expect(isDraft(object)).toBeFalsy()1359+ })1360+ it("returns false for array drafts returned by the producer", () => {1361+ const array = produce([], state => state)1362+ expect(isDraft(array)).toBeFalsy()1363+ })1364+ })1365 }1366 1367 function enumerableOnly(x) {1368 // this can be done better...2 unmodified linesmodified:src/immer.d.ts+2 added
159 unmodified linesExpand all160161162163164165159 unmodified linesExpand all
export function applyPatches<S>(state: S, patches: Patch[]): S
export function original<T>(value: T): T | void
export function isDraft(value: any): booleanmodified:src/immer.js+1 added-1 removed
1-export {setAutoFreeze, setUseProxies, original} from "./common"1+export {setAutoFreeze, setUseProxies, original, isProxy as isDraft} from "./common"2 3 import {applyPatches as applyPatchesImpl} from "./patches"4 import {isProxyable, getUseProxies, NOTHING} from "./common"5 import {produceProxy} from "./proxy"74 unmodified linesmodified:src/immer.js.flow+3 added-1 removed
85 unmodified lines86 declare export function setUseProxies(useProxies: boolean): void87 88 declare export function applyPatches<S>(state: S, patches: Patch[]): S89 90-declare export function original<S>(value: S): ?S90+declare export function original<S>(value: S): ?S91+92+declare export function isDraft(value: any): booleanmodified:changelog.md+4 added
1 # Changelog2 3+### 1.8.0 (unreleased)4+5+* Introduce the `isDraft` function to check if a value is a proxy created by Immer.6+7 ### 1.7.38 9 * fix: Only use proxies if both `Proxy` and `Reflect` global APIs exist, fixes [#226](https://github.com/mweststrate/immer/issues/226)10 186 unmodified linesmodified:readme.md+14 added
432 unmodified lines433 original(draftState.users) // is === baseState.users434 })435 ```436 437+Just want to know if a value is a proxied instance? Use the `isDraft` function!438+439+```js440+import { isDraft } from "immer";441+442+const baseState = { users: [{ name: "Bobby" }] };443+const nextState = produce(baseState, draft => {444+ isDraft(draft) // => true445+ isDraft(draft.users) // => true446+ isDraft(draft.users[0]) // => true447+})448+isDraft(nextState) // => false449+```450+451 ## Using `this`452 453 The recipe will be always invoked with the `draft` as `this` context.454 266 unmodified lines