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.
feat: add
createTupleson
fn and more (#2)
- Loading branch information
Showing
30 changed files
with
550 additions
and
464 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
export * from "./tsonBigint.js"; | ||
export * from "./tsonDate.js"; | ||
export * from "./tsonRegExp.js"; | ||
export * from "./tsonSet.js"; | ||
export * from "./tsonMap.js"; | ||
export * from "./tsonUndefined.js"; | ||
export * from "./tsonUnknownObjectGuard.js"; | ||
export * from "./tsonNumber.js"; |
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,42 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import { createTupleson } from "../tson.js"; | ||
import { tsonBigint } from "./tsonBigint.js"; | ||
import { tsonMap } from "./tsonMap.js"; | ||
import { tsonSet } from "./tsonSet.js"; | ||
|
||
test("bigint", () => { | ||
const t = createTupleson({ | ||
types: [tsonMap, tsonSet, tsonBigint], | ||
}); | ||
|
||
{ | ||
// bigint | ||
const expected = 1n; | ||
|
||
const stringified = t.stringify(expected); | ||
const deserialized = t.parse(stringified); | ||
|
||
expect(deserialized).toEqual(expected); | ||
|
||
{ | ||
// set of BigInt | ||
const expected = new Set([1n]); | ||
|
||
const stringified = t.stringify(expected); | ||
const deserialized = t.parse(stringified); | ||
|
||
expect(deserialized).toEqual(expected); | ||
} | ||
|
||
{ | ||
// set of a map of bigint | ||
const expected = new Set([new Map([["a", 1n]])]); | ||
|
||
const stringified = t.stringify(expected); | ||
const deserialized = t.parse(stringified); | ||
|
||
expect(deserialized).toEqual(expected); | ||
} | ||
} | ||
}); |
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,8 @@ | ||
import { TsonType } from "../types.js"; | ||
|
||
export const tsonBigint: TsonType<bigint, string> = { | ||
deserialize: (v) => BigInt(v), | ||
key: "bigint", | ||
primitive: "bigint", | ||
serialize: (v) => v.toString(), | ||
}; |
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,16 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import { createTupleson } from "../tson.js"; | ||
import { tsonDate } from "./tsonDate.js"; | ||
|
||
test("Date", () => { | ||
const ctx = createTupleson({ | ||
types: [tsonDate], | ||
}); | ||
|
||
const date = new Date(); | ||
|
||
const stringified = ctx.stringify(date); | ||
const deserialized = ctx.parse(stringified); | ||
expect(deserialized).toEqual(date); | ||
}); |
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,8 @@ | ||
import { TsonType } from "../types.js"; | ||
|
||
export const tsonDate: TsonType<Date, string> = { | ||
deserialize: (value) => new Date(value), | ||
key: "Date", | ||
serialize: (value) => value.toJSON(), | ||
test: (value) => value instanceof Date, | ||
}; |
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,16 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import { createTupleson } from "../tson.js"; | ||
import { tsonMap } from "./tsonMap.js"; | ||
|
||
test("Map", () => { | ||
const t = createTupleson({ | ||
types: [tsonMap], | ||
}); | ||
|
||
const expected = new Map([["a", "b"]]); | ||
|
||
const stringified = t.stringify(expected); | ||
const deserialized = t.parse(stringified); | ||
expect(deserialized).toEqual(expected); | ||
}); |
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,8 @@ | ||
import { TsonType } from "../types.js"; | ||
|
||
export const tsonMap: TsonType<Map<unknown, unknown>, [unknown, unknown][]> = { | ||
deserialize: (v) => new Map(v), | ||
key: "Map", | ||
serialize: (v) => Array.from(v.entries()), | ||
test: (v) => v instanceof Map, | ||
}; |
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,39 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import { expectError } from "../testUtils.js"; | ||
import { createTupleson } from "../tson.js"; | ||
import { tsonNumber } from "./tsonNumber.js"; | ||
|
||
test("number", () => { | ||
const t = createTupleson({ | ||
types: [tsonNumber], | ||
}); | ||
|
||
const bad = [ | ||
// | ||
NaN, | ||
Infinity, | ||
-Infinity, | ||
]; | ||
const good = [1, 0, -1, 1.1, -1.1]; | ||
|
||
const errors: unknown[] = []; | ||
|
||
for (const n of bad) { | ||
const err = expectError(() => t.parse(t.stringify(n))); | ||
errors.push(err); | ||
} | ||
|
||
expect(errors).toMatchInlineSnapshot(` | ||
[ | ||
[Error: Encountered NaN], | ||
[Error: Encountered Infinity], | ||
[Error: Encountered Infinity], | ||
] | ||
`); | ||
|
||
for (const n of good) { | ||
const deserialized = t.parse(t.stringify(n)); | ||
expect(deserialized).toEqual(n); | ||
} | ||
}); |
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,21 @@ | ||
import { TsonType } from "../types.js"; | ||
|
||
/** | ||
* Prevents `NaN` and `Infinity` from being serialized | ||
*/ | ||
|
||
export const tsonNumber: TsonType<number, number> = { | ||
primitive: "number", | ||
test: (v) => { | ||
const value = v as number; | ||
if (isNaN(value)) { | ||
throw new Error("Encountered NaN"); | ||
} | ||
|
||
if (!isFinite(value)) { | ||
throw new Error("Encountered Infinity"); | ||
} | ||
|
||
return false; | ||
}, | ||
}; |
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,33 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import { createTupleson } from "../tson.js"; | ||
import { tsonRegExp } from "./index.js"; | ||
|
||
test("regex", () => { | ||
const t = createTupleson({ | ||
types: [tsonRegExp], | ||
}); | ||
|
||
const expected = /foo/g; | ||
|
||
const stringified = t.stringify(expected, 2); | ||
|
||
expect(stringified).toMatchInlineSnapshot( | ||
` | ||
"{ | ||
\\"json\\": [ | ||
\\"RegExp\\", | ||
\\"/foo/g\\", | ||
\\"__tson\\" | ||
], | ||
\\"nonce\\": \\"__tson\\" | ||
}" | ||
`, | ||
); | ||
|
||
const deserialized = t.parse(stringified); | ||
|
||
expect(deserialized).toBeInstanceOf(RegExp); | ||
expect(deserialized).toMatchInlineSnapshot("/foo/g"); | ||
expect(deserialized + "").toEqual(expected + ""); | ||
}); |
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,12 @@ | ||
import { TsonType } from "../types.js"; | ||
|
||
export const tsonRegExp: TsonType<RegExp, string> = { | ||
deserialize: (str) => { | ||
const body = str.slice(1, str.lastIndexOf("/")); | ||
const flags = str.slice(str.lastIndexOf("/") + 1); | ||
return new RegExp(body, flags); | ||
}, | ||
key: "RegExp", | ||
serialize: (value) => "" + value, | ||
test: (value) => value instanceof RegExp, | ||
}; |
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,16 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import { createTupleson } from "../tson.js"; | ||
import { tsonSet } from "./tsonSet.js"; | ||
|
||
test("Set", () => { | ||
const t = createTupleson({ | ||
types: [tsonSet], | ||
}); | ||
|
||
const expected = new Set(["a", "b"]); | ||
|
||
const stringified = t.stringify(expected); | ||
const deserialized = t.parse(stringified); | ||
expect(deserialized).toEqual(expected); | ||
}); |
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,8 @@ | ||
import { TsonType } from "../types.js"; | ||
|
||
export const tsonSet: TsonType<Set<unknown>, unknown[]> = { | ||
deserialize: (v) => new Set(v), | ||
key: "Set", | ||
serialize: (v) => Array.from(v), | ||
test: (v) => v instanceof Set, | ||
}; |
Oops, something went wrong.