Skip to content
Myr

common.ts

This file

Path

src/utils/common.ts

175 lines · 4.6 KiB · TypeScript

Last changed by

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

History of this file

Take it

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

lines 1–69why, one step back
1import {
2 DRAFT_STATE,3 DRAFTABLE,4 hasSet,5 Objectish,6 Drafted,7 AnyObject,8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,12 hasMap,13 ArchtypeObject,14 ArchtypeArray,15 ArchtypeMap,16 ArchtypeSet,17 die18} from "../internal"1920/** Returns true if the given value is an Immer draft */21/*#__PURE__*/22export function isDraft(value: any): boolean {23 return !!value && !!value[DRAFT_STATE]24}2526/** Returns true if the given value can be drafted by Immer */27/*#__PURE__*/28export function isDraftable(value: any): boolean {29 if (!value) return false30 return (31 isPlainObject(value) ||32 Array.isArray(value) ||33 !!value[DRAFTABLE] ||34 !!value.constructor[DRAFTABLE] ||35 isMap(value) ||36 isSet(value)37 )38}3940/*#__PURE__*/41export function isPlainObject(value: any): boolean {42 if (!value || typeof value !== "object") return false43 const proto = Object.getPrototypeOf(value)44 return !proto || proto === Object.prototype45}4647/** Get the underlying object that is represented by the given draft */48/*#__PURE__*/49export function original<T>(value: T): T | undefined50export function original(value: Drafted<any>): any {51 if (value && value[DRAFT_STATE]) {52 return value[DRAFT_STATE].base_ as any53 }54 // otherwise return undefined55}5657/*#__PURE__*/58export const ownKeys: (target: AnyObject) => PropertyKey[] =59 typeof Reflect !== "undefined" && Reflect.ownKeys60 ? Reflect.ownKeys61 : typeof Object.getOwnPropertySymbols !== "undefined"62 ? obj =>63 Object.getOwnPropertyNames(obj).concat(64 Object.getOwnPropertySymbols(obj) as any65 )66 : /* istanbul ignore next */ Object.getOwnPropertyNames6768export function each<T extends Objectish>(69 obj: T,

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

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

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

line 72why, one step back
72): void

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

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

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

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

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

lines 75–77why, one step back
75 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key =>
76 iter(key, obj[key], obj)77 )

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

lines 78–147why, one step back
78 } else {
79 obj.forEach((entry: any, index: any) => iter(index, entry, obj))80 }81}8283/*#__PURE__*/84export function getArchtype(thing: any): 0 | 1 | 2 | 3 {85 /* istanbul ignore next */86 const state: undefined | ImmerState = thing[DRAFT_STATE]87 return state88 ? state.type_ > 389 ? state.type_ - 4 // cause Object and Array map back from 4 and 590 : (state.type_ as any) // others are the same91 : Array.isArray(thing)92 ? ArchtypeArray93 : isMap(thing)94 ? ArchtypeMap95 : isSet(thing)96 ? ArchtypeSet97 : ArchtypeObject98}99100/*#__PURE__*/101export function has(thing: any, prop: PropertyKey): boolean {102 return getArchtype(thing) === ArchtypeMap103 ? thing.has(prop)104 : Object.prototype.hasOwnProperty.call(thing, prop)105}106107/*#__PURE__*/108export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {109 // @ts-ignore110 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]111}112113/*#__PURE__*/114export function set(thing: any, propOrOldValue: PropertyKey, value: any) {115 const t = getArchtype(thing)116 if (t === ArchtypeMap) thing.set(propOrOldValue, value)117 else if (t === ArchtypeSet) {118 thing.delete(propOrOldValue)119 thing.add(value)120 } else thing[propOrOldValue] = value121}122123/*#__PURE__*/124export function is(x: any, y: any): boolean {125 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js126 if (x === y) {127 return x !== 0 || 1 / x === 1 / y128 } else {129 return x !== x && y !== y130 }131}132133/*#__PURE__*/134export function isMap(target: any): target is AnyMap {135 return hasMap && target instanceof Map136}137138/*#__PURE__*/139export function isSet(target: any): target is AnySet {140 return hasSet && target instanceof Set141}142/*#__PURE__*/143export function latest(state: ImmerState): any {144 return state.copy_ || state.base_145}146147/*#__PURE__*/

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

line 148why, one step back
148export function shallowCopy(base: any, makeWritable = false) {

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

line 149why, one step back
149 if (Array.isArray(base)) return base.slice()

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

lines 150–155why, one step back
150 const descriptors = Object.getOwnPropertyDescriptors(base)
151 delete descriptors[DRAFT_STATE as any]152 if (makeWritable)153 for (let key in descriptors)154 if (descriptors[key].writable === false) descriptors[key].writable = true155 return Object.create(Object.getPrototypeOf(base), descriptors)

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

lines 156–158why, one step back
156}
157158export function freeze(obj: any, deep: boolean): void {

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

line 159why, one step back
159 if (isDraft(obj) || isFrozen(obj) || !isDraftable(obj)) return

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

lines 160–163why, one step back
160 if (getArchtype(obj) > 1 /* Map or Set */) {
161 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any162 }163 Object.freeze(obj)

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

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

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

lines 165–168why, one step back
165}
166167function dontMutateFrozenCollections() {168 die(2)

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

lines 169–174why, one step back
169}
170171export function isFrozen(obj: any): boolean {172 if (obj == null || typeof obj !== "object") return true173 // See #600, IE dies on non-objects in Object.isFrozen174 return Object.isFrozen(obj)

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

line 175why, one step back
175}