Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

chore: add more extension examples to readme #10

Merged
merged 5 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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))
- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3))
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -118,14 +118,14 @@ import { TsonType, createTson } from "tupleson";
const plainDate: TsonType<Temporal.PlainDate, string> = {
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<Temporal.Instant, string> = {
deserialize: (v) => Temporal.Instant.from(v),
key: "Instant",
serialize: (v) => v.toString(),
serialize: (v) => v.toJSON(),
test: (v) => v instanceof Temporal.Instant,
};

Expand All @@ -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<Decimal, string> = {
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!

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"test": "vitest",
"tsc": "tsc"
},
"dependencies": {},
"devDependencies": {
"@js-temporal/polyfill": "^0.4.4",
"@release-it/conventional-changelog": "^7.0.2",
Expand All @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/extend/decimal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Decimal } from "decimal.js";
import { expect, test } from "vitest";

import { TsonType, createTson } from "../index.js";

const decimalJs: TsonType<Decimal, string> = {
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);
});
Loading