Skip to content
Myr

common.ts

This file

Path

src/utils/common.ts

217 lines · 6.0 KiB · TypeScript

Last changed by

fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547

History of this file

Take it

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

lines 1–38why, 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 (30 isPlainObject(value) ||31 Array.isArray(value) ||32 !!value[DRAFTABLE] ||33 !!value.constructor[DRAFTABLE] ||34 isMap(value) ||35 isSet(value)36 )37}38

fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547

line 39why, one step back
39const objectCtorString = Object.prototype.constructor.toString()

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

lines 40–43why, one step back
40/*#__PURE__*/
41export function isPlainObject(value: any): boolean {42 if (!value || typeof value !== "object") return false43 const proto = Object.getPrototypeOf(value)

fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547

lines 44–52why, one step back
44 if (proto === null) {
45 return true46 }47 const Ctor =48 Object.hasOwnProperty.call(proto, "constructor") && proto.constructor49 return (50 typeof Ctor == "function" &&51 Function.toString.call(Ctor) === objectCtorString52 )

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

lines 53–58why, one step back
53}
5455/** Get the underlying object that is represented by the given draft */56/*#__PURE__*/57export function original<T>(value: T): T | undefined58export function original(value: Drafted<any>): any {

Immer 7Michel Weststratebefore adoption467ea5d

lines 59–60why, one step back
59 if (!isDraft(value)) die(23, value)
60 return value[DRAFT_STATE].base_

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

lines 61–73why, one step back
61}
6263/*#__PURE__*/64export const ownKeys: (target: AnyObject) => PropertyKey[] =65 typeof Reflect !== "undefined" && Reflect.ownKeys66 ? Reflect.ownKeys67 : typeof Object.getOwnPropertySymbols !== "undefined"68 ? obj =>69 Object.getOwnPropertyNames(obj).concat(70 Object.getOwnPropertySymbols(obj) as any71 )72 : /* istanbul ignore next */ Object.getOwnPropertyNames73

fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2

lines 74–84why, one step back
74export const getOwnPropertyDescriptors =
75 Object.getOwnPropertyDescriptors ||76 function getOwnPropertyDescriptors(target: any) {77 // Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/27478 const res: any = {}79 ownKeys(target).forEach(key => {80 res[key] = Object.getOwnPropertyDescriptor(target, key)81 })82 return res83 }84

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

lines 85–86why, one step back
85export function each<T extends Objectish>(
86 obj: T,

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

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

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

line 89why, one step back
89): void

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

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

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

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

Immer 7Michel Weststratebefore adoption467ea5d

lines 92–94why, one step back
92 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key => {
93 if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj)94 })

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

lines 95–164why, one step back
95 } else {
96 obj.forEach((entry: any, index: any) => iter(index, entry, obj))97 }98}99100/*#__PURE__*/101export function getArchtype(thing: any): 0 | 1 | 2 | 3 {102 /* istanbul ignore next */103 const state: undefined | ImmerState = thing[DRAFT_STATE]104 return state105 ? state.type_ > 3106 ? state.type_ - 4 // cause Object and Array map back from 4 and 5107 : (state.type_ as any) // others are the same108 : Array.isArray(thing)109 ? ArchtypeArray110 : isMap(thing)111 ? ArchtypeMap112 : isSet(thing)113 ? ArchtypeSet114 : ArchtypeObject115}116117/*#__PURE__*/118export function has(thing: any, prop: PropertyKey): boolean {119 return getArchtype(thing) === ArchtypeMap120 ? thing.has(prop)121 : Object.prototype.hasOwnProperty.call(thing, prop)122}123124/*#__PURE__*/125export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {126 // @ts-ignore127 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]128}129130/*#__PURE__*/131export function set(thing: any, propOrOldValue: PropertyKey, value: any) {132 const t = getArchtype(thing)133 if (t === ArchtypeMap) thing.set(propOrOldValue, value)134 else if (t === ArchtypeSet) {135 thing.delete(propOrOldValue)136 thing.add(value)137 } else thing[propOrOldValue] = value138}139140/*#__PURE__*/141export function is(x: any, y: any): boolean {142 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js143 if (x === y) {144 return x !== 0 || 1 / x === 1 / y145 } else {146 return x !== x && y !== y147 }148}149150/*#__PURE__*/151export function isMap(target: any): target is AnyMap {152 return hasMap && target instanceof Map153}154155/*#__PURE__*/156export function isSet(target: any): target is AnySet {157 return hasSet && target instanceof Set158}159/*#__PURE__*/160export function latest(state: ImmerState): any {161 return state.copy_ || state.base_162}163164/*#__PURE__*/

Immer 7Michel Weststratebefore adoption467ea5d

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

fix: use Array.prototype.slice() for copying arrays. Fixes #650Michel Weststratebefore adoptionbf90358

line 166why, one step back
166 if (Array.isArray(base)) return Array.prototype.slice.call(base)

fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2

line 167why, one step back
167 const descriptors = getOwnPropertyDescriptors(base)

Immer 7Michel Weststratebefore adoption467ea5d

line 168why, one step back
168 delete descriptors[DRAFT_STATE as any]

fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf

lines 169–171why, one step back
169 let keys = ownKeys(descriptors)
170 for (let i = 0; i < keys.length; i++) {171 const key: any = keys[i]

Immer 7Michel Weststratebefore adoption467ea5d

lines 172–175why, one step back
172 const desc = descriptors[key]
173 if (desc.writable === false) {174 desc.writable = true175 desc.configurable = true

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

line 176why, one step back
176 }

Immer 7Michel Weststratebefore adoption467ea5d

lines 177–188why, one step back
177 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
178 // with libraries that trap values, like mobx or vue179 // unlike object.assign, non-enumerables will be copied as well180 if (desc.get || desc.set)181 descriptors[key] = {182 configurable: true,183 writable: true, // could live with !!desc.set as well here...184 enumerable: desc.enumerable,185 value: base[key]186 }187 }188 return Object.create(Object.getPrototypeOf(base), descriptors)

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

lines 189–190why, one step back
189}
190

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

lines 191–200why, one step back
191/**
192 * Freezes draftable objects. Returns the original object.193 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.194 *195 * @param obj196 * @param deep197 */198export function freeze<T>(obj: T, deep?: boolean): T199export function freeze<T>(obj: any, deep: boolean = false): T {200 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj

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

lines 201–204why, one step back
201 if (getArchtype(obj) > 1 /* Map or Set */) {
202 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any203 }204 Object.freeze(obj)

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

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

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

line 206why, one step back
206 return obj

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

lines 207–210why, one step back
207}
208209function dontMutateFrozenCollections() {210 die(2)

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

lines 211–216why, one step back
211}
212213export function isFrozen(obj: any): boolean {214 if (obj == null || typeof obj !== "object") return true215 // See #600, IE dies on non-objects in Object.isFrozen216 return Object.isFrozen(obj)

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

line 217why, one step back
217}