common.ts
This file
Path
src/utils/common.ts
186 lines · 5.0 KiB · TypeScript
Last changed by
Some dotsMichel Weststratebefore adoption4e438d0
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–29why, 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 (Made ES5 version workMichel Weststratebefore adoption870f44e
lines 30–35why, one step back30 isPlainObject(value) ||31 Array.isArray(value) ||32 !!value[DRAFTABLE] ||33 !!value.constructor[DRAFTABLE] ||34 isMap(value) ||35 isSet(value)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 36–49why, one step back36 )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 {`original` will now throw when invoked on a non-draftMichel Weststratebefore adoption529e289
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–66why, 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.getOwnPropertyNames6465export function each<T extends Objectish>(66 obj: T,fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 67–68why, one step back67 iter: (key: string | number, value: any, source: T) => void,68 enumerableOnly?: booleanMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 69why, one step back69): voidfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 70why, one step back70export function each(obj: any, iter: any, enumerableOnly = false) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 71why, one step back71 if (getArchtype(obj) === ArchtypeObject) {Don't freeze symbolic propsMichel Weststratebefore adoptionc17b6e5
lines 72–74why, one step back72 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key => {73 if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj)74 })Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 75–144why, one step back75 } else {76 obj.forEach((entry: any, index: any) => iter(index, entry, obj))77 }78}7980/*#__PURE__*/81export function getArchtype(thing: any): 0 | 1 | 2 | 3 {82 /* istanbul ignore next */83 const state: undefined | ImmerState = thing[DRAFT_STATE]84 return state85 ? state.type_ > 386 ? state.type_ - 4 // cause Object and Array map back from 4 and 587 : (state.type_ as any) // others are the same88 : Array.isArray(thing)89 ? ArchtypeArray90 : isMap(thing)91 ? ArchtypeMap92 : isSet(thing)93 ? ArchtypeSet94 : ArchtypeObject95}9697/*#__PURE__*/98export function has(thing: any, prop: PropertyKey): boolean {99 return getArchtype(thing) === ArchtypeMap100 ? thing.has(prop)101 : Object.prototype.hasOwnProperty.call(thing, prop)102}103104/*#__PURE__*/105export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {106 // @ts-ignore107 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]108}109110/*#__PURE__*/111export function set(thing: any, propOrOldValue: PropertyKey, value: any) {112 const t = getArchtype(thing)113 if (t === ArchtypeMap) thing.set(propOrOldValue, value)114 else if (t === ArchtypeSet) {115 thing.delete(propOrOldValue)116 thing.add(value)117 } else thing[propOrOldValue] = value118}119120/*#__PURE__*/121export function is(x: any, y: any): boolean {122 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js123 if (x === y) {124 return x !== 0 || 1 / x === 1 / y125 } else {126 return x !== x && y !== y127 }128}129130/*#__PURE__*/131export function isMap(target: any): target is AnyMap {132 return hasMap && target instanceof Map133}134135/*#__PURE__*/136export function isSet(target: any): target is AnySet {137 return hasSet && target instanceof Set138}139/*#__PURE__*/140export function latest(state: ImmerState): any {141 return state.copy_ || state.base_142}143144/*#__PURE__*/Some cleanupMichel Weststratebefore adoptionb3b43b9
line 145why, one step back145export function shallowCopy(base: any) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 146why, one step back146 if (Array.isArray(base)) return base.slice()Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71
lines 147–148why, one step back147 const descriptors = Object.getOwnPropertyDescriptors(base)148 delete descriptors[DRAFT_STATE as any]finished accessor props for ES5Michel Weststratebefore adoption1466db3
lines 149–150why, one step back149 for (let key in descriptors) {150 const desc = descriptors[key]Some cleanupMichel Weststratebefore adoptionb3b43b9
line 151why, one step back151 if (desc.writable === false) {finished accessor props for ES5Michel Weststratebefore adoption1466db3
lines 152–157why, one step back152 desc.writable = true153 desc.configurable = true154 }155 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing156 // with libraries that trap values, like mobx or vue157 // unlike object.assign, non-enumerables will be copied as wellSome dotsMichel Weststratebefore adoption4e438d0
line 158why, one step back158 if (desc.get || desc.set)finished accessor props for ES5Michel Weststratebefore adoption1466db3
lines 159–160why, one step back159 descriptors[key] = {160 configurable: true,Some dotsMichel Weststratebefore adoption4e438d0
line 161why, one step back161 writable: true, // could live with !!desc.set as well here...finished accessor props for ES5Michel Weststratebefore adoption1466db3
lines 162–165why, one step back162 enumerable: desc.enumerable,163 value: base[key]164 }165 }Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71
line 166why, one step back166 return Object.create(Object.getPrototypeOf(base), descriptors)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 167–169why, one step back167}168169export function freeze(obj: any, deep: boolean): void {Made ES5 version workMichel Weststratebefore adoption870f44e
line 170why, one step back170 if (Object.isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) returnMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 171–174why, one step back171 if (getArchtype(obj) > 1 /* Map or Set */) {172 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any173 }174 Object.freeze(obj)fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 175why, one step back175 if (deep) each(obj, (key, value) => freeze(value, true), true)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 176–179why, one step back176}177178function dontMutateFrozenCollections() {179 die(2)Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
lines 180–185why, one step back180}181182export function isFrozen(obj: any): boolean {183 if (obj == null || typeof obj !== "object") return true184 // See #600, IE dies on non-objects in Object.isFrozen185 return Object.isFrozen(obj)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 186why, one step back186}