Skip to content
Myr

common.ts

This file

Path

src/utils/common.ts

222 lines · 6.0 KiB · TypeScript

Last changed by

fix: non-enumerable (immutable) methods on Map/Set (#1069)gimelgbefore adoptionaa24400

History of this file

Take it

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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,

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 11–12why, one step back
11 die,
12 StrictMode

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 13–14why, one step back
13} from "../internal"
14

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 32–40why, one step back
32 isMap(value) ||
33 isSet(value)34 )35}3637const objectCtorString = Object.prototype.constructor.toString()38/*#__PURE__*/39export function isPlainObject(value: any): boolean {40 if (!value || typeof value !== "object") return false

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 42–46why, one step back
42 if (proto === null) {
43 return true44 }45 const Ctor =46 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 47why, one step back
47

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

line 48why, one step back
48 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 49why, one step back
49

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 50–59why, one step back
50 return (
51 typeof Ctor == "function" &&52 Function.toString.call(Ctor) === objectCtorString53 )54}5556/** Get the underlying object that is represented by the given draft */57/*#__PURE__*/58export function original<T>(value: T): T | undefined59export function original(value: Drafted<any>): any {

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 61–63why, one step back
61 return value[DRAFT_STATE].base_
62}63

fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e

lines 64–65why, one step back
64/**
65 * Each iterates a map, set or array.

chore: Fix typo in comment in common.ts (#1113)Fredrik Vaeng Røtnesbefore adoption073d634

line 66why, one step back
66 * Or, if any other kind of object, all of its own properties.

fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e

lines 67–68why, one step back
67 * Regardless whether they are enumerable or symbols
68 */

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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

fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e

line 71why, one step back
71 iter: (key: string | number, value: any, source: T) => void

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

line 72why, one step back
72): void

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

lines 73–74why, one step back
73export function each(obj: any, iter: any) {
74 if (getArchtype(obj) === ArchType.Object) {

fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e

lines 75–76why, one step back
75 Reflect.ownKeys(obj).forEach(key => {
76 iter(key, obj[key], obj)

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 77–83why, one step back
77 })
78 } else {79 obj.forEach((entry: any, index: any) => iter(index, entry, obj))80 }81}8283/*#__PURE__*/

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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

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

lines 93–94why, one step back
93 ? ArchType.Set
94 : ArchType.Object

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 108–112why, one step back
108}
109110/*#__PURE__*/111export function set(thing: any, propOrOldValue: PropertyKey, value: any) {112 const t = getArchtype(thing)

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 115–130why, one step back
115 thing.add(value)
116 } else thing[propOrOldValue] = value117}118119/*#__PURE__*/120export function is(x: any, y: any): boolean {121 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js122 if (x === y) {123 return x !== 0 || 1 / x === 1 / y124 } else {125 return x !== x && y !== y126 }127}128129/*#__PURE__*/130export function isMap(target: any): target is AnyMap {

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 132–135why, one step back
132}
133134/*#__PURE__*/135export function isSet(target: any): target is AnySet {

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

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

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 137–143why, one step back
137}
138/*#__PURE__*/139export function latest(state: ImmerState): any {140 return state.copy_ || state.base_141}142143/*#__PURE__*/

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

line 144why, one step back
144export function shallowCopy(base: any, strict: StrictMode) {

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

lines 145–150why, one step back
145 if (isMap(base)) {
146 return new Map(base)147 }148 if (isSet(base)) {149 return new Set(base)150 }

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

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

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

line 152why, one step back
152

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

line 153why, one step back
153 const isPlain = isPlainObject(base)

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

line 154why, one step back
154

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 155–165why, one step back
155 if (strict === true || (strict === "class_only" && !isPlain)) {
156 // Perform a strict copy157 const descriptors = Object.getOwnPropertyDescriptors(base)158 delete descriptors[DRAFT_STATE as any]159 let keys = Reflect.ownKeys(descriptors)160 for (let i = 0; i < keys.length; i++) {161 const key: any = keys[i]162 const desc = descriptors[key]163 if (desc.writable === false) {164 desc.writable = true165 desc.configurable = true

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

line 166why, one step back
166 }

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 167–186why, one step back
167 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
168 // with libraries that trap values, like mobx or vue169 // unlike object.assign, non-enumerables will be copied as well170 if (desc.get || desc.set)171 descriptors[key] = {172 configurable: true,173 writable: true, // could live with !!desc.set as well here...174 enumerable: desc.enumerable,175 value: base[key]176 }177 }178 return Object.create(getPrototypeOf(base), descriptors)179 } else {180 // perform a sloppy copy181 const proto = getPrototypeOf(base)182 if (proto !== null && isPlain) {183 return {...base} // assumption: better inner class optimization than the assign below184 }185 const obj = Object.create(proto)186 return Object.assign(obj, base)

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 187–200why, one step back
187 }
188}189190/**191 * Freezes draftable objects. Returns the original object.192 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.193 *194 * @param obj195 * @param deep196 */197export function freeze<T>(obj: T, deep?: boolean): T198export function freeze<T>(obj: any, deep: boolean = false): T {199 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj200 if (getArchtype(obj) > 1 /* Map or Set */) {

fix: non-enumerable (immutable) methods on Map/Set (#1069)gimelgbefore adoptionaa24400

lines 201–206why, one step back
201 Object.defineProperties(obj, {
202 set: {value: dontMutateFrozenCollections as any},203 add: {value: dontMutateFrozenCollections as any},204 clear: {value: dontMutateFrozenCollections as any},205 delete: {value: dontMutateFrozenCollections as any}206 })

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 207–208why, one step back
207 }
208 Object.freeze(obj)

fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e

lines 209–210why, one step back
209 if (deep)
210 // See #590, don't recurse into non-enumerable / Symbol properties when freezing

Prefix param with underscore to prevent unused param warning (#1145)Alissabefore adoption19cbe47

lines 211–212why, one step back
211 // So use Object.values (only string-like, enumerables) instead of each()
212 Object.values(obj).forEach(value => freeze(value, true))

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 213–222why, one step back
213 return obj
214}215216function dontMutateFrozenCollections() {217 die(2)218}219220export function isFrozen(obj: any): boolean {221 return Object.isFrozen(obj)222}