This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { test } from "vitest"; | ||
|
||
import { | ||
createTupleson, | ||
// Serialize BigInt | ||
tsonBigint, | ||
// Serialize Map | ||
tsonMap, | ||
// **throws** when encountering Infinity or NaN | ||
// tsonNumberGuard, | ||
// Serialize regular expression | ||
tsonRegExp, | ||
// Serialize sets | ||
tsonSet, | ||
// Serialize URLs | ||
// tsonURL, | ||
// Serialize undefined | ||
tsonUndefined, | ||
} 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; | ||
// ^? | ||
// type Obj = { | ||
// readonly foo: "bar"; | ||
// readonly set: Set<number>; | ||
// } | ||
}); |