Skip to content
Myr

common.ts

This file

Path

src/utils/common.ts

190 lines · 5.1 KiB · TypeScript

Last changed by

Made ES5 version workMichel Weststratebefore adoption870f44e

History of this file

Take it

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

lines 1–29why, one step back
1import {
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 back
30 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–53why, one step back
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 undefined

Some cleanupMichel Weststratebefore adoptionb3b43b9

line 54why, one step back
54 // TODO: or die

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

lines 55–69why, one step back
55}
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) {

Don't freeze symbolic propsMichel Weststratebefore adoptionc17b6e5

lines 75–77why, one step back
75 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key => {
76 if (!enumerableOnly || typeof key !== "symbol") 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__*/

Some cleanupMichel Weststratebefore adoptionb3b43b9

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

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–151why, one step back
150 const descriptors = Object.getOwnPropertyDescriptors(base)
151 delete descriptors[DRAFT_STATE as any]

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 152–153why, one step back
152 for (let key in descriptors) {
153 const desc = descriptors[key]

Some cleanupMichel Weststratebefore adoptionb3b43b9

line 154why, one step back
154 if (desc.writable === false) {

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 155–164why, one step back
155 desc.writable = true
156 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,

Some cleanupMichel Weststratebefore adoptionb3b43b9

line 165why, one step back
165 writable: !!desc.set, // could live with 'true' as well here...

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 166–169why, one step back
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 back
170 return Object.create(Object.getPrototypeOf(base), descriptors)

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

lines 171–173why, one step back
171}
172173export function freeze(obj: any, deep: boolean): void {

Made ES5 version workMichel Weststratebefore adoption870f44e

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

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

lines 175–178why, one step back
175 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 back
179 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 back
180}
181182function dontMutateFrozenCollections() {183 die(2)

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

lines 184–189why, one step back
184}
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 back
190}