@sapphire/utilities
Version:
Common JavaScript utilities for the Sapphire Community
56 lines (54 loc) • 1.55 kB
JavaScript
import { __name } from '../chunk-PAWJFY3S.mjs';
import { isPrimitive } from './isPrimitive.mjs';
var TypedArrayPrototype = Object.getPrototypeOf(Uint8Array);
function deepClone(source) {
if (source === null || isPrimitive(source)) {
return source;
}
if (source instanceof Date) {
const output = new source.constructor(source);
return output;
}
if (source instanceof TypedArrayPrototype) {
const output = source.constructor.from(source);
return output;
}
if (Array.isArray(source)) {
const output = new source.constructor(source.length);
for (let i = 0; i < source.length; i++) {
output[i] = deepClone(source[i]);
}
return output;
}
if (source instanceof Map) {
const output = new source.constructor();
for (const [key, value] of source.entries()) {
output.set(key, deepClone(value));
}
return output;
}
if (source instanceof Set) {
const output = new source.constructor();
for (const value of source.values()) {
output.add(deepClone(value));
}
return output;
}
if (typeof source === "object") {
const output = new source.constructor();
for (const [key, value] of Object.entries(source)) {
Object.defineProperty(output, key, {
configurable: true,
enumerable: true,
value: deepClone(value),
writable: true
});
}
return output;
}
return source;
}
__name(deepClone, "deepClone");
export { deepClone };
//# sourceMappingURL=deepClone.mjs.map
//# sourceMappingURL=deepClone.mjs.map