pgh-common-utils
Version:
A collection of utility functions for TypeScript.
17 lines (16 loc) • 481 B
JavaScript
export function deepClone(obj) {
if (obj === null || typeof obj !== 'object')
return obj;
if (obj instanceof Date)
return new Date(obj.getTime());
if (obj instanceof RegExp)
return new RegExp(obj);
const clone = Array.isArray(obj) ? [] : {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
// @ts-ignore
clone[key] = deepClone(obj[key]);
}
}
return clone;
}