common.ts
This file
Path
src/utils/common.ts
286 lines · 7.7 KiB · TypeScript
Last changed by
chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
History of this fileTake it
The walk stopped after 200 commits: lines older than 3f9a94e point at it.
chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 1–9why, one step back1import {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 back10 ArchType,feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 11–12why, one step back11 die,12 StrictModechore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 13–14why, one step back13} from "../internal"14perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 15why, one step back15const O = ObjectMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 16why, one step back16perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 17–26why, one step back17export const getPrototypeOf = O.getPrototypeOf1819export const CONSTRUCTOR = "constructor"20export const PROTOTYPE = "prototype"2122export const CONFIGURABLE = "configurable"23export const ENUMERABLE = "enumerable"24export const WRITABLE = "writable"25export const VALUE = "value"26chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 27–28why, one step back27/** Returns true if the given value is an Immer draft */28/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 29why, one step back29export let isDraft = (value: any): boolean => !!value && !!value[DRAFT_STATE]chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 30–36why, one step back3031/** Returns true if the given value can be drafted by Immer */32/*#__PURE__*/33export function isDraftable(value: any): boolean {34 if (!value) return false35 return (36 isPlainObject(value) ||perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 37why, one step back37 isArray(value) ||chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 38why, one step back38 !!value[DRAFTABLE] ||perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 39why, one step back39 !!value[CONSTRUCTOR]?.[DRAFTABLE] ||chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 40–44why, one step back40 isMap(value) ||41 isSet(value)42 )43}44perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 45why, one step back45const objectCtorString = O[PROTOTYPE][CONSTRUCTOR].toString()feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 46why, one step back46const cachedCtorStrings = new WeakMap()chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 47–48why, one step back47/*#__PURE__*/48export function isPlainObject(value: any): boolean {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 49–51why, one step back49 if (!value || !isObjectish(value)) return false50 const proto = getPrototypeOf(value)51 if (proto === null || proto === O[PROTOTYPE]) return truefeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 52why, one step back52perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 53why, one step back53 const Ctor = O.hasOwnProperty.call(proto, CONSTRUCTOR) && proto[CONSTRUCTOR]chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 54–55why, one step back54 if (Ctor === Object) return true55perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 56why, one step back56 if (!isFunction(Ctor)) return falsefeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 57–64why, one step back5758 let ctorString = cachedCtorStrings.get(Ctor)59 if (ctorString === undefined) {60 ctorString = Function.toString.call(Ctor)61 cachedCtorStrings.set(Ctor, ctorString)62 }6364 return ctorString === objectCtorStringchore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 65–70why, one step back65}6667/** Get the underlying object that is represented by the given draft */68/*#__PURE__*/69export function original<T>(value: T): T | undefined70export function original(value: Drafted<any>): any {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 71why, one step back71 if (!isDraft(value)) die(15, value)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 72–74why, one step back72 return value[DRAFT_STATE].base_73}74fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 75–76why, one step back75/**76 * Each iterates a map, set or array.chore: Fix typo in comment in common.ts (#1113)Fredrik Vaeng Røtnesbefore adoption073d634
line 77why, one step back77 * Or, if any other kind of object, all of its own properties.feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 78–82why, one step back78 *79 * @param obj The object to iterate over80 * @param iter The iterator function81 * @param strict When true (default), includes symbols and non-enumerable properties.82 * When false, uses looseiteration over only enumerable string properties.fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
line 83why, one step back83 */chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 84–85why, one step back84export function each<T extends Objectish>(85 obj: T,feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 86–87why, one step back86 iter: (key: string | number, value: any, source: T) => void,87 strict?: booleanchore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 88why, one step back88): voidfeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 89why, one step back89export function each(obj: any, iter: any, strict: boolean = true) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 90why, one step back90 if (getArchtype(obj) === ArchType.Object) {feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 91–92why, one step back91 // If strict, we do a full iteration including symbols and non-enumerable properties92 // Otherwise, we only iterate enumerable string properties for performanceperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 93why, one step back93 const keys = strict ? Reflect.ownKeys(obj) : O.keys(obj)feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 94why, one step back94 keys.forEach(key => {fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
line 95why, one step back95 iter(key, obj[key], obj)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 96–102why, one step back96 })97 } else {98 obj.forEach((entry: any, index: any) => iter(index, entry, obj))99 }100}101102/*#__PURE__*/Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 103why, one step back103export function getArchtype(thing: any): ArchType {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 104–105why, one step back104 const state: undefined | ImmerState = thing[DRAFT_STATE]105 return stateMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 106why, one step back106 ? state.type_perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 107why, one step back107 : isArray(thing)chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
lines 108–113why, one step back108 ? ArchType.Array109 : isMap(thing)110 ? ArchType.Map111 : isSet(thing)112 ? ArchType.Set113 : ArchType.Objectchore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 114–116why, one step back114}115116/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 117–122why, one step back117export let has = (118 thing: any,119 prop: PropertyKey,120 type = getArchtype(thing)121): boolean =>122 type === ArchType.Mapchore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 123why, one step back123 ? thing.has(prop)perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 124why, one step back124 : O[PROTOTYPE].hasOwnProperty.call(thing, prop)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 125–126why, one step back125126/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 127–131why, one step back127export let get = (128 thing: AnyMap | AnyObject,129 prop: PropertyKey,130 type = getArchtype(thing)131): any =>chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 132why, one step back132 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 133why, one step back133 type === ArchType.Map ? thing.get(prop) : thing[prop]chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 134–135why, one step back134135/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 136–143why, one step back136export let set = (137 thing: any,138 propOrOldValue: PropertyKey,139 value: any,140 type = getArchtype(thing)141) => {142 if (type === ArchType.Map) thing.set(propOrOldValue, value)143 else if (type === ArchType.Set) {chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 144–157why, one step back144 thing.add(value)145 } else thing[propOrOldValue] = value146}147148/*#__PURE__*/149export function is(x: any, y: any): boolean {150 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js151 if (x === y) {152 return x !== 0 || 1 / x === 1 / y153 } else {154 return x !== x && y !== y155 }156}157perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 158–159why, one step back158export let isArray = Array.isArray159chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 160why, one step back160/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 161why, one step back161export let isMap = (target: any): target is AnyMap => target instanceof Mapchore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 162–163why, one step back162163/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 164–173why, one step back164export let isSet = (target: any): target is AnySet => target instanceof Set165166export let isObjectish = (target: any) => typeof target === "object"167168export let isFunction = (target: any): target is Function =>169 typeof target === "function"170171export let isBoolean = (target: any): target is boolean =>172 typeof target === "boolean"173feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 174–178why, one step back174export function isArrayIndex(value: string | number): value is number | string {175 const n = +value176 return Number.isInteger(n) && String(n) === value177}178perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 179–181why, one step back179export let getProxyDraft = <T extends any>(value: T): ImmerState | null => {180 if (!isObjectish(value)) return null181 return (value as {[DRAFT_STATE]: any})?.[DRAFT_STATE]chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 182why, one step back182}perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 183why, one step back183chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 184why, one step back184/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 185–188why, one step back185export let latest = (state: ImmerState): any => state.copy_ || state.base_186187export let getValue = <T extends object>(value: T): T => {188 const proxyDraft = getProxyDraft(value)chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f
line 189why, one step back189 return proxyDraft ? (proxyDraft.copy_ ?? proxyDraft.base_) : valuechore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 190–191why, one step back190}191perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 192–194why, one step back192export let getFinalValue = (state: ImmerState): any =>193 state.modified_ ? state.copy_ : state.base_194chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 195why, one step back195/*#__PURE__*/feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 196why, one step back196export function shallowCopy(base: any, strict: StrictMode) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 197–202why, one step back197 if (isMap(base)) {198 return new Map(base)199 }200 if (isSet(base)) {201 return new Set(base)202 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 203why, one step back203 if (isArray(base)) return Array[PROTOTYPE].slice.call(base)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 204why, one step back204feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 205why, one step back205 const isPlain = isPlainObject(base)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 206why, one step back206feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 207–208why, one step back207 if (strict === true || (strict === "class_only" && !isPlain)) {208 // Perform a strict copyperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 209why, one step back209 const descriptors = O.getOwnPropertyDescriptors(base)feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 210–214why, one step back210 delete descriptors[DRAFT_STATE as any]211 let keys = Reflect.ownKeys(descriptors)212 for (let i = 0; i < keys.length; i++) {213 const key: any = keys[i]214 const desc = descriptors[key]perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 215–217why, one step back215 if (desc[WRITABLE] === false) {216 desc[WRITABLE] = true217 desc[CONFIGURABLE] = truechore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 218why, one step back218 }feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 219–223why, one step back219 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing220 // with libraries that trap values, like mobx or vue221 // unlike object.assign, non-enumerables will be copied as well222 if (desc.get || desc.set)223 descriptors[key] = {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 224–227why, one step back224 [CONFIGURABLE]: true,225 [WRITABLE]: true, // could live with !!desc.set as well here...226 [ENUMERABLE]: desc[ENUMERABLE],227 [VALUE]: base[key]feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 228–229why, one step back228 }229 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 230why, one step back230 return O.create(getPrototypeOf(base), descriptors)feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 231–236why, one step back231 } else {232 // perform a sloppy copy233 const proto = getPrototypeOf(base)234 if (proto !== null && isPlain) {235 return {...base} // assumption: better inner class optimization than the assign below236 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 237–238why, one step back237 const obj = O.create(proto)238 return O.assign(obj, base)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 239–250why, one step back239 }240}241242/**243 * Freezes draftable objects. Returns the original object.244 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.245 *246 * @param obj247 * @param deep248 */249export function freeze<T>(obj: T, deep?: boolean): T250export function freeze<T>(obj: any, deep: boolean = false): T {Merge pull request #1193 from immerjs/bugfix/1192-non-draftable-valuesMark Eriksonbefore adoption4d2179b
line 251why, one step back251 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return objchore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 252why, one step back252 if (getArchtype(obj) > 1 /* Map or Set */) {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 253why, one step back253 O.defineProperties(obj, {feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 254–258why, one step back254 set: dontMutateMethodOverride,255 add: dontMutateMethodOverride,256 clear: dontMutateMethodOverride,257 delete: dontMutateMethodOverride258 })chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 259why, one step back259 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 260why, one step back260 O.freeze(obj)fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 261–262why, one step back261 if (deep)262 // See #590, don't recurse into non-enumerable / Symbol properties when freezingPrefix param with underscore to prevent unused param warning (#1145)Alissabefore adoption19cbe47
line 263why, one step back263 // So use Object.values (only string-like, enumerables) instead of each()perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 264–270why, one step back264 each(265 obj,266 (_key, value) => {267 freeze(value, true)268 },269 false270 )chore: let's test before publishMichel Weststratebefore adoption3f9a94e
lines 271–277why, one step back271 return obj272}273274function dontMutateFrozenCollections() {275 die(2)276}277feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 278why, one step back278const dontMutateMethodOverride = {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 279why, one step back279 [VALUE]: dontMutateFrozenCollectionsfeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 280–281why, one step back280}281chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 282why, one step back282export function isFrozen(obj: any): boolean {feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 283why, one step back283 // Fast path: primitives and null/undefined are always "frozen"perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 284–285why, one step back284 if (obj === null || !isObjectish(obj)) return true285 return O.isFrozen(obj)chore: let's test before publishMichel Weststratebefore adoption3f9a94e
line 286why, one step back286}