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

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Sep 30, 2023
1 parent 1b6a4fc commit 9d8ffea
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ 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;

Check failure on line 90 in README.md

View workflow job for this annotation

GitHub Actions / lint

'Obj' is defined but never used
// ^?
// type Obj = {
// readonly foo: "bar";
// readonly set: Set<number>;
// }
```

**Footnotes**:
Expand Down
68 changes: 68 additions & 0 deletions src/readme.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

Check failure on line 1 in src/readme.test.ts

View workflow job for this annotation

GitHub Actions / lint

Requires 'eslint-enable' directive for '@typescript-eslint/no-unused-vars'
import { test } from "vitest";

import {
createTupleson,
// Serialize BigInt
tsonBigint,

Check failure on line 7 in src/readme.test.ts

View workflow job for this annotation

GitHub Actions / type_check

'tsonBigint' is declared but its value is never read.
// Serialize Map
tsonMap,

Check failure on line 9 in src/readme.test.ts

View workflow job for this annotation

GitHub Actions / type_check

'tsonMap' is declared but its value is never read.
// **throws** when encountering Infinity or NaN
// tsonNumberGuard,
// Serialize regular expression
tsonRegExp,

Check failure on line 13 in src/readme.test.ts

View workflow job for this annotation

GitHub Actions / type_check

'tsonRegExp' is declared but its value is never read.
// Serialize sets
tsonSet,
// Serialize URLs
// tsonURL,
// Serialize undefined
tsonUndefined,

Check failure on line 19 in src/readme.test.ts

View workflow job for this annotation

GitHub Actions / type_check

'tsonUndefined' is declared but its value is never read.
} from "./index.js";

test("readme", () => {
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: [
// Pick which types you want to support
tsonSet,
],
});

const myObj = {
foo: "bar",
set: new Set([1, 2, 3]),
} as const;

const str = json.stringify(myObj, 2);
// console.log(str);
// ->
// {
// "json": {
// "foo": "bar",
// "set": [
// "Set",
// [
// 1,
// 2,
// 3
// ],
// "__tson"
// ]
// },
// "nonce": "__tson"
// }

const obj = json.parse(str);

// console.log(obj);
// -> { foo: 'bar', set: Set(3) { 1, 2, 3 } }

type Obj = typeof obj;

Check failure on line 62 in src/readme.test.ts

View workflow job for this annotation

GitHub Actions / type_check

'Obj' is declared but never used.
// ^?
// type Obj = {
// readonly foo: "bar";
// readonly set: Set<number>;
// }
});

0 comments on commit 9d8ffea

Please sign in to comment.