common.ts
This file
Path
src/utils/common.ts
190 lines · 5.1 KiB · TypeScript
Last changed by
finished accessor props for ES5Michel Weststratebefore adoption1466db3
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–68why, 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 {50 if (value && value[DRAFT_STATE]) {51 return value[DRAFT_STATE].base_ as any52 }53 // otherwise return undefined54}5556/*#__PURE__*/57export const ownKeys: (target: AnyObject) => PropertyKey[] =58 typeof Reflect !== "undefined" && Reflect.ownKeys59 ? Reflect.ownKeys60 : typeof Object.getOwnPropertySymbols !== "undefined"61 ? obj =>62 Object.getOwnPropertyNames(obj).concat(63 Object.getOwnPropertySymbols(obj) as any64 )65 : /* istanbul ignore next */ Object.getOwnPropertyNames6667export function each<T extends Objectish>(68 obj: T,fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 69–70why, one step back69 iter: (key: string | number, value: any, source: T) => void,70 enumerableOnly?: booleanMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 71why, one step back71): voidfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 72why, one step back72export function each(obj: any, iter: any, enumerableOnly = false) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 73why, one step back73 if (getArchtype(obj) === ArchtypeObject) {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 74–76why, one step back74 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key =>75 iter(key, obj[key], obj)76 )Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 77–146why, one step back77 } else {78 obj.forEach((entry: any, index: any) => iter(index, entry, obj))79 }80}8182/*#__PURE__*/83export function getArchtype(thing: any): 0 | 1 | 2 | 3 {84 /* istanbul ignore next */85 const state: undefined | ImmerState = thing[DRAFT_STATE]86 return state87 ? state.type_ > 388 ? state.type_ - 4 // cause Object and Array map back from 4 and 589 : (state.type_ as any) // others are the same90 : Array.isArray(thing)91 ? ArchtypeArray92 : isMap(thing)93 ? ArchtypeMap94 : isSet(thing)95 ? ArchtypeSet96 : ArchtypeObject97}9899/*#__PURE__*/100export function has(thing: any, prop: PropertyKey): boolean {101 return getArchtype(thing) === ArchtypeMap102 ? thing.has(prop)103 : Object.prototype.hasOwnProperty.call(thing, prop)104}105106/*#__PURE__*/107export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {108 // @ts-ignore109 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]110}111112/*#__PURE__*/113export function set(thing: any, propOrOldValue: PropertyKey, value: any) {114 const t = getArchtype(thing)115 if (t === ArchtypeMap) thing.set(propOrOldValue, value)116 else if (t === ArchtypeSet) {117 thing.delete(propOrOldValue)118 thing.add(value)119 } else thing[propOrOldValue] = value120}121122/*#__PURE__*/123export function is(x: any, y: any): boolean {124 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js125 if (x === y) {126 return x !== 0 || 1 / x === 1 / y127 } else {128 return x !== x && y !== y129 }130}131132/*#__PURE__*/133export function isMap(target: any): target is AnyMap {134 return hasMap && target instanceof Map135}136137/*#__PURE__*/138export function isSet(target: any): target is AnySet {139 return hasSet && target instanceof Set140}141/*#__PURE__*/142export function latest(state: ImmerState): any {143 return state.copy_ || state.base_144}145146/*#__PURE__*/Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71
line 147why, one step back147export function shallowCopy(base: any, makeWritable = false) {finished accessor props for ES5Michel Weststratebefore adoption1466db3
line 148why, one step back148 // TODO: makeWritable is always true?Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 149why, one step back149 if (Array.isArray(base)) return base.slice()Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71
lines 150–151why, one step back150 const descriptors = Object.getOwnPropertyDescriptors(base)151 delete descriptors[DRAFT_STATE as any]finished accessor props for ES5Michel Weststratebefore adoption1466db3
lines 152–169why, one step back152 for (let key in descriptors) {153 const desc = descriptors[key]154 if (makeWritable && desc.writable === false) {155 desc.writable = true156 desc.configurable = true157 }158 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing159 // with libraries that trap values, like mobx or vue160 // unlike object.assign, non-enumerables will be copied as well161 // TODO: document!162 if (desc.get)163 descriptors[key] = {164 configurable: true,165 writable: true, // TODO: or !!desc.set ?166 enumerable: desc.enumerable,167 value: base[key]168 }169 }Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71
line 170why, one step back170 return Object.create(Object.getPrototypeOf(base), descriptors)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 171–173why, one step back171}172173export function freeze(obj: any, deep: boolean): void {Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
line 174why, one step back174 if (isDraft(obj) || isFrozen(obj) || !isDraftable(obj)) returnMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 175–178why, one step back175 if (getArchtype(obj) > 1 /* Map or Set */) {176 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any177 }178 Object.freeze(obj)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 179why, one step back179 if (deep) each(obj, (key, value) => freeze(value, true), true)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 180–183why, one step back180}181182function dontMutateFrozenCollections() {183 die(2)Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
lines 184–189why, one step back184}185186export function isFrozen(obj: any): boolean {187 if (obj == null || typeof obj !== "object") return true188 // See #600, IE dies on non-objects in Object.isFrozen189 return Object.isFrozen(obj)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 190why, one step back190}