Skip to content
Myr

Dropped Object.assign

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 74c0536 · Browse files · 5 files+17 added−48 removed
This commit

State

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

Diff stats

5 files+17 added−48 removed

  • tests:+0 added−27 removed
  • other:+17 added−21 removed

5 files+17 added−48 removed

modified:__tests__/polyfills.js-27 removed
modified:src/common.ts+5 added-15 removed
1import {Objectish, ObjectishNoSet} from "./types"1import {Objectish, ObjectishNoSet, Drafted, AnyObject, AnyArray} from "./types"23/** Use a class type for `nothing` so its type is unique */4export class Nothing {5 // This lets us do `Exclude<T, Nothing>`
43 unmodified lines
49 isSet(value)50 )51}5253export function isPlainObject(value): boolean {53export function isPlainObject(value): value is AnyObject | AnyArray {54 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 */61export function original<T>(value: T): T | undefined {61export function original<T>(value: Drafted<T>): T | undefined {62 if (value && value[DRAFT_STATE]) {63 return value[DRAFT_STATE].base63 return value[DRAFT_STATE].base as any64 }65 // otherwise return undefined66}6768export const assign =69 Object.assign ||70 ((target, ...overrides) => {71 overrides.forEach(override => {72 if (typeof override === "object" && override !== null)73 Object.keys(override).forEach(key => (target[key] = override[key]))74 })75 return target76 })7768export const ownKeys: (target) => PropertyKey[] =69 typeof Reflect !== "undefined" && Reflect.ownKeys70 ? Reflect.ownKeys71 : typeof Object.getOwnPropertySymbols !== "undefined"
16 unmodified lines
88 throw new Error("Nope")89 }90}91102export function isEnumerable(base: {}, prop: PropertyKey): boolean {92export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {93 const desc = Object.getOwnPropertyDescriptor(base, prop)94 return desc && desc.enumerable ? true : false95}96
88 unmodified lines
modified:src/immer.ts+7 added-2 removed
1 unmodified line
2import {createProxy, markChanged} from "./proxy"34import {applyPatches, generatePatches} from "./patches"5import {6 assign,6 each,7 has,8 is,9 isDraft,
61 unmodified lines
71 ) => void72 onDelete?: (state: ImmerState, prop: string | number) => void73 onCopy?: (state: ImmerState) => void74 }) {76 assign(this, configDefaults, config)75 each(configDefaults, (key, value) => {76 this[key] = value77 })78 config &&79 each(config, (key, value) => {80 this[key] = value81 })82 this.setUseProxies(this.useProxies)83 this.produce = this.produce.bind(this)84 this.produceWithPatches = this.produceWithPatches.bind(this)85 }
388 unmodified lines
modified:src/proxy.ts+3 added-2 removed
1"use strict"2import {3 assign,3 each,4 has,5 is,6 isDraftable,
219 unmodified lines
226 const {base, drafts, parent} = state227 if (!isMap(base) && !isSet(base)) {228 // TODO: drop creating copies here?229 const copy = (state.copy = shallowCopy(base))231 assign(copy, drafts)230 each(drafts, (key, value) => {231 copy[key] = value232 })233 state.drafts = null234 }235236 if (parent) {
11 unmodified lines
modified:src/types.ts+2 added-2 removed
2 unmodified lines
3import {MapState} from "./map"4import {ProxyObjectState, ProxyArrayState} from "./proxy"5import {ES5ObjectState, ES5ArrayState} from "./es5"67export type Objectish = any[] | Map<any, any> | Set<any> | {}7export type Objectish = AnyObject | AnyArray | AnyMap | AnySet89export type ObjectishNoSet = any[] | Map<any, any> | {}9export type ObjectishNoSet = AnyObject | AnyArray | AnyMap1011export type AnyObject = {[key: string]: any}12export type AnyArray = Array<any>13export type AnySet = Set<any>
196 unmodified lines