Skip to content
Myr

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

6 files+76 added−3 removed

modified:__tests__/base.js+52 added-1 removed
1"use strict"2import produce, {setAutoFreeze, setUseProxies, nothing} from "../src/immer"2import produce, {3 setAutoFreeze,4 setUseProxies,5 nothing,6 isDraft7} from "../src/immer"8import deepFreeze from "deep-freeze"9import cloneDeep from "lodash.clonedeep"10import * as lodash from "lodash"11
1303 unmodified lines
1315 }1316 return freeze ? deepFreeze(data) : data1317 }1318 })13191320 describe(`isDraft - ${name}`, () => {1321 beforeAll(() => {1322 setAutoFreeze(freeze)1323 setUseProxies(useProxies)1324 })13251326 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}13661367function enumerableOnly(x) {1368 // this can be done better...
2 unmodified lines
modified:src/immer.d.ts+2 added
modified:src/immer.js+1 added-1 removed
1export {setAutoFreeze, setUseProxies, original} from "./common"1export {setAutoFreeze, setUseProxies, original, isProxy as isDraft} from "./common"23import {applyPatches as applyPatchesImpl} from "./patches"4import {isProxyable, getUseProxies, NOTHING} from "./common"5import {produceProxy} from "./proxy"
74 unmodified lines
modified:src/immer.js.flow+3 added-1 removed
85 unmodified lines
86declare export function setUseProxies(useProxies: boolean): void8788declare export function applyPatches<S>(state: S, patches: Patch[]): S8990declare export function original<S>(value: S): ?S90declare export function original<S>(value: S): ?S9192declare export function isDraft(value: any): boolean
modified:changelog.md+4 added
1# Changelog23### 1.8.0 (unreleased)45* Introduce the `isDraft` function to check if a value is a proxy created by Immer.67### 1.7.389* fix: Only use proxies if both `Proxy` and `Reflect` global APIs exist, fixes [#226](https://github.com/mweststrate/immer/issues/226)10
186 unmodified lines
modified:readme.md+14 added
432 unmodified lines
433 original(draftState.users) // is === baseState.users434})435```436437Just want to know if a value is a proxied instance? Use the `isDraft` function!438439```js440import { isDraft } from "immer";441442const baseState = { users: [{ name: "Bobby" }] };443const nextState = produce(baseState, draft => {444 isDraft(draft) // => true445 isDraft(draft.users) // => true446 isDraft(draft.users[0]) // => true447})448isDraft(nextState) // => false449```450451## Using `this`452453The recipe will be always invoked with the `draft` as `this` context.454
266 unmodified lines