Skip to content
Myr

common.ts

This file

Path

src/utils/common.ts

204 lines · 5.2 KiB · TypeScript

Last changed by

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

History of this file

Take it

The walk stopped after 200 commits: lines older than 497d1a0 point at it.

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

lines 1–9why, one step back
1import {
2 DRAFT_STATE,3 DRAFTABLE,4 Objectish,5 Drafted,6 AnyObject,7 AnyMap,8 AnySet,9 ImmerState,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 10why, one step back
10 ArchType,

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

lines 11–13why, one step back
11 die
12} from "../internal"13

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 14–15why, one step back
14export const getPrototypeOf = Object.getPrototypeOf
15

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

lines 16–29why, one step back
16/** Returns true if the given value is an Immer draft */
17/*#__PURE__*/18export function isDraft(value: any): boolean {19 return !!value && !!value[DRAFT_STATE]20}2122/** Returns true if the given value can be drafted by Immer */23/*#__PURE__*/24export function isDraftable(value: any): boolean {25 if (!value) return false26 return (27 isPlainObject(value) ||28 Array.isArray(value) ||29 !!value[DRAFTABLE] ||

fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563

line 30why, one step back
30 !!value.constructor?.[DRAFTABLE] ||

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

lines 31–35why, one step back
31 isMap(value) ||
32 isSet(value)33 )34}35

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

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

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

lines 37–39why, one step back
37/*#__PURE__*/
38export function isPlainObject(value: any): boolean {39 if (!value || typeof value !== "object") return false

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 40why, one step back
40 const proto = getPrototypeOf(value)

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

lines 41–45why, one step back
41 if (proto === null) {
42 return true43 }44 const Ctor =45 Object.hasOwnProperty.call(proto, "constructor") && proto.constructor

fix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3

line 46why, one step back
46

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 47why, one step back
47 if (Ctor === Object) return true

fix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3

line 48why, one step back
48

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

lines 49–52why, one step back
49 return (
50 typeof Ctor == "function" &&51 Function.toString.call(Ctor) === objectCtorString52 )

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

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 {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 59why, one step back
59 if (!isDraft(value)) die(15, value)

Immer 7Michel Weststratebefore adoption467ea5d

line 60why, one step back
60 return value[DRAFT_STATE].base_

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

lines 61–67why, one step back
61}
6263export function each<T extends Objectish>(64 obj: T,65 iter: (key: string | number, value: any, source: T) => void,66 enumerableOnly?: boolean67): void

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 68–71why, one step back
68export function each(obj: any, iter: any) {
69 if (getArchtype(obj) === ArchType.Object) {70 Object.entries(obj).forEach(([key, value]) => {71 iter(key, value, obj)

Immer 7Michel Weststratebefore adoption467ea5d

line 72why, one step back
72 })

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

lines 73–78why, one step back
73 } else {
74 obj.forEach((entry: any, index: any) => iter(index, entry, obj))75 }76}7778/*#__PURE__*/

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 79why, one step back
79export function getArchtype(thing: any): ArchType {

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

lines 80–81why, one step back
80 const state: undefined | ImmerState = thing[DRAFT_STATE]
81 return state

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 82why, one step back
82 ? state.type_

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

line 83why, one step back
83 : Array.isArray(thing)

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 84why, one step back
84 ? ArchType.Array

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

line 85why, one step back
85 : isMap(thing)

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 86why, one step back
86 ? ArchType.Map

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

line 87why, one step back
87 : isSet(thing)

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 88–89why, one step back
88 ? ArchType.Set
89 : ArchType.Object

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

lines 90–93why, one step back
90}
9192/*#__PURE__*/93export function has(thing: any, prop: PropertyKey): boolean {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 94why, one step back
94 return getArchtype(thing) === ArchType.Map

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

lines 95–101why, one step back
95 ? thing.has(prop)
96 : Object.prototype.hasOwnProperty.call(thing, prop)97}9899/*#__PURE__*/100export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {101 // @ts-ignore

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 102why, one step back
102 return getArchtype(thing) === ArchType.Map ? thing.get(prop) : thing[prop]

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

lines 103–107why, one step back
103}
104105/*#__PURE__*/106export function set(thing: any, propOrOldValue: PropertyKey, value: any) {107 const t = getArchtype(thing)

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 108–109why, one step back
108 if (t === ArchType.Map) thing.set(propOrOldValue, value)
109 else if (t === ArchType.Set) {

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

lines 110–125why, one step back
110 thing.add(value)
111 } else thing[propOrOldValue] = value112}113114/*#__PURE__*/115export function is(x: any, y: any): boolean {116 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js117 if (x === y) {118 return x !== 0 || 1 / x === 1 / y119 } else {120 return x !== x && y !== y121 }122}123124/*#__PURE__*/125export function isMap(target: any): target is AnyMap {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 126why, one step back
126 return target instanceof Map

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

lines 127–130why, one step back
127}
128129/*#__PURE__*/130export function isSet(target: any): target is AnySet {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 131why, one step back
131 return target instanceof Set

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

lines 132–138why, one step back
132}
133/*#__PURE__*/134export function latest(state: ImmerState): any {135 return state.copy_ || state.base_136}137138/*#__PURE__*/

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 139–145why, one step back
139export function shallowCopy(base: any, strict: boolean) {
140 if (isMap(base)) {141 return new Map(base)142 }143 if (isSet(base)) {144 return new Set(base)145 }

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

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

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 147–156why, one step back
147
148 if (!strict && isPlainObject(base)) {149 if (!getPrototypeOf(base)) {150 const obj = Object.create(null)151 return Object.assign(obj, base)152 }153 return {...base}154 }155156 const descriptors = Object.getOwnPropertyDescriptors(base)

Immer 7Michel Weststratebefore adoption467ea5d

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

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 158why, one step back
158 let keys = Reflect.ownKeys(descriptors)

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

lines 159–160why, one step back
159 for (let i = 0; i < keys.length; i++) {
160 const key: any = keys[i]

Immer 7Michel Weststratebefore adoption467ea5d

lines 161–164why, one step back
161 const desc = descriptors[key]
162 if (desc.writable === false) {163 desc.writable = true164 desc.configurable = true

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

line 165why, one step back
165 }

Immer 7Michel Weststratebefore adoption467ea5d

lines 166–176why, one step back
166 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
167 // with libraries that trap values, like mobx or vue168 // unlike object.assign, non-enumerables will be copied as well169 if (desc.get || desc.set)170 descriptors[key] = {171 configurable: true,172 writable: true, // could live with !!desc.set as well here...173 enumerable: desc.enumerable,174 value: base[key]175 }176 }

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 177why, one step back
177 return Object.create(getPrototypeOf(base), descriptors)

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

lines 178–179why, one step back
178}
179

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

lines 180–189why, one step back
180/**
181 * Freezes draftable objects. Returns the original object.182 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.183 *184 * @param obj185 * @param deep186 */187export function freeze<T>(obj: T, deep?: boolean): T188export function freeze<T>(obj: any, deep: boolean = false): T {189 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj

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

lines 190–193why, one step back
190 if (getArchtype(obj) > 1 /* Map or Set */) {
191 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any192 }193 Object.freeze(obj)

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

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

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

line 195why, one step back
195 return obj

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

lines 196–199why, one step back
196}
197198function dontMutateFrozenCollections() {199 die(2)

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

lines 200–203why, one step back
200}
201202export function isFrozen(obj: any): boolean {203 return Object.isFrozen(obj)

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

line 204why, one step back
204}