Skip to content
Myr

Renamed utils dir

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent db16520 · Browse files · 9 files+403 added−403 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+403 added−403 removed

  • other:+403 added−403 removed

9 files+403 added−403 removed

deleted:src/crap/common.ts-192 removed
1import {2 DRAFT_STATE,3 DRAFTABLE,4 hasSet,5 Objectish,6 Drafted,7 AnyObject,8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,12 hasMap,13 ArchtypeObject,14 ArchtypeArray,15 ArchtypeMap,16 ArchtypeSet,17 die18} from "../internal"1920/** Returns true if the given value is an Immer draft */21/*#__PURE__*/22export function isDraft(value: any): boolean {23 return !!value && !!value[DRAFT_STATE]24}2526/** Returns true if the given value can be drafted by Immer */27/*#__PURE__*/28export function isDraftable(value: any): boolean {29 if (!value) return false30 return (31 isPlainObject(value) ||32 Array.isArray(value) ||33 !!value[DRAFTABLE] ||34 !!value.constructor[DRAFTABLE] ||35 isMap(value) ||36 isSet(value)37 )38}3940/*#__PURE__*/41export function isPlainObject(value: any): boolean {42 if (!value || typeof value !== "object") return false43 const proto = Object.getPrototypeOf(value)44 return !proto || proto === Object.prototype45}4647/** Get the underlying object that is represented by the given draft */48/*#__PURE__*/49export function original<T>(value: T): T | undefined50export function original(value: Drafted<any>): any {51 if (value && value[DRAFT_STATE]) {52 return value[DRAFT_STATE].base_ as any53 }54 // otherwise return undefined55}5657/*#__PURE__*/58export const ownKeys: (target: AnyObject) => PropertyKey[] =59 typeof Reflect !== "undefined" && Reflect.ownKeys60 ? Reflect.ownKeys61 : typeof Object.getOwnPropertySymbols !== "undefined"62 ? obj =>63 Object.getOwnPropertyNames(obj).concat(64 Object.getOwnPropertySymbols(obj) as any65 )66 : /* istanbul ignore next */ Object.getOwnPropertyNames6768export function each<T extends Objectish>(69 obj: T,70 iter: (key: string | number, value: any, source: T) => void71): void72export function each(obj: any, iter: any) {73 if (getArchtype(obj) === ArchtypeObject) {74 ownKeys(obj).forEach(key => iter(key, obj[key], obj))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 switch (getArchtype(thing)) {113 case ArchtypeMap:114 thing.set(propOrOldValue, value)115 break116 case ArchtypeSet:117 thing.delete(propOrOldValue)118 thing.add(value)119 break120 default:121 thing[propOrOldValue] = value122 }123}124125/*#__PURE__*/126export function is(x: any, y: any): boolean {127 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js128 if (x === y) {129 return x !== 0 || 1 / x === 1 / y130 } else {131 return x !== x && y !== y132 }133}134135/*#__PURE__*/136export function isMap(target: any): target is AnyMap {137 return hasMap && target instanceof Map138}139140/*#__PURE__*/141export function isSet(target: any): target is AnySet {142 return hasSet && target instanceof Set143}144/*#__PURE__*/145export function latest(state: ImmerState): any {146 return state.copy_ || state.base_147}148149/*#__PURE__*/150export function shallowCopy<T extends AnyObject | AnyArray>(151 base: T,152 invokeGetters?: boolean153): T154export function shallowCopy(base: any, invokeGetters = false) {155 if (Array.isArray(base)) return base.slice()156 const clone = Object.create(Object.getPrototypeOf(base))157 // TODO: each?158 ownKeys(base).forEach(key => {159 if (key === DRAFT_STATE) {160 return // Never copy over draft state.161 }162 const desc = Object.getOwnPropertyDescriptor(base, key)!163 let {value} = desc164 if (desc.get) {165 if (!invokeGetters) die(1)166 value = desc.get.call(base)167 }168 if (desc.enumerable) {169 clone[key] = value170 } else {171 Object.defineProperty(clone, key, {172 value,173 writable: true,174 configurable: true175 })176 }177 })178 return clone179}180181export function freeze(obj: any, deep: boolean): void {182 if (isDraft(obj) || Object.isFrozen(obj) || !isDraftable(obj)) return183 if (getArchtype(obj) > 1 /* Map or Set */) {184 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any185 }186 Object.freeze(obj)187 if (deep) each(obj, (_, value) => freeze(value, true))188}189190function dontMutateFrozenCollections() {191 die(2)192}
deleted:src/crap/env.ts-48 removed
deleted:src/crap/errors.ts-51 removed
deleted:src/crap/plugins.ts-108 removed
1import {2 ImmerState,3 Patch,4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyArray,9 AnyMap,10 AnySet,11 ProxyTypeES5Array,12 ProxyTypeES5Object,13 ProxyTypeMap,14 ProxyTypeSet,15 die16} from "../internal"1718/** Plugin utilities */19const plugins: {20 Patches?: {21 generatePatches_(22 state: ImmerState,23 basePath: PatchPath,24 patches: Patch[],25 inversePatches: Patch[]26 ): void27 applyPatches_<T>(draft: T, patches: Patch[]): T28 }29 ES5?: {30 willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void31 createES5Proxy_<T>(32 base: T,33 parent?: ImmerState34 ): Drafted<T, ES5ObjectState | ES5ArrayState>35 markChangedES5_(state: ImmerState): void36 }37 MapSet?: {38 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T39 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T40 }41} = {}4243type Plugins = typeof plugins4445export function getPlugin<K extends keyof Plugins>(46 pluginKey: K47): Exclude<Plugins[K], undefined> {48 const plugin = plugins[pluginKey]49 if (!plugin) {50 die(__DEV__ ? 18 : 19, pluginKey)51 }52 // @ts-ignore53 return plugin54}5556export function loadPlugin<K extends keyof Plugins>(57 pluginKey: K,58 implementation: Plugins[K]59): void {60 plugins[pluginKey] = implementation61}6263/** ES5 Plugin */6465interface ES5BaseState extends ImmerBaseState {66 finalizing_: boolean67 assigned_: {[key: string]: any}68 parent_?: ImmerState69 revoked_: boolean70}7172export interface ES5ObjectState extends ES5BaseState {73 type_: typeof ProxyTypeES5Object74 draft_: Drafted<AnyObject, ES5ObjectState>75 base_: AnyObject76 copy_: AnyObject | null77}7879export interface ES5ArrayState extends ES5BaseState {80 type_: typeof ProxyTypeES5Array81 draft_: Drafted<AnyObject, ES5ArrayState>82 base_: AnyArray83 copy_: AnyArray | null84}8586/** Map / Set plugin */8788export interface MapState extends ImmerBaseState {89 type_: typeof ProxyTypeMap90 copy_: AnyMap | undefined91 assigned_: Map<any, boolean> | undefined92 base_: AnyMap93 revoked_: boolean94 draft_: Drafted<AnyMap, MapState>95}9697export interface SetState extends ImmerBaseState {98 type_: typeof ProxyTypeSet99 copy_: AnySet | undefined100 base_: AnySet101 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set102 revoked_: boolean103 draft_: Drafted<AnySet, SetState>104}105106/** Patches plugin */107108export type PatchPath = (string | number)[]
added:src/utils/common.ts+192 added
1import {2 DRAFT_STATE,3 DRAFTABLE,4 hasSet,5 Objectish,6 Drafted,7 AnyObject,8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,12 hasMap,13 ArchtypeObject,14 ArchtypeArray,15 ArchtypeMap,16 ArchtypeSet,17 die18} from "../internal"1920/** Returns true if the given value is an Immer draft */21/*#__PURE__*/22export function isDraft(value: any): boolean {23 return !!value && !!value[DRAFT_STATE]24}2526/** Returns true if the given value can be drafted by Immer */27/*#__PURE__*/28export function isDraftable(value: any): boolean {29 if (!value) return false30 return (31 isPlainObject(value) ||32 Array.isArray(value) ||33 !!value[DRAFTABLE] ||34 !!value.constructor[DRAFTABLE] ||35 isMap(value) ||36 isSet(value)37 )38}3940/*#__PURE__*/41export function isPlainObject(value: any): boolean {42 if (!value || typeof value !== "object") return false43 const proto = Object.getPrototypeOf(value)44 return !proto || proto === Object.prototype45}4647/** Get the underlying object that is represented by the given draft */48/*#__PURE__*/49export function original<T>(value: T): T | undefined50export function original(value: Drafted<any>): any {51 if (value && value[DRAFT_STATE]) {52 return value[DRAFT_STATE].base_ as any53 }54 // otherwise return undefined55}5657/*#__PURE__*/58export const ownKeys: (target: AnyObject) => PropertyKey[] =59 typeof Reflect !== "undefined" && Reflect.ownKeys60 ? Reflect.ownKeys61 : typeof Object.getOwnPropertySymbols !== "undefined"62 ? obj =>63 Object.getOwnPropertyNames(obj).concat(64 Object.getOwnPropertySymbols(obj) as any65 )66 : /* istanbul ignore next */ Object.getOwnPropertyNames6768export function each<T extends Objectish>(69 obj: T,70 iter: (key: string | number, value: any, source: T) => void71): void72export function each(obj: any, iter: any) {73 if (getArchtype(obj) === ArchtypeObject) {74 ownKeys(obj).forEach(key => iter(key, obj[key], obj))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 switch (getArchtype(thing)) {113 case ArchtypeMap:114 thing.set(propOrOldValue, value)115 break116 case ArchtypeSet:117 thing.delete(propOrOldValue)118 thing.add(value)119 break120 default:121 thing[propOrOldValue] = value122 }123}124125/*#__PURE__*/126export function is(x: any, y: any): boolean {127 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js128 if (x === y) {129 return x !== 0 || 1 / x === 1 / y130 } else {131 return x !== x && y !== y132 }133}134135/*#__PURE__*/136export function isMap(target: any): target is AnyMap {137 return hasMap && target instanceof Map138}139140/*#__PURE__*/141export function isSet(target: any): target is AnySet {142 return hasSet && target instanceof Set143}144/*#__PURE__*/145export function latest(state: ImmerState): any {146 return state.copy_ || state.base_147}148149/*#__PURE__*/150export function shallowCopy<T extends AnyObject | AnyArray>(151 base: T,152 invokeGetters?: boolean153): T154export function shallowCopy(base: any, invokeGetters = false) {155 if (Array.isArray(base)) return base.slice()156 const clone = Object.create(Object.getPrototypeOf(base))157 // TODO: each?158 ownKeys(base).forEach(key => {159 if (key === DRAFT_STATE) {160 return // Never copy over draft state.161 }162 const desc = Object.getOwnPropertyDescriptor(base, key)!163 let {value} = desc164 if (desc.get) {165 if (!invokeGetters) die(1)166 value = desc.get.call(base)167 }168 if (desc.enumerable) {169 clone[key] = value170 } else {171 Object.defineProperty(clone, key, {172 value,173 writable: true,174 configurable: true175 })176 }177 })178 return clone179}180181export function freeze(obj: any, deep: boolean): void {182 if (isDraft(obj) || Object.isFrozen(obj) || !isDraftable(obj)) return183 if (getArchtype(obj) > 1 /* Map or Set */) {184 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any185 }186 Object.freeze(obj)187 if (deep) each(obj, (_, value) => freeze(value, true))188}189190function dontMutateFrozenCollections() {191 die(2)192}
added:src/utils/env.ts+48 added
added:src/utils/errors.ts+51 added
added:src/utils/plugins.ts+108 added
1import {2 ImmerState,3 Patch,4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyArray,9 AnyMap,10 AnySet,11 ProxyTypeES5Array,12 ProxyTypeES5Object,13 ProxyTypeMap,14 ProxyTypeSet,15 die16} from "../internal"1718/** Plugin utilities */19const plugins: {20 Patches?: {21 generatePatches_(22 state: ImmerState,23 basePath: PatchPath,24 patches: Patch[],25 inversePatches: Patch[]26 ): void27 applyPatches_<T>(draft: T, patches: Patch[]): T28 }29 ES5?: {30 willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void31 createES5Proxy_<T>(32 base: T,33 parent?: ImmerState34 ): Drafted<T, ES5ObjectState | ES5ArrayState>35 markChangedES5_(state: ImmerState): void36 }37 MapSet?: {38 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T39 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T40 }41} = {}4243type Plugins = typeof plugins4445export function getPlugin<K extends keyof Plugins>(46 pluginKey: K47): Exclude<Plugins[K], undefined> {48 const plugin = plugins[pluginKey]49 if (!plugin) {50 die(__DEV__ ? 18 : 19, pluginKey)51 }52 // @ts-ignore53 return plugin54}5556export function loadPlugin<K extends keyof Plugins>(57 pluginKey: K,58 implementation: Plugins[K]59): void {60 plugins[pluginKey] = implementation61}6263/** ES5 Plugin */6465interface ES5BaseState extends ImmerBaseState {66 finalizing_: boolean67 assigned_: {[key: string]: any}68 parent_?: ImmerState69 revoked_: boolean70}7172export interface ES5ObjectState extends ES5BaseState {73 type_: typeof ProxyTypeES5Object74 draft_: Drafted<AnyObject, ES5ObjectState>75 base_: AnyObject76 copy_: AnyObject | null77}7879export interface ES5ArrayState extends ES5BaseState {80 type_: typeof ProxyTypeES5Array81 draft_: Drafted<AnyObject, ES5ArrayState>82 base_: AnyArray83 copy_: AnyArray | null84}8586/** Map / Set plugin */8788export interface MapState extends ImmerBaseState {89 type_: typeof ProxyTypeMap90 copy_: AnyMap | undefined91 assigned_: Map<any, boolean> | undefined92 base_: AnyMap93 revoked_: boolean94 draft_: Drafted<AnyMap, MapState>95}9697export interface SetState extends ImmerBaseState {98 type_: typeof ProxyTypeSet99 copy_: AnySet | undefined100 base_: AnySet101 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set102 revoked_: boolean103 draft_: Drafted<AnySet, SetState>104}105106/** Patches plugin */107108export type PatchPath = (string | number)[]
modified:src/internal.ts+4 added-4 removed