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

feat: allow number in nonce callback #5

Merged
merged 13 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.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)

Expand All @@ -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))
- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3))
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<number>;
// }
```

**Footnotes**:
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TsonType } from "../types.js";
* Prevents `NaN` and `Infinity` from being serialized
*/

export const tsonNumber: TsonType<number, number> = {
export const tsonNumberGuard: TsonType<number, number> = {
primitive: "number",
test: (v) => {
const value = v as number;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export type TsonType<
> = TsonTypeTester & TsonTransformer<TValue, TSerializedType>;

export interface TsonOptions {
nonce?: () => string;
nonce?: () => number | string;
types: (TsonType<any, any> | TsonType<any, never>)[];
}

Expand Down
Loading