Skip to content
Myr

common.ts

This file

Path

src/common.ts

212 lines · 5.3 KiB · TypeScript

Last changed by

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

History of this file

Take it

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 1why, one step back
1import {

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 2–4why, one step back
2 DRAFT_STATE,
3 DRAFTABLE,4 hasSet,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 5–12why, one step back
5 Objectish,
6 Drafted,7 AnyObject,8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,12 ProxyType,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 13–15why, one step back
13 Archtype,
14 hasMap15} from "./internal"

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 16why, one step back
16

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 17–26why, one step back
17/** Returns true if the given value is an Immer draft */
18export function isDraft(value: any): boolean {19 return !!value && !!value[DRAFT_STATE]20}2122/** Returns true if the given value can be drafted by Immer */23export function isDraftable(value: any): boolean {24 if (!value) return false25 return (26 isPlainObject(value) ||

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 27why, one step back
27 Array.isArray(value) ||

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 28–34why, one step back
28 !!value[DRAFTABLE] ||
29 !!value.constructor[DRAFTABLE] ||30 isMap(value) ||31 isSet(value)32 )33}34

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 35why, one step back
35export function isPlainObject(value: any): boolean {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 36–41why, one step back
36 if (!value || typeof value !== "object") return false
37 const proto = Object.getPrototypeOf(value)38 return !proto || proto === Object.prototype39}4041/** Get the underlying object that is represented by the given draft */

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 42–43why, one step back
42export function original<T>(value: T): T | undefined
43export function original(value: Drafted<any>): any {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 44why, one step back
44 if (value && value[DRAFT_STATE]) {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 45why, one step back
45 return value[DRAFT_STATE].base as any

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 46–49why, one step back
46 }
47 // otherwise return undefined48}49

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 50why, one step back
50export const ownKeys: (target: AnyObject) => PropertyKey[] =

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 51–54why, one step back
51 typeof Reflect !== "undefined" && Reflect.ownKeys
52 ? Reflect.ownKeys53 : typeof Object.getOwnPropertySymbols !== "undefined"54 ? obj =>

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 55–58why, one step back
55 Object.getOwnPropertyNames(obj).concat(
56 Object.getOwnPropertySymbols(obj) as any57 )58 : /* istanbul ignore next */ Object.getOwnPropertyNames

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 59–61why, one step back
59
60export function each<T extends Objectish>(61 obj: T,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 62–65why, one step back
62 iter: (key: string | number, value: any, source: T) => void
63): void64export function each(obj: any, iter: any) {65 if (getArchtype(obj) === Archtype.Object) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 66why, one step back
66 ownKeys(obj).forEach(key => iter(key, obj[key], obj))

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 67–68why, one step back
67 } else {
68 obj.forEach((entry: any, index: any) => iter(index, entry, obj))

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 69–71why, one step back
69 }
70}71

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 72why, one step back
72export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 73–76why, one step back
73 const desc = Object.getOwnPropertyDescriptor(base, prop)
74 return desc && desc.enumerable ? true : false75}76

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 77–104why, one step back
77export function getArchtype(thing: any): Archtype {
78 /* istanbul ignore next */79 if (!thing) die()80 if (thing[DRAFT_STATE]) {81 switch ((thing as Drafted)[DRAFT_STATE].type) {82 case ProxyType.ES5Object:83 case ProxyType.ProxyObject:84 return Archtype.Object85 case ProxyType.ES5Array:86 case ProxyType.ProxyArray:87 return Archtype.Array88 case ProxyType.Map:89 return Archtype.Map90 case ProxyType.Set:91 return Archtype.Set92 }93 }94 return Array.isArray(thing)95 ? Archtype.Array96 : isMap(thing)97 ? Archtype.Map98 : isSet(thing)99 ? Archtype.Set100 : Archtype.Object101}102103export function has(thing: any, prop: PropertyKey): boolean {104 return getArchtype(thing) === Archtype.Map

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 105–108why, one step back
105 ? thing.has(prop)
106 : Object.prototype.hasOwnProperty.call(thing, prop)107}108

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 109–111why, one step back
109export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {
110 // @ts-ignore111 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 112–113why, one step back
112}
113

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 114–128why, one step back
114export function set(thing: any, propOrOldValue: PropertyKey, value: any) {
115 switch (getArchtype(thing)) {116 case Archtype.Map:117 thing.set(propOrOldValue, value)118 break119 case Archtype.Set:120 thing.delete(propOrOldValue)121 thing.add(value)122 break123 default:124 thing[propOrOldValue] = value125 }126}127128export function is(x: any, y: any): boolean {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 129–136why, one step back
129 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
130 if (x === y) {131 return x !== 0 || 1 / x === 1 / y132 } else {133 return x !== x && y !== y134 }135}136

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 137why, one step back
137export function isMap(target: any): target is AnyMap {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 138–140why, one step back
138 return hasMap && target instanceof Map
139}140

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 141why, one step back
141export function isSet(target: any): target is AnySet {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 142–144why, one step back
142 return hasSet && target instanceof Set
143}144

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 145–146why, one step back
145export function latest(state: ImmerState): any {
146 return state.copy || state.base

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 147–148why, one step back
147}
148

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 149–158why, one step back
149export function shallowCopy<T extends AnyObject | AnyArray>(
150 base: T,151 invokeGetters?: boolean152): T153export function shallowCopy(base: any, invokeGetters = false) {154 if (Array.isArray(base)) return base.slice()155 const clone = Object.create(Object.getPrototypeOf(base))156 ownKeys(base).forEach(key => {157 if (key === DRAFT_STATE) {158 return // Never copy over draft state.

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 159why, one step back
159 }

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 160–164why, one step back
160 const desc = Object.getOwnPropertyDescriptor(base, key)!
161 let {value} = desc162 if (desc.get) {163 if (!invokeGetters) {164 throw new Error("Immer drafts cannot have computed properties")

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 165why, one step back
165 }

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 166why, one step back
166 value = desc.get.call(base)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 167why, one step back
167 }

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 168–178why, one step back
168 if (desc.enumerable) {
169 clone[key] = value170 } else {171 Object.defineProperty(clone, key, {172 value,173 writable: true,174 configurable: true175 })176 }177 })178 return clone

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 179–180why, one step back
179}
180

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 181why, one step back
181export function freeze(obj: any, deep: boolean): void {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 182why, one step back
182 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 183–184why, one step back
183 const type = getArchtype(obj)
184 if (type === Archtype.Set) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 185why, one step back
185 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 186why, one step back
186 } else if (type === Archtype.Map) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 187–194why, one step back
187 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any
188 }189 Object.freeze(obj)190 if (deep) each(obj, (_, value) => freeze(value, true))191}192193function dontMutateFrozenCollections() {194 throw new Error("This object has been frozen and should not be mutated")

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 195–211why, one step back
195}
196197export function createHiddenProperty(198 target: AnyObject,199 prop: PropertyKey,200 value: any201) {202 Object.defineProperty(target, prop, {203 value: value,204 enumerable: false,205 writable: true206 })207}208209/* istanbul ignore next */210export function die(): never {211 throw new Error("Illegal state, please file a bug")

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 212why, one step back
212}