function cloneDeep(object, cache = new WeakMap()) {
    if (null === object) return null;
    if (cache.has(object)) return cache.get(object);
    if (object instanceof Date) return new Date(object.getTime());
    if (object instanceof RegExp) return new RegExp(object.source, object.flags);
    if (object instanceof Map) {
        const clonedMap = new Map();
        cache.set(object, clonedMap);
        for (const [key, value] of object.entries())clonedMap.set(key, cloneDeep(value, cache));
        return clonedMap;
    }
    if (object instanceof Set) {
        const clonedSet = new Set();
        cache.set(object, clonedSet);
        for (const value of object.values())clonedSet.add(cloneDeep(value, cache));
        return clonedSet;
    }
    if ('object' == typeof object && null !== object) {
        const clonedObject = Array.isArray(object) ? [] : Object.create(Object.getPrototypeOf(object));
        cache.set(object, clonedObject);
        for(const key in object)if (Object.hasOwn(object, key) || Object.getOwnPropertySymbols(object).includes(key)) clonedObject[key] = cloneDeep(object[key], cache);
        return clonedObject;
    }
    return object;
}
export { cloneDeep };
