forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* perf: update deepCopy * fix: using deepCopy in core and cli packages * fix: using deepCopy in editor * chore: formatting * fix: some micro optimisation in deepCopy
- Loading branch information
Showing
14 changed files
with
101 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { deepCopy } from './utils'; | ||
|
||
describe('deepCopy', () => { | ||
it('should deep copy an object', () => { | ||
const object = { | ||
deep: { | ||
props: { | ||
list: [{ a: 1 }, { b: 2 }, { c: 3 }], | ||
}, | ||
arr: [1, 2, 3], | ||
}, | ||
arr: [ | ||
{ | ||
prop: { | ||
list: ['a', 'b', 'c'], | ||
}, | ||
}, | ||
], | ||
func: () => {}, | ||
date: new Date(), | ||
undef: undefined, | ||
nil: null, | ||
bool: true, | ||
num: 1, | ||
}; | ||
const copy = deepCopy(object); | ||
expect(copy).toEqual(object); | ||
expect(copy).not.toBe(object); | ||
expect(copy.arr).toEqual(object.arr); | ||
expect(copy.arr).not.toBe(object.arr); | ||
expect(copy.deep.props).toEqual(object.deep.props); | ||
expect(copy.deep.props).not.toBe(object.deep.props); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |