common.ts
This file
Path
src/utils/common.ts
216 lines · 5.9 KiB · TypeScript
Last changed by
fix: Preserve insertion order of Sets, fixes #819 (#976)jdipbefore adoptionb3eeb69
History of this fileTake it
The walk stopped after 200 commits: lines older than 497d1a0 point at it.
fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 1–11why, one step back1import {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 back12 Archtype,fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 13–29why, one step back13 die14} 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 back30 !!value.constructor?.[DRAFTABLE] ||fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 31–35why, one step back31 isMap(value) ||32 isSet(value)33 )34}35fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547
line 36why, one step back36const objectCtorString = Object.prototype.constructor.toString()fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 37–40why, one step back37/*#__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 back41 if (proto === null) {42 return true43 }44 const Ctor =45 Object.hasOwnProperty.call(proto, "constructor") && proto.constructorfix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3
line 46why, one step back46chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 47why, one step back47 if (Ctor === Object) return truefix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3
line 48why, one step back48fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547
lines 49–52why, one step back49 return (50 typeof Ctor == "function" &&51 Function.toString.call(Ctor) === objectCtorString52 )fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 53–58why, one step back53}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 back59 if (!isDraft(value)) die(23, value)60 return value[DRAFT_STATE].base_fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 61–73why, one step back61}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.getOwnPropertyNames73fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2
lines 74–84why, one step back74export 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 }84fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 85–90why, one step back85export function each<T extends Objectish>(86 obj: T,87 iter: (key: string | number, value: any, source: T) => void,88 enumerableOnly?: boolean89): void90export function each(obj: any, iter: any, enumerableOnly = false) {chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 91why, one step back91 if (getArchtype(obj) === Archtype.Object) {Immer 7Michel Weststratebefore adoption467ea5d
lines 92–94why, one step back92 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key => {93 if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj)94 })fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 95–100why, one step back95 } 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 back101export function getArchtype(thing: any): Archtype {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 102–108why, one step back102 /* 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 back109 ? Archtype.Arrayfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 110why, one step back110 : isMap(thing)chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 111why, one step back111 ? Archtype.Mapfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 112why, one step back112 : isSet(thing)chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
lines 113–114why, one step back113 ? Archtype.Set114 : Archtype.Objectfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 115–118why, one step back115}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 back119 return getArchtype(thing) === Archtype.Mapfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 120–126why, one step back120 ? thing.has(prop)121 : Object.prototype.hasOwnProperty.call(thing, prop)122}123124/*#__PURE__*/125export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {126 // @ts-ignorechore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 127why, one step back127 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 128–132why, one step back128}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 back133 if (t === Archtype.Map) thing.set(propOrOldValue, value)134 else if (t === Archtype.Set) {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 135–163why, one step back135 thing.add(value)136 } else thing[propOrOldValue] = value137}138139/*#__PURE__*/140export function is(x: any, y: any): boolean {141 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js142 if (x === y) {143 return x !== 0 || 1 / x === 1 / y144 } else {145 return x !== x && y !== y146 }147}148149/*#__PURE__*/150export function isMap(target: any): target is AnyMap {151 return hasMap && target instanceof Map152}153154/*#__PURE__*/155export function isSet(target: any): target is AnySet {156 return hasSet && target instanceof Set157}158/*#__PURE__*/159export function latest(state: ImmerState): any {160 return state.copy_ || state.base_161}162163/*#__PURE__*/Immer 7Michel Weststratebefore adoption467ea5d
line 164why, one step back164export function shallowCopy(base: any) {fix: use Array.prototype.slice() for copying arrays. Fixes #650Michel Weststratebefore adoptionbf90358
line 165why, one step back165 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 166why, one step back166 const descriptors = getOwnPropertyDescriptors(base)Immer 7Michel Weststratebefore adoption467ea5d
line 167why, one step back167 delete descriptors[DRAFT_STATE as any]fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
lines 168–170why, one step back168 let keys = ownKeys(descriptors)169 for (let i = 0; i < keys.length; i++) {170 const key: any = keys[i]Immer 7Michel Weststratebefore adoption467ea5d
lines 171–174why, one step back171 const desc = descriptors[key]172 if (desc.writable === false) {173 desc.writable = true174 desc.configurable = truefix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 175why, one step back175 }Immer 7Michel Weststratebefore adoption467ea5d
lines 176–187why, one step back176 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing177 // with libraries that trap values, like mobx or vue178 // unlike object.assign, non-enumerables will be copied as well179 if (desc.get || desc.set)180 descriptors[key] = {181 configurable: true,182 writable: true, // could live with !!desc.set as well here...183 enumerable: desc.enumerable,184 value: base[key]185 }186 }187 return Object.create(Object.getPrototypeOf(base), descriptors)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 188–189why, one step back188}189feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
lines 190–199why, one step back190/**191 * Freezes draftable objects. Returns the original object.192 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.193 *194 * @param obj195 * @param deep196 */197export function freeze<T>(obj: T, deep?: boolean): T198export function freeze<T>(obj: any, deep: boolean = false): T {199 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return objfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 200–204why, one step back200 if (getArchtype(obj) > 1 /* Map or Set */) {201 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any202 }203 Object.freeze(obj)204 if (deep) each(obj, (key, value) => freeze(value, true), true)feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
line 205why, one step back205 return objfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 206–209why, one step back206}207208function dontMutateFrozenCollections() {209 die(2)Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
lines 210–215why, one step back210}211212export function isFrozen(obj: any): boolean {213 if (obj == null || typeof obj !== "object") return true214 // See #600, IE dies on non-objects in Object.isFrozen215 return Object.isFrozen(obj)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 216why, one step back216}