UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

62 lines (61 loc) 2.21 kB
import { isPrimitive } from "../predicate/isPrimitive.mjs"; import { isTypedArray } from "../predicate/isTypedArray.mjs"; //#region src/object/clone.ts /** * Creates a shallow clone of the given object. * * @template T - The type of the object. * @param obj - The object to clone. * @returns A shallow clone of the given object. * * @example * // Clone a primitive value * const num = 29; * const clonedNum = clone(num); * console.log(clonedNum); // 29 * console.log(clonedNum === num); // true * * @example * // Clone an array * const arr = [1, 2, 3]; * const clonedArr = clone(arr); * console.log(clonedArr); // [1, 2, 3] * console.log(clonedArr === arr); // false * * @example * // Clone an object * const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] }; * const clonedObj = clone(obj); * console.log(clonedObj); // { a: 1, b: 'es-toolkit', c: [1, 2, 3] } * console.log(clonedObj === obj); // false */ function clone(obj) { if (isPrimitive(obj)) return obj; if (Array.isArray(obj) || isTypedArray(obj) || obj instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && obj instanceof SharedArrayBuffer) return obj.slice(0); const prototype = Object.getPrototypeOf(obj); if (prototype == null) return Object.assign(Object.create(prototype), obj); const Constructor = prototype.constructor; if (obj instanceof Date || obj instanceof Map || obj instanceof Set) return new Constructor(obj); if (obj instanceof RegExp) { const newRegExp = new Constructor(obj); newRegExp.lastIndex = obj.lastIndex; return newRegExp; } if (obj instanceof DataView) return new Constructor(obj.buffer.slice(0)); if (obj instanceof Error) { let newError; if (obj instanceof AggregateError) newError = new Constructor(obj.errors, obj.message, { cause: obj.cause }); else newError = new Constructor(obj.message, { cause: obj.cause }); newError.stack = obj.stack; Object.assign(newError, obj); return newError; } if (typeof File !== "undefined" && obj instanceof File) return new Constructor([obj], obj.name, { type: obj.type, lastModified: obj.lastModified }); if (typeof obj === "object") return Object.assign(Object.create(prototype), obj); return obj; } //#endregion export { clone };