Skip to content
Myr

common.ts

This file

Path

src/utils/common.ts

217 lines · 6.0 KiB · TypeScript

Last changed by

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–11why, one step back
1import {
2 DRAFT_STATE,3 DRAFTABLE,4 hasSet,5 Objectish,6 Drafted,7 AnyObject,8 AnyMap,9 AnySet,10 ImmerState,11 hasMap,

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 12why, one step back
12 Archtype,

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 13–29why, one step back
13 die
14} from "../internal"1516/** Returns true if the given value is an Immer draft */17/*#__PURE__*/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 */23/*#__PURE__*/24export function isDraftable(value: any): boolean {25 if (!value) return false26 return (27 isPlainObject(value) ||28 Array.isArray(value) ||29 !!value[DRAFTABLE] ||

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

line 30why, one step back
30 !!value.constructor?.[DRAFTABLE] ||

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 31–35why, one step back
31 isMap(value) ||
32 isSet(value)33 )34}35

fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547

line 36why, one step back
36const objectCtorString = Object.prototype.constructor.toString()

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 37–40why, one step back
37/*#__PURE__*/
38export function isPlainObject(value: any): boolean {39 if (!value || typeof value !== "object") return false40 const proto = Object.getPrototypeOf(value)

fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547

lines 41–45why, one step back
41 if (proto === null) {
42 return true43 }44 const Ctor =45 Object.hasOwnProperty.call(proto, "constructor") && proto.constructor

fix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3

line 46why, one step back
46

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 47why, one step back
47 if (Ctor === Object) return true

fix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3

line 48why, one step back
48

fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547

lines 49–52why, one step back
49 return (
50 typeof Ctor == "function" &&51 Function.toString.call(Ctor) === objectCtorString52 )

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 53–58why, one step back
53}
5455/** Get the underlying object that is represented by the given draft */56/*#__PURE__*/57export function original<T>(value: T): T | undefined58export function original(value: Drafted<any>): any {

Immer 7Michel Weststratebefore adoption467ea5d

lines 59–60why, one step back
59 if (!isDraft(value)) die(23, value)
60 return value[DRAFT_STATE].base_

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 61–73why, one step back
61}
6263/*#__PURE__*/64export const ownKeys: (target: AnyObject) => PropertyKey[] =65 typeof Reflect !== "undefined" && Reflect.ownKeys66 ? Reflect.ownKeys67 : typeof Object.getOwnPropertySymbols !== "undefined"68 ? obj =>69 Object.getOwnPropertyNames(obj).concat(70 Object.getOwnPropertySymbols(obj) as any71 )72 : /* istanbul ignore next */ Object.getOwnPropertyNames73

fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2

lines 74–84why, one step back
74export const getOwnPropertyDescriptors =
75 Object.getOwnPropertyDescriptors ||76 function getOwnPropertyDescriptors(target: any) {77 // Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/27478 const res: any = {}79 ownKeys(target).forEach(key => {80 res[key] = Object.getOwnPropertyDescriptor(target, key)81 })82 return res83 }84

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 85–86why, one step back
85export function each<T extends Objectish>(
86 obj: T,

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 87–88why, one step back
87 iter: (key: string | number, value: any, source: T) => void,
88 enumerableOnly?: boolean

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 89why, one step back
89): void

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 90why, one step back
90export function each(obj: any, iter: any, enumerableOnly = false) {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 91why, one step back
91 if (getArchtype(obj) === Archtype.Object) {

Immer 7Michel Weststratebefore adoption467ea5d

lines 92–94why, one step back
92 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key => {
93 if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj)94 })

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 95–100why, one step back
95 } else {
96 obj.forEach((entry: any, index: any) => iter(index, entry, obj))97 }98}99100/*#__PURE__*/

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 101why, one step back
101export function getArchtype(thing: any): Archtype {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 102–108why, one step back
102 /* istanbul ignore next */
103 const state: undefined | ImmerState = thing[DRAFT_STATE]104 return state105 ? state.type_ > 3106 ? state.type_ - 4 // cause Object and Array map back from 4 and 5107 : (state.type_ as any) // others are the same108 : Array.isArray(thing)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 109why, one step back
109 ? Archtype.Array

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 110why, one step back
110 : isMap(thing)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 111why, one step back
111 ? Archtype.Map

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 112why, one step back
112 : isSet(thing)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

lines 113–114why, one step back
113 ? Archtype.Set
114 : Archtype.Object

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 115–118why, one step back
115}
116117/*#__PURE__*/118export function has(thing: any, prop: PropertyKey): boolean {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 119why, one step back
119 return getArchtype(thing) === Archtype.Map

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 120–126why, one step back
120 ? thing.has(prop)
121 : Object.prototype.hasOwnProperty.call(thing, prop)122}123124/*#__PURE__*/125export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {126 // @ts-ignore

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 127why, one step back
127 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 128–132why, one step back
128}
129130/*#__PURE__*/131export function set(thing: any, propOrOldValue: PropertyKey, value: any) {132 const t = getArchtype(thing)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

lines 133–134why, one step back
133 if (t === Archtype.Map) thing.set(propOrOldValue, value)
134 else if (t === Archtype.Set) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 135–164why, one step back
135 thing.delete(propOrOldValue)
136 thing.add(value)137 } else thing[propOrOldValue] = value138}139140/*#__PURE__*/141export function is(x: any, y: any): boolean {142 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js143 if (x === y) {144 return x !== 0 || 1 / x === 1 / y145 } else {146 return x !== x && y !== y147 }148}149150/*#__PURE__*/151export function isMap(target: any): target is AnyMap {152 return hasMap && target instanceof Map153}154155/*#__PURE__*/156export function isSet(target: any): target is AnySet {157 return hasSet && target instanceof Set158}159/*#__PURE__*/160export function latest(state: ImmerState): any {161 return state.copy_ || state.base_162}163164/*#__PURE__*/

Immer 7Michel Weststratebefore adoption467ea5d

line 165why, one step back
165export function shallowCopy(base: any) {

fix: use Array.prototype.slice() for copying arrays. Fixes #650Michel Weststratebefore adoptionbf90358

line 166why, one step back
166 if (Array.isArray(base)) return Array.prototype.slice.call(base)

fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2

line 167why, one step back
167 const descriptors = getOwnPropertyDescriptors(base)

Immer 7Michel Weststratebefore adoption467ea5d

line 168why, one step back
168 delete descriptors[DRAFT_STATE as any]

fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf

lines 169–171why, one step back
169 let keys = ownKeys(descriptors)
170 for (let i = 0; i < keys.length; i++) {171 const key: any = keys[i]

Immer 7Michel Weststratebefore adoption467ea5d

lines 172–175why, one step back
172 const desc = descriptors[key]
173 if (desc.writable === false) {174 desc.writable = true175 desc.configurable = true

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 176why, one step back
176 }

Immer 7Michel Weststratebefore adoption467ea5d

lines 177–188why, one step back
177 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
178 // with libraries that trap values, like mobx or vue179 // unlike object.assign, non-enumerables will be copied as well180 if (desc.get || desc.set)181 descriptors[key] = {182 configurable: true,183 writable: true, // could live with !!desc.set as well here...184 enumerable: desc.enumerable,185 value: base[key]186 }187 }188 return Object.create(Object.getPrototypeOf(base), descriptors)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 189–190why, one step back
189}
190

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

lines 191–200why, one step back
191/**
192 * Freezes draftable objects. Returns the original object.193 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.194 *195 * @param obj196 * @param deep197 */198export function freeze<T>(obj: T, deep?: boolean): T199export function freeze<T>(obj: any, deep: boolean = false): T {200 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 201–204why, one step back
201 if (getArchtype(obj) > 1 /* Map or Set */) {
202 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any203 }204 Object.freeze(obj)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 205why, one step back
205 if (deep) each(obj, (key, value) => freeze(value, true), true)

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

line 206why, one step back
206 return obj

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 207–210why, one step back
207}
208209function dontMutateFrozenCollections() {210 die(2)

Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de

lines 211–216why, one step back
211}
212213export function isFrozen(obj: any): boolean {214 if (obj == null || typeof obj !== "object") return true215 // See #600, IE dies on non-objects in Object.isFrozen216 return Object.isFrozen(obj)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 217why, one step back
217}