Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(util/clone): Support cloning class-based context instances (#228)
* fix(util/clone): Use native `structuredClone` for deep copying * Bump to `1.1.23` * Back to use klona
- Loading branch information
Showing
4 changed files
with
57 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { clone } from './clone'; | ||
|
||
describe('util/clone', () => { | ||
describe('primitives', () => { | ||
it.each` | ||
value | ||
${1} | ||
${'a'} | ||
${true} | ||
${false} | ||
${null} | ||
${undefined} | ||
`('$value -> $value', ({ value }) => { | ||
const cloned = clone<unknown>(value); | ||
expect(cloned).toBe(value); | ||
}); | ||
}); | ||
|
||
describe('non-primitives', () => { | ||
it.each` | ||
from | to | ||
${[]} | ${[]} | ||
${[1, 2, 3]} | ${[1, 2, 3]} | ||
${{}} | ${{}} | ||
${{ a: 1, b: 2 }} | ${{ a: 1, b: 2 }} | ||
`('$from -> $to', ({ from, to }) => { | ||
const cloned = clone<unknown>(from); | ||
expect(cloned).toEqual(to); | ||
expect(cloned).not.toBe(from); | ||
}); | ||
|
||
test('classes', () => { | ||
class Test { | ||
constructor(private data: { value: number }) {} | ||
|
||
getData(): { value: number } { | ||
return this.data; | ||
} | ||
} | ||
|
||
const a = new Test({ value: 42 }); | ||
const b = clone(a); | ||
|
||
expect(b).toEqual(a); | ||
expect(b).not.toBe(a); | ||
expect(b.getData()).toEqual(a.getData()); | ||
expect(b.getData()).not.toBe(a.getData()); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
export { klona as clone } from 'klona/json'; | ||
export { klona as clone } from 'klona/lite'; |
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