mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
perf: update deepCopy (#4364)
* perf: update deepCopy * fix: using deepCopy in core and cli packages * fix: using deepCopy in editor * chore: formatting * fix: some micro optimisation in deepCopy
This commit is contained in:
@@ -1 +1,32 @@
|
||||
export const deepCopy = <T>(toCopy: T) => JSON.parse(JSON.stringify(toCopy)) as T;
|
||||
/* 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 */
|
||||
export const deepCopy = <T>(source: T): T => {
|
||||
let clone: any;
|
||||
let i: any;
|
||||
const hasOwnProp = Object.prototype.hasOwnProperty.bind(source);
|
||||
// Primitives & Null
|
||||
if (typeof source !== 'object' || source === null) {
|
||||
return source;
|
||||
}
|
||||
// Date
|
||||
if (source instanceof Date) {
|
||||
return new Date(source.getTime()) as T;
|
||||
}
|
||||
// Array
|
||||
if (Array.isArray(source)) {
|
||||
clone = [];
|
||||
const len = source.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
clone[i] = deepCopy(source[i]);
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
// Object
|
||||
clone = {};
|
||||
for (i in source) {
|
||||
if (hasOwnProp(i)) {
|
||||
clone[i] = deepCopy((source as any)[i]);
|
||||
}
|
||||
}
|
||||
return clone;
|
||||
};
|
||||
// eslint-enable
|
||||
|
||||
Reference in New Issue
Block a user