Skip to content

Commit

Permalink
fix(util/clone): Support cloning class-based context instances (#228)
Browse files Browse the repository at this point in the history
* fix(util/clone): Use native `structuredClone` for deep copying

* Bump to `1.1.23`

* Back to use klona
  • Loading branch information
zharinov committed May 12, 2023
1 parent 5ca7eb2 commit cb0d3b8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
50 changes: 50 additions & 0 deletions lib/util/clone.spec.ts
@@ -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());
});
});
});
2 changes: 1 addition & 1 deletion lib/util/clone.ts
@@ -1 +1 @@
export { klona as clone } from 'klona/json';
export { klona as clone } from 'klona/lite';
4 changes: 2 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "good-enough-parser",
"description": "Parse and query computer programs source code",
"version": "1.1.22",
"version": "1.1.23",
"repository": "https://github.com/zharinov/good-enough-parser.git",
"author": "Sergei Zharinov",
"contributors": [
Expand Down Expand Up @@ -40,7 +40,7 @@
"dependencies": {
"@thi.ng/zipper": "1.0.3",
"@types/moo": "0.5.5",
"klona": "2.0.5",
"klona": "2.0.6",
"moo": "0.5.2"
},
"devDependencies": {
Expand Down
9 changes: 4 additions & 5 deletions yarn.lock
Expand Up @@ -1049,7 +1049,6 @@

"@renovate/eslint-plugin@https://github.com/renovatebot/eslint-plugin#v0.0.5":
version "0.0.5"
uid "1923e2e2776549cb25a2c6ea09915ee31bff6d05"
resolved "https://github.com/renovatebot/eslint-plugin#1923e2e2776549cb25a2c6ea09915ee31bff6d05"

"@sinclair/typebox@^0.24.1":
Expand Down Expand Up @@ -4317,10 +4316,10 @@ kleur@^3.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==

klona@2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/klona/-/klona-2.0.5.tgz#d166574d90076395d9963aa7a928fabb8d76afbc"
integrity sha512-pJiBpiXMbt7dkzXe8Ghj/u4FfXOOa98fPW+bihOJ4SjnoijweJrNThJfd3ifXpXhREjpoF2mZVH1GfS9LV3kHQ==
klona@2.0.6:
version "2.0.6"
resolved "https://registry.npmjs.org/klona/-/klona-2.0.6.tgz#85bffbf819c03b2f53270412420a4555ef882e22"
integrity sha512-dhG34DXATL5hSxJbIexCft8FChFXtmskoZYnoPWjXQuebWYCNkVeV3KkGegCK9CP1oswI/vQibS2GY7Em/sJJA==

latest-version@^7.0.0:
version "7.0.0"
Expand Down

0 comments on commit cb0d3b8

Please sign in to comment.