mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(core): keep backward compatibility in deepCopy by calling toJSON
on objects that have it
This commit is contained in:
parent
19b8139264
commit
e237dab3ed
|
@ -1,37 +1,41 @@
|
||||||
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return */
|
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument */
|
||||||
|
type Primitives = string | number | boolean | bigint | symbol | null | undefined;
|
||||||
type Serializable = { toJSON?: () => string };
|
export const deepCopy = <T extends ((object | Date) & { toJSON?: () => string }) | Primitives>(
|
||||||
|
source: T,
|
||||||
export const deepCopy = <T>(source: T, hash = new WeakMap(), path = ''): T => {
|
hash = new WeakMap(),
|
||||||
|
path = '',
|
||||||
|
): T => {
|
||||||
let clone: any;
|
let clone: any;
|
||||||
|
let i: any;
|
||||||
const hasOwnProp = Object.prototype.hasOwnProperty.bind(source);
|
const hasOwnProp = Object.prototype.hasOwnProperty.bind(source);
|
||||||
// Primitives & Null & Function
|
// Primitives & Null & Function
|
||||||
if (typeof source !== 'object' || source === null || source instanceof Function) {
|
if (typeof source !== 'object' || source === null || typeof source === 'function') {
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
// Date and other Serializable objects
|
// TODO: remove this when other code parts not expecting objects with `.toJSON` method called
|
||||||
const toJSON = (source as Serializable).toJSON;
|
if (typeof source.toJSON === 'function') {
|
||||||
if (typeof toJSON === 'function') {
|
return source.toJSON() as T;
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
||||||
return toJSON.call(source) as T;
|
|
||||||
}
|
}
|
||||||
// Break any cyclic dependencies
|
|
||||||
if (hash.has(source)) {
|
if (hash.has(source)) {
|
||||||
return hash.get(source);
|
return hash.get(source);
|
||||||
}
|
}
|
||||||
|
// Date
|
||||||
|
if (source instanceof Date) {
|
||||||
|
return new Date(source.getTime()) as T;
|
||||||
|
}
|
||||||
// Array
|
// Array
|
||||||
if (Array.isArray(source)) {
|
if (Array.isArray(source)) {
|
||||||
clone = [];
|
clone = [];
|
||||||
const len = source.length;
|
const len = source.length;
|
||||||
for (let i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
clone[i] = deepCopy(source[i], hash, path + `[${i}]`);
|
clone[i] = deepCopy(source[i], hash, path + `[${i as string}]`);
|
||||||
}
|
}
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
// Object
|
// Object
|
||||||
clone = {};
|
clone = {};
|
||||||
hash.set(source, clone);
|
hash.set(source, clone);
|
||||||
for (const i in source) {
|
for (i in source) {
|
||||||
if (hasOwnProp(i)) {
|
if (hasOwnProp(i)) {
|
||||||
clone[i] = deepCopy((source as any)[i], hash, path + `.${i as string}`);
|
clone[i] = deepCopy((source as any)[i], hash, path + `.${i as string}`);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue