common.ts
This file
Path
src/utils/common.ts
211 lines · 5.5 KiB · TypeScript
Last changed by
fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
History of this fileTake it
The walk stopped after 200 commits: lines older than 33a305b point at it.
fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
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: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
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: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
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: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
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: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
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: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
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)fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 60–62why, one step back60 return value[DRAFT_STATE].base_61}62fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 63–67why, one step back63/**64 * Each iterates a map, set or array.65 * Or, if any other kind of of object all it's own properties.66 * Regardless whether they are enumerable or symbols67 */fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 68–69why, one step back68export function each<T extends Objectish>(69 obj: T,fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
line 70why, one step back70 iter: (key: string | number, value: any, source: T) => voidfix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
line 71why, one step back71): voidMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 72–73why, one step back72export function each(obj: any, iter: any) {73 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 74–75why, one step back74 Reflect.ownKeys(obj).forEach(key => {75 iter(key, obj[key], obj)fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 76–82why, one step back76 })77 } else {78 obj.forEach((entry: any, index: any) => iter(index, entry, obj))79 }80}8182/*#__PURE__*/Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 83why, one step back83export function getArchtype(thing: any): ArchType {fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 84–85why, one step back84 const state: undefined | ImmerState = thing[DRAFT_STATE]85 return stateMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 86why, one step back86 ? state.type_fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
line 87why, one step back87 : Array.isArray(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 88why, one step back88 ? ArchType.Arrayfix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
line 89why, one step back89 : isMap(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 90why, one step back90 ? ArchType.Mapfix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
line 91why, one step back91 : isSet(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 92–93why, one step back92 ? ArchType.Set93 : ArchType.Objectfix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 94–97why, one step back94}9596/*#__PURE__*/97export function has(thing: any, prop: PropertyKey): boolean {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 98why, one step back98 return getArchtype(thing) === ArchType.Mapfix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 99–105why, one step back99 ? thing.has(prop)100 : Object.prototype.hasOwnProperty.call(thing, prop)101}102103/*#__PURE__*/104export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {105 // @ts-ignoreMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 106why, one step back106 return getArchtype(thing) === ArchType.Map ? thing.get(prop) : thing[prop]fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 107–111why, one step back107}108109/*#__PURE__*/110export function set(thing: any, propOrOldValue: PropertyKey, value: any) {111 const t = getArchtype(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 112–113why, one step back112 if (t === ArchType.Map) thing.set(propOrOldValue, value)113 else if (t === ArchType.Set) {fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 114–129why, one step back114 thing.add(value)115 } else thing[propOrOldValue] = value116}117118/*#__PURE__*/119export function is(x: any, y: any): boolean {120 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js121 if (x === y) {122 return x !== 0 || 1 / x === 1 / y123 } else {124 return x !== x && y !== y125 }126}127128/*#__PURE__*/129export function isMap(target: any): target is AnyMap {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 130why, one step back130 return target instanceof Mapfix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 131–134why, one step back131}132133/*#__PURE__*/134export function isSet(target: any): target is AnySet {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 135why, one step back135 return target instanceof Setfix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 136–142why, one step back136}137/*#__PURE__*/138export function latest(state: ImmerState): any {139 return state.copy_ || state.base_140}141142/*#__PURE__*/Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 143–149why, one step back143export function shallowCopy(base: any, strict: boolean) {144 if (isMap(base)) {145 return new Map(base)146 }147 if (isSet(base)) {148 return new Set(base)149 }fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
line 150why, one step back150 if (Array.isArray(base)) return Array.prototype.slice.call(base)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 151–160why, one step back151152 if (!strict && isPlainObject(base)) {153 if (!getPrototypeOf(base)) {154 const obj = Object.create(null)155 return Object.assign(obj, base)156 }157 return {...base}158 }159160 const descriptors = Object.getOwnPropertyDescriptors(base)fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
line 161why, one step back161 delete descriptors[DRAFT_STATE as any]Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 162why, one step back162 let keys = Reflect.ownKeys(descriptors)fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 163–180why, one step back163 for (let i = 0; i < keys.length; i++) {164 const key: any = keys[i]165 const desc = descriptors[key]166 if (desc.writable === false) {167 desc.writable = true168 desc.configurable = true169 }170 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing171 // with libraries that trap values, like mobx or vue172 // unlike object.assign, non-enumerables will be copied as well173 if (desc.get || desc.set)174 descriptors[key] = {175 configurable: true,176 writable: true, // could live with !!desc.set as well here...177 enumerable: desc.enumerable,178 value: base[key]179 }180 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 181why, one step back181 return Object.create(getPrototypeOf(base), descriptors)fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 182–183why, one step back182}183feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
lines 184–193why, one step back184/**185 * Freezes draftable objects. Returns the original object.186 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.187 *188 * @param obj189 * @param deep190 */191export function freeze<T>(obj: T, deep?: boolean): T192export function freeze<T>(obj: any, deep: boolean = false): T {193 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return objfix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 194–197why, one step back194 if (getArchtype(obj) > 1 /* Map or Set */) {195 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any196 }197 Object.freeze(obj)fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 198–201why, one step back198 if (deep)199 // See #590, don't recurse into non-enumerable / Symbol properties when freezing200 // So use Object.entries (only string-like, enumerables) instead of each()201 Object.entries(obj).forEach(([key, value]) => freeze(value, true))feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
line 202why, one step back202 return objfix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 203–211why, one step back203}204205function dontMutateFrozenCollections() {206 die(2)207}208209export function isFrozen(obj: any): boolean {210 return Object.isFrozen(obj)211}