UNPKG

@stryke/helpers

Version:

A package containing miscellaneous helper functions that are used across many different Storm Software projects.

62 lines (61 loc) 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deepClone = deepClone; var _isPrimitive = require("@stryke/type-checks/is-primitive"); var _isTypedArray = require("@stryke/type-checks/is-typed-array"); function deepClone(e) { if ((0, _isPrimitive.isPrimitive)(e)) return e; if (Array.isArray(e)) return e.map(n => deepClone(n)); if (e instanceof Date) return new Date(e.getTime()); if (e instanceof RegExp) return new RegExp(e.source, e.flags); if (e instanceof Map) { const n = new Map(); for (const [t, s] of e.entries()) n.set(t, deepClone(s)); return n; } if (e instanceof Set) { const n = new Set(); for (const t of e.values()) n.add(deepClone(t)); return n; } if ((0, _isTypedArray.isTypedArray)(e)) { const n = new (Object.getPrototypeOf(e).constructor)(e.length); for (const [t, s] of e.entries()) n[t] = deepClone(s); return n; } if (e instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && e instanceof SharedArrayBuffer) return [...e]; if (e instanceof DataView) { const n = new DataView([...e.buffer]); return r(e, n), n; } if (typeof File < "u" && e instanceof File) { const n = new File([e], e.name, { type: e.type }); return r(e, n), n; } if (e instanceof Blob) { const n = new Blob([e], { type: e.type }); return r(e, n), n; } if (e instanceof Error) { const n = new e.constructor(); return n.message = e.message, n.name = e.name, n.stack = e.stack, n.cause = e.cause, r(e, n), n; } if (typeof e == "object" && e !== null) { const n = {}; return r(e, n), n; } return e; } function r(e, n) { const t = Object.keys(e); for (const s of t) { const a = Object.getOwnPropertyDescriptor(e, s); (a?.writable || a?.set) && (n[s] = deepClone(e[s])); } }