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 fileTake 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 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,fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 11–13why, one step back11 die12} from "../internal"13Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 14–15why, one step back14export const getPrototypeOf = Object.getPrototypeOf15fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 16–29why, one step back16/** 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 back30 !!value.constructor?.[DRAFTABLE] ||fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 31–35why, one step back31 isMap(value) ||32 isSet(value)33 )34}35fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547
line 36why, one step back36const 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 back37/*#__PURE__*/38export function isPlainObject(value: any): boolean {39 if (!value || typeof value !== "object") return falseMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 40why, one step back40 const proto = getPrototypeOf(value)fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547
lines 41–45why, one step back41 if (proto === null) {42 return true43 }44 const Ctor =45 Object.hasOwnProperty.call(proto, "constructor") && proto.constructorfix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3
line 46why, one step back46chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 47why, one step back47 if (Ctor === Object) return truefix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3
line 48why, one step back48fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547
lines 49–52why, one step back49 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 back53}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 back59 if (!isDraft(value)) die(15, value)Immer 7Michel Weststratebefore adoption467ea5d
line 60why, one step back60 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 back61}6263export function each<T extends Objectish>(64 obj: T,65 iter: (key: string | number, value: any, source: T) => void,66 enumerableOnly?: boolean67): voidMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 68–71why, one step back68export 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 back72 })fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 73–78why, one step back73 } 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 back79export 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 back80 const state: undefined | ImmerState = thing[DRAFT_STATE]81 return stateMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 82why, one step back82 ? state.type_fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 83why, one step back83 : Array.isArray(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 84why, one step back84 ? ArchType.Arrayfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 85why, one step back85 : isMap(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 86why, one step back86 ? ArchType.Mapfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 87why, one step back87 : isSet(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 88–89why, one step back88 ? ArchType.Set89 : ArchType.Objectfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 90–93why, one step back90}9192/*#__PURE__*/93export function has(thing: any, prop: PropertyKey): boolean {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 94why, one step back94 return getArchtype(thing) === ArchType.Mapfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 95–101why, one step back95 ? thing.has(prop)96 : Object.prototype.hasOwnProperty.call(thing, prop)97}9899/*#__PURE__*/100export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {101 // @ts-ignoreMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 102why, one step back102 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 back103}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 back108 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 back110 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 back126 return target instanceof Mapfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 127–130why, one step back127}128129/*#__PURE__*/130export function isSet(target: any): target is AnySet {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 131why, one step back131 return target instanceof Setfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 132–138why, one step back132}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 back139export 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 back146 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 back147148 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 back157 delete descriptors[DRAFT_STATE as any]Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 158why, one step back158 let keys = Reflect.ownKeys(descriptors)fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
lines 159–160why, one step back159 for (let i = 0; i < keys.length; i++) {160 const key: any = keys[i]Immer 7Michel Weststratebefore adoption467ea5d
lines 161–164why, one step back161 const desc = descriptors[key]162 if (desc.writable === false) {163 desc.writable = true164 desc.configurable = truefix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
line 165why, one step back165 }Immer 7Michel Weststratebefore adoption467ea5d
lines 166–176why, one step back166 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing167 // 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 back177 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 back178}179feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
lines 180–189why, one step back180/**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 objfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 190–193why, one step back190 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 back194 if (deep) each(obj, (_key, value) => freeze(value, true), true)feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
line 195why, one step back195 return objfix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 196–199why, one step back196}197198function dontMutateFrozenCollections() {199 die(2)Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
lines 200–203why, one step back200}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 back204}