Skip to content
Myr

common.ts

This file

Path

src/utils/common.ts

221 lines · 6.0 KiB · TypeScript

Last changed by

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

History of this file

Take it

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

lines 1–38why, 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,12 ArchtypeObject,13 ArchtypeArray,14 ArchtypeMap,15 ArchtypeSet,16 die17} from "../internal"1819/** Returns true if the given value is an Immer draft */20/*#__PURE__*/21export function isDraft(value: any): boolean {22 return !!value && !!value[DRAFT_STATE]23}2425/** Returns true if the given value can be drafted by Immer */26/*#__PURE__*/27export function isDraftable(value: any): boolean {28 if (!value) return false29 return (30 isPlainObject(value) ||31 Array.isArray(value) ||32 !!value[DRAFTABLE] ||33 !!value.constructor[DRAFTABLE] ||34 isMap(value) ||35 isSet(value)36 )37}38

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

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

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

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

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

lines 44–48why, one step back
44 if (proto === null) {
45 return true46 }47 const Ctor =48 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

lines 49–52why, one step back
49
50 if (Ctor === Object)51 return true52

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

lines 53–56why, one step back
53 return (
54 typeof Ctor == "function" &&55 Function.toString.call(Ctor) === objectCtorString56 )

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

lines 57–62why, one step back
57}
5859/** Get the underlying object that is represented by the given draft */60/*#__PURE__*/61export function original<T>(value: T): T | undefined62export function original(value: Drafted<any>): any {

Immer 7Michel Weststratebefore adoption467ea5d

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

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

lines 65–77why, one step back
65}
6667/*#__PURE__*/68export const ownKeys: (target: AnyObject) => PropertyKey[] =69 typeof Reflect !== "undefined" && Reflect.ownKeys70 ? Reflect.ownKeys71 : typeof Object.getOwnPropertySymbols !== "undefined"72 ? obj =>73 Object.getOwnPropertyNames(obj).concat(74 Object.getOwnPropertySymbols(obj) as any75 )76 : /* istanbul ignore next */ Object.getOwnPropertyNames77

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

lines 78–88why, one step back
78export const getOwnPropertyDescriptors =
79 Object.getOwnPropertyDescriptors ||80 function getOwnPropertyDescriptors(target: any) {81 // Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/27482 const res: any = {}83 ownKeys(target).forEach(key => {84 res[key] = Object.getOwnPropertyDescriptor(target, key)85 })86 return res87 }88

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

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

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

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

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

line 93why, one step back
93): void

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

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

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

line 95why, one step back
95 if (getArchtype(obj) === ArchtypeObject) {

Immer 7Michel Weststratebefore adoption467ea5d

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

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

lines 99–168why, one step back
99 } else {
100 obj.forEach((entry: any, index: any) => iter(index, entry, obj))101 }102}103104/*#__PURE__*/105export function getArchtype(thing: any): 0 | 1 | 2 | 3 {106 /* istanbul ignore next */107 const state: undefined | ImmerState = thing[DRAFT_STATE]108 return state109 ? state.type_ > 3110 ? state.type_ - 4 // cause Object and Array map back from 4 and 5111 : (state.type_ as any) // others are the same112 : Array.isArray(thing)113 ? ArchtypeArray114 : isMap(thing)115 ? ArchtypeMap116 : isSet(thing)117 ? ArchtypeSet118 : ArchtypeObject119}120121/*#__PURE__*/122export function has(thing: any, prop: PropertyKey): boolean {123 return getArchtype(thing) === ArchtypeMap124 ? thing.has(prop)125 : Object.prototype.hasOwnProperty.call(thing, prop)126}127128/*#__PURE__*/129export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {130 // @ts-ignore131 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]132}133134/*#__PURE__*/135export function set(thing: any, propOrOldValue: PropertyKey, value: any) {136 const t = getArchtype(thing)137 if (t === ArchtypeMap) thing.set(propOrOldValue, value)138 else if (t === ArchtypeSet) {139 thing.delete(propOrOldValue)140 thing.add(value)141 } else thing[propOrOldValue] = value142}143144/*#__PURE__*/145export function is(x: any, y: any): boolean {146 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js147 if (x === y) {148 return x !== 0 || 1 / x === 1 / y149 } else {150 return x !== x && y !== y151 }152}153154/*#__PURE__*/155export function isMap(target: any): target is AnyMap {156 return hasMap && target instanceof Map157}158159/*#__PURE__*/160export function isSet(target: any): target is AnySet {161 return hasSet && target instanceof Set162}163/*#__PURE__*/164export function latest(state: ImmerState): any {165 return state.copy_ || state.base_166}167168/*#__PURE__*/

Immer 7Michel Weststratebefore adoption467ea5d

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

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

line 170why, one step back
170 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 171why, one step back
171 const descriptors = getOwnPropertyDescriptors(base)

Immer 7Michel Weststratebefore adoption467ea5d

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

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

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

Immer 7Michel Weststratebefore adoption467ea5d

lines 176–179why, one step back
176 const desc = descriptors[key]
177 if (desc.writable === false) {178 desc.writable = true179 desc.configurable = true

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

line 180why, one step back
180 }

Immer 7Michel Weststratebefore adoption467ea5d

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

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

lines 193–194why, one step back
193}
194

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

lines 195–204why, one step back
195/**
196 * Freezes draftable objects. Returns the original object.197 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.198 *199 * @param obj200 * @param deep201 */202export function freeze<T>(obj: T, deep?: boolean): T203export function freeze<T>(obj: any, deep: boolean = false): T {204 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj

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

lines 205–208why, one step back
205 if (getArchtype(obj) > 1 /* Map or Set */) {
206 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any207 }208 Object.freeze(obj)

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

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

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

line 210why, one step back
210 return obj

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

lines 211–214why, one step back
211}
212213function dontMutateFrozenCollections() {214 die(2)

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

lines 215–220why, one step back
215}
216217export function isFrozen(obj: any): boolean {218 if (obj == null || typeof obj !== "object") return true219 // See #600, IE dies on non-objects in Object.isFrozen220 return Object.isFrozen(obj)

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

line 221why, one step back
221}