From 004dafa27620c6495fec3e0db02c098daa4aac2e Mon Sep 17 00:00:00 2001 From: KATT Date: Sat, 30 Sep 2023 23:40:57 +0200 Subject: [PATCH 1/5] decimaljs --- CHANGELOG.md | 7 ++----- README.md | 24 ++++++++++++++++++++---- package.json | 2 ++ pnpm-lock.yaml | 7 +++++++ src/extend/Decimal.js.test.ts | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 src/extend/Decimal.js.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fe40b8b..c3e7d959 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,8 @@ - - # [0.6.0](https://github.com/KATT/tupleson/compare/0.5.0...0.6.0) (2023-09-30) - ### Features -* add custom serializer example ([#8](https://github.com/KATT/tupleson/issues/8)) ([58eab05](https://github.com/KATT/tupleson/commit/58eab05baff0dd07803b5aa01c402fcd02df5b09)) +- add custom serializer example ([#8](https://github.com/KATT/tupleson/issues/8)) ([58eab05](https://github.com/KATT/tupleson/commit/58eab05baff0dd07803b5aa01c402fcd02df5b09)) # [0.5.0](https://github.com/KATT/tupleson/compare/0.4.0...0.5.0) (2023-09-30) @@ -42,4 +39,4 @@ ### Features - initial version ([#1](https://github.com/KATT/tupleson/issues/1)) ([ccce25b](https://github.com/KATT/tupleson/commit/ccce25b6a039cf2e5c1a774c1ab022f0946ca8d5)) -- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3)) \ No newline at end of file +- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3)) diff --git a/README.md b/README.md index b4b13d08..3624de78 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ type Obj = typeof obj; ### Extend with a custom serializer -#### `Temporal` +#### [Temporal](https://www.npmjs.com/package/@js-temporal/polyfill) > See test reference in [`./src/extend/temporal.test.ts`](./src/extend/temporal.test.ts) @@ -118,14 +118,14 @@ import { TsonType, createTson } from "tupleson"; const plainDate: TsonType = { deserialize: (v) => Temporal.PlainDate.from(v), key: "PlainDate", - serialize: (v) => v.toString(), + serialize: (v) => v.toJSON(), test: (v) => v instanceof Temporal.PlainDate, }; const instant: TsonType = { deserialize: (v) => Temporal.Instant.from(v), key: "Instant", - serialize: (v) => v.toString(), + serialize: (v) => v.toJSON(), test: (v) => v instanceof Temporal.Instant, }; @@ -134,7 +134,23 @@ const tson = createTson({ }); ``` -**Footnotes**: +#### [Decimal.js](https://github.com/MikeMcl/decimal.js) + +```ts +/* eslint-disable eslint-comments/disable-enable-pair, @typescript-eslint/no-unused-vars, n/no-missing-import, n/no-unpublished-import */ +import { Decimal } from "decimal.js"; + +const decimaljs: TsonType = { + deserialize: (v) => new Decimal(v), + key: "Decimal", + serialize: (v) => v.toJSON(), + test: (v) => v instanceof Decimal, +}; + +const tson = createTson({ + types: [decimaljs], +}); +``` [^1]: We don't support circular references as we don't think it's very desireable, but if you wanna contribute with adding opt-in support for that, you are very welcome! diff --git a/package.json b/package.json index cbddae9a..86a99786 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "test": "vitest", "tsc": "tsc" }, + "dependencies": {}, "devDependencies": { "@js-temporal/polyfill": "^0.4.4", "@release-it/conventional-changelog": "^7.0.2", @@ -41,6 +42,7 @@ "@vitest/coverage-v8": "^0.34.6", "console-fail-test": "^0.2.3", "cspell": "^7.3.7", + "decimal.js": "^10.4.3", "eslint": "^8.50.0", "eslint-plugin-deprecation": "^2.0.0", "eslint-plugin-eslint-comments": "^3.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bc24daaa..9553f4b6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,6 +32,9 @@ devDependencies: cspell: specifier: ^7.3.7 version: 7.3.7 + decimal.js: + specifier: ^10.4.3 + version: 10.4.3 eslint: specifier: ^8.50.0 version: 8.50.0 @@ -2376,6 +2379,10 @@ packages: engines: {node: '>=0.10.0'} dev: true + /decimal.js@10.4.3: + resolution: {integrity: sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==} + dev: true + /decompress-response@6.0.0: resolution: {integrity: sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==} engines: {node: '>=10'} diff --git a/src/extend/Decimal.js.test.ts b/src/extend/Decimal.js.test.ts new file mode 100644 index 00000000..b7f9d338 --- /dev/null +++ b/src/extend/Decimal.js.test.ts @@ -0,0 +1,35 @@ +import { Decimal } from "decimal.js"; +import { expect, test } from "vitest"; + +import { TsonType, createTson } from "../index.js"; + +const decimaljs: TsonType = { + deserialize: (v) => new Decimal(v), + key: "Decimal", + serialize: (v) => v.toJSON(), + test: (v) => v instanceof Decimal, +}; + +const tson = createTson({ + types: [decimaljs], +}); + +test("Decimal.js", () => { + const expected = new Decimal(1.23); + const serialized = tson.serialize(expected); + + expect(serialized).toMatchInlineSnapshot(` + { + "json": [ + "Decimal", + "1.23", + "__tson", + ], + "nonce": "__tson", + } + `); + + const deserialized = tson.deserialize(serialized); + + expect(deserialized).toEqual(expected); +}); From 20381b547332c1da87f2c7d3728cad1f9d1e98ca Mon Sep 17 00:00:00 2001 From: KATT Date: Sat, 30 Sep 2023 23:41:14 +0200 Subject: [PATCH 2/5] decimal --- src/extend/{Decimal.js.test.ts => decimaljs.test.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/extend/{Decimal.js.test.ts => decimaljs.test.ts} (100%) diff --git a/src/extend/Decimal.js.test.ts b/src/extend/decimaljs.test.ts similarity index 100% rename from src/extend/Decimal.js.test.ts rename to src/extend/decimaljs.test.ts From 2dd0f5397df18a8b15ad2ace8b8426b0555ab4bc Mon Sep 17 00:00:00 2001 From: KATT Date: Sat, 30 Sep 2023 23:42:40 +0200 Subject: [PATCH 3/5] extend --- README.md | 2 ++ src/extend/{decimaljs.test.ts => decimal.test.ts} | 0 2 files changed, 2 insertions(+) rename src/extend/{decimaljs.test.ts => decimal.test.ts} (100%) diff --git a/README.md b/README.md index 3624de78..80d10ef3 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,8 @@ const tson = createTson({ #### [Decimal.js](https://github.com/MikeMcl/decimal.js) +> See test reference in [`./src/extend/decimal.test.ts`](./src/extend/decimal.test.ts) + ```ts /* eslint-disable eslint-comments/disable-enable-pair, @typescript-eslint/no-unused-vars, n/no-missing-import, n/no-unpublished-import */ import { Decimal } from "decimal.js"; diff --git a/src/extend/decimaljs.test.ts b/src/extend/decimal.test.ts similarity index 100% rename from src/extend/decimaljs.test.ts rename to src/extend/decimal.test.ts From c21493143a946338816cdfec8a575a3b85a1dd9a Mon Sep 17 00:00:00 2001 From: KATT Date: Sat, 30 Sep 2023 23:43:40 +0200 Subject: [PATCH 4/5] spell fix --- src/extend/decimal.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extend/decimal.test.ts b/src/extend/decimal.test.ts index b7f9d338..c0c34eb2 100644 --- a/src/extend/decimal.test.ts +++ b/src/extend/decimal.test.ts @@ -3,7 +3,7 @@ import { expect, test } from "vitest"; import { TsonType, createTson } from "../index.js"; -const decimaljs: TsonType = { +const decimalJs: TsonType = { deserialize: (v) => new Decimal(v), key: "Decimal", serialize: (v) => v.toJSON(), @@ -11,7 +11,7 @@ const decimaljs: TsonType = { }; const tson = createTson({ - types: [decimaljs], + types: [decimalJs], }); test("Decimal.js", () => { From 54f7e20422a57a51fa1b385957193e6ddf0ae2ff Mon Sep 17 00:00:00 2001 From: KATT Date: Sat, 30 Sep 2023 23:43:54 +0200 Subject: [PATCH 5/5] spell --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 80d10ef3..a57fa5c4 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ const tson = createTson({ /* eslint-disable eslint-comments/disable-enable-pair, @typescript-eslint/no-unused-vars, n/no-missing-import, n/no-unpublished-import */ import { Decimal } from "decimal.js"; -const decimaljs: TsonType = { +const decimalJs: TsonType = { deserialize: (v) => new Decimal(v), key: "Decimal", serialize: (v) => v.toJSON(), @@ -150,7 +150,7 @@ const decimaljs: TsonType = { }; const tson = createTson({ - types: [decimaljs], + types: [decimalJs], }); ```