Skip to content
Myr

common.ts

This file

Path

src/utils/common.ts

187 lines · 5.1 KiB · TypeScript

Last changed by

`original` will now throw when invoked on a non-draftMichel Weststratebefore adoption529e289

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–29why, one step back
1import {
2 DRAFT_STATE,3 DRAFTABLE,4 hasSet,5 Objectish,6 Drafted,7 AnyObject,8 AnyMap,9 AnySet,10 ImmerState,11 hasMap,12 ArchtypeObject,13 ArchtypeArray,14 ArchtypeMap,15 ArchtypeSet,16 die17} from "../internal"1819/** Returns true if the given value is an Immer draft */20/*#__PURE__*/21export function isDraft(value: any): boolean {22 return !!value && !!value[DRAFT_STATE]23}2425/** Returns true if the given value can be drafted by Immer */26/*#__PURE__*/27export function isDraftable(value: any): boolean {28 if (!value) return false29 return (

Made ES5 version workMichel Weststratebefore adoption870f44e

lines 30–35why, one step back
30 isPlainObject(value) ||
31 Array.isArray(value) ||32 !!value[DRAFTABLE] ||33 !!value.constructor[DRAFTABLE] ||34 isMap(value) ||35 isSet(value)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 36–49why, one step back
36 )
37}3839/*#__PURE__*/40export function isPlainObject(value: any): boolean {41 if (!value || typeof value !== "object") return false42 const proto = Object.getPrototypeOf(value)43 return !proto || proto === Object.prototype44}4546/** Get the underlying object that is represented by the given draft */47/*#__PURE__*/48export function original<T>(value: T): T | undefined49export function original(value: Drafted<any>): any {

`original` will now throw when invoked on a non-draftMichel Weststratebefore adoption529e289

lines 50–51why, one step back
50 if (!isDraft(value)) die(23, value)
51 return value[DRAFT_STATE].base_

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 52–66why, one step back
52}
5354/*#__PURE__*/55export const ownKeys: (target: AnyObject) => PropertyKey[] =56 typeof Reflect !== "undefined" && Reflect.ownKeys57 ? Reflect.ownKeys58 : typeof Object.getOwnPropertySymbols !== "undefined"59 ? obj =>60 Object.getOwnPropertyNames(obj).concat(61 Object.getOwnPropertySymbols(obj) as any62 )63 : /* istanbul ignore next */ Object.getOwnPropertyNames6465export function each<T extends Objectish>(66 obj: T,

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

lines 67–68why, one step back
67 iter: (key: string | number, value: any, source: T) => void,
68 enumerableOnly?: boolean

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 69why, one step back
69): void

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

line 70why, one step back
70export function each(obj: any, iter: any, enumerableOnly = false) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 71why, one step back
71 if (getArchtype(obj) === ArchtypeObject) {

Don't freeze symbolic propsMichel Weststratebefore adoptionc17b6e5

lines 72–74why, one step back
72 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key => {
73 if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj)74 })

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 75–144why, one step back
75 } else {
76 obj.forEach((entry: any, index: any) => iter(index, entry, obj))77 }78}7980/*#__PURE__*/81export function getArchtype(thing: any): 0 | 1 | 2 | 3 {82 /* istanbul ignore next */83 const state: undefined | ImmerState = thing[DRAFT_STATE]84 return state85 ? state.type_ > 386 ? state.type_ - 4 // cause Object and Array map back from 4 and 587 : (state.type_ as any) // others are the same88 : Array.isArray(thing)89 ? ArchtypeArray90 : isMap(thing)91 ? ArchtypeMap92 : isSet(thing)93 ? ArchtypeSet94 : ArchtypeObject95}9697/*#__PURE__*/98export function has(thing: any, prop: PropertyKey): boolean {99 return getArchtype(thing) === ArchtypeMap100 ? thing.has(prop)101 : Object.prototype.hasOwnProperty.call(thing, prop)102}103104/*#__PURE__*/105export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {106 // @ts-ignore107 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]108}109110/*#__PURE__*/111export function set(thing: any, propOrOldValue: PropertyKey, value: any) {112 const t = getArchtype(thing)113 if (t === ArchtypeMap) thing.set(propOrOldValue, value)114 else if (t === ArchtypeSet) {115 thing.delete(propOrOldValue)116 thing.add(value)117 } else thing[propOrOldValue] = value118}119120/*#__PURE__*/121export function is(x: any, y: any): boolean {122 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js123 if (x === y) {124 return x !== 0 || 1 / x === 1 / y125 } else {126 return x !== x && y !== y127 }128}129130/*#__PURE__*/131export function isMap(target: any): target is AnyMap {132 return hasMap && target instanceof Map133}134135/*#__PURE__*/136export function isSet(target: any): target is AnySet {137 return hasSet && target instanceof Set138}139/*#__PURE__*/140export function latest(state: ImmerState): any {141 return state.copy_ || state.base_142}143144/*#__PURE__*/

Some cleanupMichel Weststratebefore adoptionb3b43b9

line 145why, one step back
145export function shallowCopy(base: any) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

lines 147–148why, one step back
147 const descriptors = Object.getOwnPropertyDescriptors(base)
148 delete descriptors[DRAFT_STATE as any]

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 149–150why, one step back
149 for (let key in descriptors) {
150 const desc = descriptors[key]

Some cleanupMichel Weststratebefore adoptionb3b43b9

line 151why, one step back
151 if (desc.writable === false) {

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 152–161why, one step back
152 desc.writable = true
153 desc.configurable = true154 }155 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing156 // with libraries that trap values, like mobx or vue157 // unlike object.assign, non-enumerables will be copied as well158 // TODO: document!159 if (desc.get)160 descriptors[key] = {161 configurable: true,

Some cleanupMichel Weststratebefore adoptionb3b43b9

line 162why, one step back
162 writable: !!desc.set, // could live with 'true' as well here...

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 163–166why, one step back
163 enumerable: desc.enumerable,
164 value: base[key]165 }166 }

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 168–170why, one step back
168}
169170export function freeze(obj: any, deep: boolean): void {

Made ES5 version workMichel Weststratebefore adoption870f44e

line 171why, one step back
171 if (Object.isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 172–175why, one step back
172 if (getArchtype(obj) > 1 /* Map or Set */) {
173 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any174 }175 Object.freeze(obj)

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

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 177–180why, one step back
177}
178179function dontMutateFrozenCollections() {180 die(2)

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

lines 181–186why, one step back
181}
182183export function isFrozen(obj: any): boolean {184 if (obj == null || typeof obj !== "object") return true185 // See #600, IE dies on non-objects in Object.isFrozen186 return Object.isFrozen(obj)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 187why, one step back
187}