Skip to content
Myr

fix: #506 original should not rely on `Drafted`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent 2d27f33 · Browse files · 2 files+10 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+10 added−2 removed

  • tests:+8 added−1 removed
  • other:+2 added−1 removed

2 files+10 added−2 removed

modified:__tests__/draft.ts+8 added-1 removed
1import {assert, _} from "spec.ts"2import produce, {Draft, Draft, castDraft} from "../src/index"2import produce, {Draft, Draft, castDraft, original} from "../src/index"34// For checking if a type is assignable to its draft type (and vice versa)5const toDraft: <T>(value: T) => Draft<T> = x => x as any6const fromDraft: <T>(draft: Draft<T>) => T = x => x as any
310 unmodified lines
317 draft.finishedTodos = castDraft(state.unfinishedTodos)318 })319 }320})321322test("#505 original", () => {323 const baseState = {users: [{name: "Richie"}] as const}324 const nextState = produce(baseState, draftState => {325 original(draftState.users) === baseState.users326 })327})
modified:src/common.ts+2 added-1 removed
37 unmodified lines
38 return !proto || proto === Object.prototype39}4041/** Get the underlying object that is represented by the given draft */42export function original<T>(value: Drafted<T>): T | undefined {42export function original<T>(value: T): T | undefined43export function original(value: Drafted<any>): any {44 if (value && value[DRAFT_STATE]) {45 return value[DRAFT_STATE].base as any46 }47 // otherwise return undefined
165 unmodified lines