-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move message merging utilities to shared module (#1635)
- Loading branch information
1 parent
be32a37
commit 9d337bf
Showing
5 changed files
with
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { hasOwn, isArray, isObject } from './utils' | ||
|
||
const isNotObjectOrIsArray = (val: unknown) => !isObject(val) || isArray(val) | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
export function deepCopy(src: any, des: any): void { | ||
// src and des should both be objects, and none of them can be a array | ||
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) { | ||
throw new Error('Invalid value') | ||
} | ||
|
||
for (const key in src) { | ||
if (hasOwn(src, key)) { | ||
if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) { | ||
// replace with src[key] when: | ||
// src[key] or des[key] is not an object, or | ||
// src[key] or des[key] is an array | ||
des[key] = src[key] | ||
} else { | ||
// src[key] and des[key] are both objects, merge them | ||
deepCopy(src[key], des[key]) | ||
} | ||
} | ||
} | ||
} |
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