From d2fcb7f5175705bf1ec01fc67b8ed2feacdcac7d Mon Sep 17 00:00:00 2001 From: Alex / KATT Date: Sat, 30 Sep 2023 22:54:21 +0200 Subject: [PATCH] feat: allow `number` in `nonce` callback (#5) feat: - allow `number` in `nonce` callback - export `tsonNumberGuard` - update README --- CHANGELOG.md | 7 +-- README.md | 51 ++++++++++++++++++- src/handlers/index.ts | 3 +- ...Number.test.ts => tsonNumberGuard.test.ts} | 4 +- .../{tsonNumber.ts => tsonNumberGuard.ts} | 2 +- src/types.ts | 2 +- 6 files changed, 58 insertions(+), 11 deletions(-) rename src/handlers/{tsonNumber.test.ts => tsonNumberGuard.test.ts} (89%) rename src/handlers/{tsonNumber.ts => tsonNumberGuard.ts} (85%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 893ec132..f3e283f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,8 @@ - - ## [0.3.1](https://github.com/KATT/tupleson/compare/0.3.0...0.3.1) (2023-09-30) - ### Bug Fixes -* `serializer` -> `serialize` ([#4](https://github.com/KATT/tupleson/issues/4)) ([93a45a5](https://github.com/KATT/tupleson/commit/93a45a5e32ad1ebddba6283627551830c1e621ec)) +- `serializer` -> `serialize` ([#4](https://github.com/KATT/tupleson/issues/4)) ([93a45a5](https://github.com/KATT/tupleson/commit/93a45a5e32ad1ebddba6283627551830c1e621ec)) # [0.3.0](https://github.com/KATT/tupleson/compare/0.2.0...0.3.0) (2023-09-30) @@ -24,4 +21,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 387af25d..83d41ef0 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,36 @@ A hackable JSON serializer/deserializer that allows you to serialize/deserialize ### Example ```ts +/* eslint-disable eslint-comments/disable-enable-pair, @typescript-eslint/no-unused-vars, n/no-missing-import */ + +import { + createTupleson, + // Serialize `bigint` + tsonBigint, + // Serialize `Map`s + tsonMap, + // **throws** when encountering Infinity or NaN + tsonNumberGuard, + // Serialize regular expression + tsonRegExp, + // Serialize `Set`s + tsonSet, + // Serialize `URL`s + tsonURL, + // Serialize `undefined` + tsonUndefined, + // **throws** when encountering non-registered complex objects (like class instances) + tsonUnknownObjectGuard, +} from "tupleson"; + const json = createTupleson({ // This nonce function is used to generate a nonce for the serialized value // This is used to identify the value as a serialized value nonce: () => "__tson", - types: [tsonSet], + types: [ + // Pick which types you want to support + tsonSet, + ], }); const result = json.stringify( @@ -45,6 +70,30 @@ const result = json.stringify( 2, ); console.log(result); +// -> +// { +// "json": { +// "foo": "bar", +// "set": [ +// "Set", +// [ +// 1, +// 2, +// 3 +// ], +// "__tson" +// ] +// }, +// "nonce": "__tson" +// } + +// ✨ Retains type integrity +type Obj = typeof obj; +// ^? +// type Obj = { +// readonly foo: "bar"; +// readonly set: Set; +// } ``` **Footnotes**: diff --git a/src/handlers/index.ts b/src/handlers/index.ts index f2966607..66da9100 100644 --- a/src/handlers/index.ts +++ b/src/handlers/index.ts @@ -5,4 +5,5 @@ export * from "./tsonSet.js"; export * from "./tsonMap.js"; export * from "./tsonUndefined.js"; export * from "./tsonUnknownObjectGuard.js"; -export * from "./tsonNumber.js"; +export * from "./tsonNumberGuard.js"; +export * from "./tsonURL.js"; diff --git a/src/handlers/tsonNumber.test.ts b/src/handlers/tsonNumberGuard.test.ts similarity index 89% rename from src/handlers/tsonNumber.test.ts rename to src/handlers/tsonNumberGuard.test.ts index 3e3acc91..4f70f9e9 100644 --- a/src/handlers/tsonNumber.test.ts +++ b/src/handlers/tsonNumberGuard.test.ts @@ -2,11 +2,11 @@ import { expect, test } from "vitest"; import { expectError } from "../testUtils.js"; import { createTupleson } from "../tson.js"; -import { tsonNumber } from "./tsonNumber.js"; +import { tsonNumberGuard } from "./tsonNumberGuard.js"; test("number", () => { const t = createTupleson({ - types: [tsonNumber], + types: [tsonNumberGuard], }); const bad = [ diff --git a/src/handlers/tsonNumber.ts b/src/handlers/tsonNumberGuard.ts similarity index 85% rename from src/handlers/tsonNumber.ts rename to src/handlers/tsonNumberGuard.ts index 70677b99..f27b2782 100644 --- a/src/handlers/tsonNumber.ts +++ b/src/handlers/tsonNumberGuard.ts @@ -4,7 +4,7 @@ import { TsonType } from "../types.js"; * Prevents `NaN` and `Infinity` from being serialized */ -export const tsonNumber: TsonType = { +export const tsonNumberGuard: TsonType = { primitive: "number", test: (v) => { const value = v as number; diff --git a/src/types.ts b/src/types.ts index a03940f9..99584f45 100644 --- a/src/types.ts +++ b/src/types.ts @@ -95,7 +95,7 @@ export type TsonType< > = TsonTypeTester & TsonTransformer; export interface TsonOptions { - nonce?: () => string; + nonce?: () => number | string; types: (TsonType | TsonType)[]; }