diff --git a/src/object/clone.spec.ts b/src/object/clone.spec.ts index 4af6a7fec..0d6c1b401 100644 --- a/src/object/clone.spec.ts +++ b/src/object/clone.spec.ts @@ -1,19 +1,19 @@ -import { describe, expect, it } from "vitest"; -import { clone } from "./clone"; +import { describe, expect, it } from 'vitest'; +import { clone } from './clone'; -describe("clone", () => { - it("should return primitive values as is", () => { - const symbol = Symbol("symbol"); +describe('clone', () => { + it('should return primitive values as is', () => { + const symbol = Symbol('symbol'); expect(clone(42)).toBe(42); - expect(clone("es-toolkit")).toBe("es-toolkit"); + expect(clone('es-toolkit')).toBe('es-toolkit'); expect(clone(symbol)).toBe(symbol); expect(clone(true)).toBe(true); expect(clone(null)).toBe(null); expect(clone(undefined)).toBe(undefined); }); - it("should clone arrays", () => { + it('should clone arrays', () => { const arr = [1, 2, 3]; const clonedArr = clone(arr); @@ -21,15 +21,38 @@ describe("clone", () => { expect(clonedArr).not.toBe(arr); }); - it("should clone objects", () => { - const obj = { a: 1, b: "es-toolkit", c: [1, 2, 3] }; + it('should clone ArrayBuffer', () => { + const buffer = new ArrayBuffer(8); + const clonedBuffer = clone(buffer); + + expect(clonedBuffer).toEqual(buffer); + expect(clonedBuffer).not.toBe(buffer); + }); + + it('should clone objects', () => { + const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] }; const clonedObj = clone(obj); expect(clonedObj).toEqual(obj); expect(clonedObj).not.toBe(obj); }); - it("should clone dates", () => { + it('should clone custom objects', () => { + class CustomObject { + constructor( + public a: number, + public b: string + ) {} + } + + const customObj = new CustomObject(1, 'es-toolkit'); + const clonedCustomObj = clone(customObj); + + expect(clonedCustomObj).toEqual(customObj); + expect(clonedCustomObj).not.toBe(customObj); + }); + + it('should clone dates', () => { const date = new Date(); const clonedDate = clone(date); @@ -37,7 +60,7 @@ describe("clone", () => { expect(clonedDate).not.toBe(date); }); - it("should clone regular expressions", () => { + it('should clone regular expressions', () => { const regex = /abc/g; const clonedRegex = clone(regex); @@ -45,8 +68,8 @@ describe("clone", () => { expect(clonedRegex).not.toBe(regex); }); - it("should shallow clone nested objects", () => { - const nestedObj = { a: [1, 2, 3], b: { c: "es-toolkit" }, d: new Date() }; + it('should shallow clone nested objects', () => { + const nestedObj = { a: [1, 2, 3], b: { c: 'es-toolkit' }, d: new Date() }; const clonedNestedObj = clone(nestedObj); expect(clonedNestedObj).toEqual(nestedObj); @@ -55,14 +78,14 @@ describe("clone", () => { expect(clonedNestedObj.a[2]).toEqual(nestedObj.a[2]); }); - it("should return functions as is", () => { + it('should return functions as is', () => { const func = () => {}; const clonedFunc = clone(func); expect(clonedFunc).toBe(func); }); - it("should clone sets", () => { + it('should clone sets', () => { const set = new Set([1, 2, 3]); const clonedSet = clone(set); @@ -70,8 +93,12 @@ describe("clone", () => { expect(clonedSet).not.toBe(set); }); - it("should clone maps", () => { - const map = new Map([[1, "a"], [2, "b"], [3, "c"]]); + it('should clone maps', () => { + const map = new Map([ + [1, 'a'], + [2, 'b'], + [3, 'c'], + ]); const clonedMap = clone(map); expect(clonedMap).toEqual(map); diff --git a/src/object/clone.ts b/src/object/clone.ts index 7cccdf6a7..8dd028433 100644 --- a/src/object/clone.ts +++ b/src/object/clone.ts @@ -59,8 +59,8 @@ export function clone(obj: T): T { return result as T; } - if (typeof obj === "object") { - return Object.assign({}, obj) as T; + if (typeof obj === 'object') { + return Object.assign(Object.create(Object.getPrototypeOf(obj)), obj); } return obj; } @@ -68,6 +68,5 @@ export function clone(obj: T): T { type Primitive = null | undefined | string | number | boolean | symbol | bigint; function isPrimitive(value: unknown): value is Primitive { - return value == null || - (typeof value !== "object" && typeof value !== "function"); + return value == null || (typeof value !== 'object' && typeof value !== 'function'); }