UNPKG

@signaldb/core

Version:

SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON in

48 lines (47 loc) 1.8 kB
//#region src/utils/deepClone.ts /** * Performs a deep clone of a value, supporting various types including arrays, objects, * Maps, Sets, Dates, and RegExps. Functions are not supported and will throw an error. * @template T - The type of the value to clone. * @param value - The value to deep clone. * @returns A deep copy of the provided value. * @throws {Error} An error if the value is a function, as cloning functions is not supported. */ function clone(value) { if (typeof value === "function") throw new Error("Cloning functions is not supported"); if (value === null || typeof value !== "object") return value; if (value instanceof Date) return new Date(value); if (Array.isArray(value)) return value.map((item) => clone(item)); if (value instanceof Map) { const result = /* @__PURE__ */ new Map(); value.forEach((currentValue, key) => { result.set(key, clone(currentValue)); }); return result; } if (value instanceof Set) { const result = /* @__PURE__ */ new Set(); value.forEach((currentValue) => { result.add(clone(currentValue)); }); return result; } if (value instanceof RegExp) return new RegExp(value); const result = {}; for (const key in value) if (Object.hasOwnProperty.call(value, key)) result[key] = clone(value[key]); return result; } /** * Creates a deep clone of an object. Uses the `structuredClone` function if available, * otherwise falls back to a manual deep clone implementation. * @template T - The type of the object to clone. * @param object - The object to deep clone. * @returns A deep copy of the provided object. */ function deepClone(object) { if (typeof structuredClone === "function") return structuredClone(object); /* istanbul ignore next -- @preserve */ return clone(object); } //#endregion export { deepClone as default };