common.ts
This file
Path
src/common.ts
184 lines · 5.2 KiB · TypeScript
Last changed by
Dropped Object.assignMichel Weststratebefore adoption79a1d2c
History of this fileTake it
Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 1why, one step back1import {Objectish, ObjectishNoSet, Drafted, AnyObject, AnyArray} from "./types"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 2–52why, one step back23/** Use a class type for `nothing` so its type is unique */4export class Nothing {5 // This lets us do `Exclude<T, Nothing>`6 // TODO: do this better, use unique symbol instead7 private _: any8}910/**11 * The sentinel value returned by producers to replace the draft with undefined.12 */13export const NOTHING: Nothing =14 typeof Symbol !== "undefined"15 ? Symbol("immer-nothing")16 : ({["immer-nothing"]: true} as any)1718/**19 * To let Immer treat your class instances as plain immutable objects20 * (albeit with a custom prototype), you must define either an instance property21 * or a static property on each of your custom classes.22 *23 * Otherwise, your class instance will never be drafted, which means it won't be24 * safe to mutate in a produce callback.25 */26export const DRAFTABLE: unique symbol =27 typeof Symbol !== "undefined" && Symbol.for28 ? Symbol.for("immer-draftable")29 : ("__$immer_draftable" as any)3031export const DRAFT_STATE: unique symbol =32 typeof Symbol !== "undefined" && Symbol.for33 ? Symbol.for("immer-state")34 : ("__$immer_state" as any)3536/** Returns true if the given value is an Immer draft */37export function isDraft(value: any): boolean {38 return !!value && !!value[DRAFT_STATE]39}4041/** Returns true if the given value can be drafted by Immer */42export function isDraftable(value: any): boolean {43 if (!value) return false44 return (45 isPlainObject(value) ||46 !!value[DRAFTABLE] ||47 !!value.constructor[DRAFTABLE] ||48 isMap(value) ||49 isSet(value)50 )51}52Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 53why, one step back53export function isPlainObject(value): value is AnyObject | AnyArray {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 54–60why, one step back54 if (!value || typeof value !== "object") return false55 if (Array.isArray(value)) return true56 const proto = Object.getPrototypeOf(value)57 return !proto || proto === Object.prototype58}5960/** Get the underlying object that is represented by the given draft */Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 61why, one step back61export function original<T>(value: Drafted<T>): T | undefined {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 62why, one step back62 if (value && value[DRAFT_STATE]) {Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 63why, one step back63 return value[DRAFT_STATE].base as anyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 64–72why, one step back64 }65 // otherwise return undefined66}6768export const ownKeys: (target) => PropertyKey[] =69 typeof Reflect !== "undefined" && Reflect.ownKeys70 ? Reflect.ownKeys71 : typeof Object.getOwnPropertySymbols !== "undefined"72 ? obj =>🎵...killing me softly, with your fingers... 🎶Michel Weststratebefore adoption19a6b66
lines 73–75why, one step back73 Object.getOwnPropertyNames(obj).concat(74 Object.getOwnPropertySymbols(obj) as any75 )Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 76–84why, one step back76 : Object.getOwnPropertyNames7778export function each<T extends Objectish>(79 obj: T,80 iter: (key: PropertyKey, value: any, source: T) => void81)82export function each(obj, iter) {83 if (Array.isArray(obj) || isMap(obj) || isSet(obj)) {84 obj.forEach((entry, index) => iter(index, entry, obj))basic map tests succeedingMichel Weststratebefore adoption735c402
line 85why, one step back85 } else if (obj && typeof obj === "object") {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 86why, one step back86 ownKeys(obj).forEach(key => iter(key, obj[key], obj))basic map tests succeedingMichel Weststratebefore adoption735c402
line 87why, one step back87 } else {🎵...killing me softly, with your fingers... 🎶Michel Weststratebefore adoption19a6b66
line 88why, one step back88 throw new Error("Nope")Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 89–91why, one step back89 }90}91Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 92why, one step back92export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 93–97why, one step back93 const desc = Object.getOwnPropertyDescriptor(base, prop)94 return desc && desc.enumerable ? true : false95}9697export function has(thing: ObjectishNoSet, prop: PropertyKey): boolean {basic map tests succeedingMichel Weststratebefore adoption735c402
lines 98–100why, one step back98 return !thing99 ? false100 : isMap(thing)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 101–131why, one step back101 ? thing.has(prop)102 : Object.prototype.hasOwnProperty.call(thing, prop)103}104105export function get(thing: ObjectishNoSet, prop: PropertyKey) {106 return isMap(thing) ? thing.get(prop) : thing[prop]107}108109export function is(x, y): boolean {110 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js111 if (x === y) {112 return x !== 0 || 1 / x === 1 / y113 } else {114 return x !== x && y !== y115 }116}117118export const hasSymbol = typeof Symbol !== "undefined"119120export const hasMap = typeof Map !== "undefined"121122export function isMap(target): target is Map<any, any> {123 return hasMap && target instanceof Map124}125126export const hasSet = typeof Set !== "undefined"127128export function isSet(target): target is Set<any> {129 return hasSet && target instanceof Set130}131map unification WIPMichel Weststratebefore adoption9bcefb6
line 132why, one step back132export function latest(state: any): any {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 133–134why, one step back133 return state.copy || state.base134}fixed most testsMichel Weststratebefore adoption385c0a4
line 135why, one step back135Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 136–165why, one step back136export function shallowCopy<T extends Objectish>(137 base: T,138 invokeGetters?: boolean139): T140export function shallowCopy(base, invokeGetters = false) {141 if (Array.isArray(base)) return base.slice()142 const clone = Object.create(Object.getPrototypeOf(base))143 ownKeys(base).forEach(key => {144 if (key === DRAFT_STATE) {145 return // Never copy over draft state.146 }147 const desc = Object.getOwnPropertyDescriptor(base, key)!148 let {value} = desc149 if (desc.get) {150 if (!invokeGetters) {151 throw new Error("Immer drafts cannot have computed properties")152 }153 value = desc.get.call(base)154 }155 if (desc.enumerable) {156 clone[key] = value157 } else {158 Object.defineProperty(clone, key, {159 value,160 writable: true,161 configurable: true162 })163 }164 })165 return cloneMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 166–184why, one step back166}167168export function freeze<T extends Objectish>(169 obj: T,170 deep: boolean = false171): void {172 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return173 if (isSet(obj)) {174 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any175 } else if (isMap(obj)) {176 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any177 }178 Object.freeze(obj)179 if (deep) each(obj, (_, value) => freeze(value, true))180}181182function dontMutateFrozenCollections() {183 throw new Error("This object has been frozen and should not be mutated")184}