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
Provenance
- Commit
79a1d2c- Tree
5d4d8c9- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
74c0536
5 files changed+17 added−48 removed
modified:__tests__/polyfills.js-27 removed
28 unmodified lines293031323334353637383940414243444546474849505152535455565758593334353612 unmodified lines28 unmodified lines expect(typeof value).toBe("string") })})
describe("Object.assign", () => { const {assign} = common
it("only copies enumerable keys", () => { const src = {a: 1} Object.defineProperty(src, "b", {value: 1}) const dest = {} assign(dest, src) expect(dest.a).toBe(1) expect(dest.b).toBeUndefined() })
it("only copies own properties", () => { const src = Object.create({a: 1}) src.b = 1 const dest = {} assign(dest, src) expect(dest.a).toBeUndefined() expect(dest.b).toBe(1) })
it("can handle null", () => { const res = assign({a: 1, b: 2}, null, {a: 2, c: 2}) expect(res).toEqual({a: 2, b: 2, c: 2}) })})
describe("Reflect.ownKeys", () => { const {ownKeys} = common
// Symbol keys are always last.12 unmodified linesmodified:src/common.ts+5 added-15 removed
1-import {Objectish, ObjectishNoSet} from "./types"1+import {Objectish, ObjectishNoSet, Drafted, AnyObject, AnyArray} from "./types"2 3 /** Use a class type for `nothing` so its type is unique */4 export class Nothing {5 // This lets us do `Exclude<T, Nothing>`43 unmodified lines49 isSet(value)50 )51 }52 53-export function isPlainObject(value): boolean {53+export 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 }59 60 /** Get the underlying object that is represented by the given draft */61-export function original<T>(value: T): T | undefined {61+export 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 }67 68-export 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- })77-68 export const ownKeys: (target) => PropertyKey[] =69 typeof Reflect !== "undefined" && Reflect.ownKeys70 ? Reflect.ownKeys71 : typeof Object.getOwnPropertySymbols !== "undefined"16 unmodified lines88 throw new Error("Nope")89 }90 }91 102-export function isEnumerable(base: {}, prop: PropertyKey): boolean {92+export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {93 const desc = Object.getOwnPropertyDescriptor(base, prop)94 return desc && desc.enumerable ? true : false95 }96 88 unmodified linesmodified:src/immer.ts+7 added-2 removed
1 unmodified line2 import {createProxy, markChanged} from "./proxy"3 4 import {applyPatches, generatePatches} from "./patches"5 import {6- assign,6 each,7 has,8 is,9 isDraft,61 unmodified lines71 ) => 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 linesmodified:src/proxy.ts+3 added-2 removed
1 "use strict"2 import {3- assign,3 each,4 has,5 is,6 isDraftable,219 unmodified lines226 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 }235 236 if (parent) {11 unmodified linesmodified:src/types.ts+2 added-2 removed
2 unmodified lines3 import {MapState} from "./map"4 import {ProxyObjectState, ProxyArrayState} from "./proxy"5 import {ES5ObjectState, ES5ArrayState} from "./es5"6 7-export type Objectish = any[] | Map<any, any> | Set<any> | {}7+export type Objectish = AnyObject | AnyArray | AnyMap | AnySet8 9-export type ObjectishNoSet = any[] | Map<any, any> | {}9+export type ObjectishNoSet = AnyObject | AnyArray | AnyMap10 11 export type AnyObject = {[key: string]: any}12 export type AnyArray = Array<any>13 export type AnySet = Set<any>196 unmodified lines