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..a57fa5c4 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,25 @@ const tson = createTson({ }); ``` -**Footnotes**: +#### [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"; + +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.test.ts b/src/extend/decimal.test.ts new file mode 100644 index 00000000..c0c34eb2 --- /dev/null +++ b/src/extend/decimal.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); +});