@makerx/ts-dossier
Version:
A support library to facilitate the easy creation of builders for use with an Object-Mother test pattern in TypeScript
60 lines (58 loc) • 2.32 kB
JavaScript
/* eslint-disable @typescript-eslint/no-explicit-any */
// Ref: https://javascript.plainenglish.io/deep-clone-an-object-and-preserve-its-type-with-typescript-d488c35e5574
// TODO: Make this the fallback when using classes or when structuredClone is unavailable
/**
* Deeply clones the supplied source
*
* ```typescript
* const source = { a: 1, b: { a: 2 } }
* const cloneA = deepClone(source)
* console.log(cloneA !== source) // Outputs false
* ```
*
* @template T
* @param {T} source The source value to clone
* @return {T} A clone of the source
*/
function deepClone(source) {
if (Array.isArray(source))
return source.map((item) => deepClone(item));
if (source instanceof Int8Array)
return new Int8Array(source);
if (source instanceof Uint8Array)
return new Uint8Array(source);
if (source instanceof Uint8ClampedArray)
return new Uint8ClampedArray(source);
if (source instanceof Int16Array)
return new Int16Array(source);
if (source instanceof Uint16Array)
return new Uint16Array(source);
if (source instanceof Int32Array)
return new Int32Array(source);
if (source instanceof Uint32Array)
return new Uint32Array(source);
if (source instanceof Float32Array)
return new Float32Array(source);
if (source instanceof Float64Array)
return new Float64Array(source);
if (source instanceof BigInt64Array)
return new BigInt64Array(source);
if (source instanceof BigUint64Array)
return new BigUint64Array(source);
if (source instanceof Date)
return new Date(source.getTime());
if (source && typeof source === 'object') {
// Support mongoose ObjectId
if (Object.getPrototypeOf(source).constructor?.name === 'ObjectId' && Object.getPrototypeOf(source)._bsontype === 'ObjectID') {
return Object.getPrototypeOf(source).constructor(source);
}
return Object.getOwnPropertyNames(source).reduce((o, prop) => {
Object.defineProperty(o, prop, Object.getOwnPropertyDescriptor(source, prop));
o[prop] = deepClone(source[prop]);
return o;
}, Object.create(Object.getPrototypeOf(source)));
}
return source;
}
export { deepClone };
//# sourceMappingURL=deep-clone.mjs.map