-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use
klona
library for deep clone (#22979)
- Loading branch information
Showing
7 changed files
with
110 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
import { klona } from 'klona/json'; | ||
import { logger } from '../logger'; | ||
import { quickStringify } from './stringify'; | ||
|
||
/** | ||
* Creates a deep clone of an object. | ||
* @deprecated Use {@link structuredClone} instead. | ||
* @param input The object to clone. | ||
*/ | ||
export function clone<T>(input: T | null = null): T { | ||
const stringifiedInput = quickStringify(input); | ||
return stringifiedInput ? JSON.parse(stringifiedInput) : null; | ||
export function clone<T = unknown>(input: T): T { | ||
try { | ||
return klona(input); | ||
} catch (err) { | ||
logger.warn({ err }, 'error cloning object'); | ||
const str = quickStringify(input); | ||
if (str) { | ||
return JSON.parse(str); | ||
} | ||
|
||
// istanbul ignore next: not easily testable | ||
throw err; | ||
} | ||
} |
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