common.ts
This file
Path
src/utils/common.ts
199 lines · 5.4 KiB · TypeScript
Last changed by
fix: Fixed regression in Object.frozen for IE11. Fixes #600Michel Weststratebefore adoption6371d05
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–49why, one step back1import {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}3839/*#__PURE__*/40export function isPlainObject(value: any): boolean {41 if (!value || typeof value !== "object") return false42 const proto = Object.getPrototypeOf(value)43 return !proto || proto === Object.prototype44}4546/** Get the underlying object that is represented by the given draft */47/*#__PURE__*/48export function original<T>(value: T): T | undefined49export function original(value: Drafted<any>): any {Immer 7Michel Weststratebefore adoption467ea5d
lines 50–51why, one step back50 if (!isDraft(value)) die(23, value)51 return value[DRAFT_STATE].base_Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 52–64why, one step back52}5354/*#__PURE__*/55export const ownKeys: (target: AnyObject) => PropertyKey[] =56 typeof Reflect !== "undefined" && Reflect.ownKeys57 ? Reflect.ownKeys58 : typeof Object.getOwnPropertySymbols !== "undefined"59 ? obj =>60 Object.getOwnPropertyNames(obj).concat(61 Object.getOwnPropertySymbols(obj) as any62 )63 : /* istanbul ignore next */ Object.getOwnPropertyNames64fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2
lines 65–75why, one step back65export const getOwnPropertyDescriptors =66 Object.getOwnPropertyDescriptors ||67 function getOwnPropertyDescriptors(target: any) {68 // Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/27469 const res: any = {}70 ownKeys(target).forEach(key => {71 res[key] = Object.getOwnPropertyDescriptor(target, key)72 })73 return res74 }75Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 76–77why, one step back76export function each<T extends Objectish>(77 obj: T,fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 78–79why, one step back78 iter: (key: string | number, value: any, source: T) => void,79 enumerableOnly?: booleanMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 80why, one step back80): voidfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 81why, one step back81export function each(obj: any, iter: any, enumerableOnly = false) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 82why, one step back82 if (getArchtype(obj) === ArchtypeObject) {Immer 7Michel Weststratebefore adoption467ea5d
lines 83–85why, one step back83 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key => {84 if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj)85 })Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 86–155why, one step back86 } else {87 obj.forEach((entry: any, index: any) => iter(index, entry, obj))88 }89}9091/*#__PURE__*/92export function getArchtype(thing: any): 0 | 1 | 2 | 3 {93 /* istanbul ignore next */94 const state: undefined | ImmerState = thing[DRAFT_STATE]95 return state96 ? state.type_ > 397 ? state.type_ - 4 // cause Object and Array map back from 4 and 598 : (state.type_ as any) // others are the same99 : Array.isArray(thing)100 ? ArchtypeArray101 : isMap(thing)102 ? ArchtypeMap103 : isSet(thing)104 ? ArchtypeSet105 : ArchtypeObject106}107108/*#__PURE__*/109export function has(thing: any, prop: PropertyKey): boolean {110 return getArchtype(thing) === ArchtypeMap111 ? thing.has(prop)112 : Object.prototype.hasOwnProperty.call(thing, prop)113}114115/*#__PURE__*/116export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {117 // @ts-ignore118 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]119}120121/*#__PURE__*/122export function set(thing: any, propOrOldValue: PropertyKey, value: any) {123 const t = getArchtype(thing)124 if (t === ArchtypeMap) thing.set(propOrOldValue, value)125 else if (t === ArchtypeSet) {126 thing.delete(propOrOldValue)127 thing.add(value)128 } else thing[propOrOldValue] = value129}130131/*#__PURE__*/132export function is(x: any, y: any): boolean {133 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js134 if (x === y) {135 return x !== 0 || 1 / x === 1 / y136 } else {137 return x !== x && y !== y138 }139}140141/*#__PURE__*/142export function isMap(target: any): target is AnyMap {143 return hasMap && target instanceof Map144}145146/*#__PURE__*/147export function isSet(target: any): target is AnySet {148 return hasSet && target instanceof Set149}150/*#__PURE__*/151export function latest(state: ImmerState): any {152 return state.copy_ || state.base_153}154155/*#__PURE__*/Immer 7Michel Weststratebefore adoption467ea5d
line 156why, one step back156export function shallowCopy(base: any) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 157why, one step back157 if (Array.isArray(base)) return base.slice()fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2
line 158why, one step back158 const descriptors = getOwnPropertyDescriptors(base)Immer 7Michel Weststratebefore adoption467ea5d
line 159why, one step back159 delete descriptors[DRAFT_STATE as any]fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
lines 160–162why, one step back160 let keys = ownKeys(descriptors)161 for (let i = 0; i < keys.length; i++) {162 const key: any = keys[i]Immer 7Michel Weststratebefore adoption467ea5d
lines 163–166why, one step back163 const desc = descriptors[key]164 if (desc.writable === false) {165 desc.writable = true166 desc.configurable = trueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 167why, one step back167 }Immer 7Michel Weststratebefore adoption467ea5d
lines 168–179why, one step back168 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing169 // with libraries that trap values, like mobx or vue170 // unlike object.assign, non-enumerables will be copied as well171 if (desc.get || desc.set)172 descriptors[key] = {173 configurable: true,174 writable: true, // could live with !!desc.set as well here...175 enumerable: desc.enumerable,176 value: base[key]177 }178 }179 return Object.create(Object.getPrototypeOf(base), descriptors)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 180–182why, one step back180}181182export function freeze(obj: any, deep: boolean): void {fix: Fixed regression in Object.frozen for IE11. Fixes #600Michel Weststratebefore adoption6371d05
line 183why, one step back183 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) returnMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 184–187why, one step back184 if (getArchtype(obj) > 1 /* Map or Set */) {185 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any186 }187 Object.freeze(obj)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 188why, one step back188 if (deep) each(obj, (key, value) => freeze(value, true), true)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 189–192why, one step back189}190191function dontMutateFrozenCollections() {192 die(2)Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
lines 193–198why, one step back193}194195export function isFrozen(obj: any): boolean {196 if (obj == null || typeof obj !== "object") return true197 // See #600, IE dies on non-objects in Object.isFrozen198 return Object.isFrozen(obj)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 199why, one step back199}