Fix deepClone so we can overwrite with undefined in when we mock kcContext

This commit is contained in:
garronej 2021-11-08 19:33:06 +01:00
parent 41ee7e90ef
commit 3ecb63d500
2 changed files with 17 additions and 29 deletions

View File

@ -1,32 +1,6 @@
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
import { is } from "tsafe/is"; import { is } from "tsafe/is";
import { deepClone } from "./deepClone";
function deepClone<T>(src: T): T {
const generateId = (() => {
const prefix = "xIfKdLsIIdJdLdOeJqePe";
let counter = 0;
return () => `${prefix}${counter++}`;
})();
const map = new Map<string, Function>();
return JSON.parse(
JSON.stringify(src, (...[, value]) => {
if (typeof value === "function") {
const id = generateId();
map.set(id, value);
return id;
}
return value;
}),
(...[, value]) => (typeof value === "string" && map.has(value) ? map.get(value) : value),
);
}
//Warning: Be mindful that because of array this is not idempotent. //Warning: Be mindful that because of array this is not idempotent.
export function deepAssign(params: { target: Record<string, unknown>; source: Record<string, unknown> }) { export function deepAssign(params: { target: Record<string, unknown>; source: Record<string, unknown> }) {

View File

@ -1,3 +1,17 @@
export function deepClone<T>(arg: T): T { import "minimal-polyfills/Object.fromEntries";
return JSON.parse(JSON.stringify(arg));
export function deepClone<T>(o: T): T {
if (!(o instanceof Object)) {
return o;
}
if (typeof o === "function") {
return o;
}
if (o instanceof Array) {
return o.map(deepClone) as any;
}
return Object.fromEntries(Object.entries(o).map(([key, value]) => [key, deepClone(value)])) as any;
} }