From 29496486333e3a794b4cca346e492b782b1837e6 Mon Sep 17 00:00:00 2001 From: KATT Date: Sat, 30 Sep 2023 23:05:44 +0200 Subject: [PATCH 1/3] cool --- CHANGELOG.md | 7 ++----- README.md | 5 +++-- src/handlers/tsonBigint.test.ts | 4 ++-- src/handlers/tsonDate.test.ts | 4 ++-- src/handlers/tsonMap.test.ts | 4 ++-- src/handlers/tsonNumberGuard.test.ts | 4 ++-- src/handlers/tsonRegExp.test.ts | 4 ++-- src/handlers/tsonSet.test.ts | 4 ++-- src/handlers/tsonURL.test.ts | 4 ++-- src/handlers/tsonUndefined.test.ts | 4 ++-- src/handlers/tsonUnknownObjectGuard.test.ts | 4 ++-- src/index.ts | 10 +++++----- src/stringify.test.ts | 4 ++-- src/tson.ts | 22 ++++++++++----------- src/types.test.ts | 4 ++-- 15 files changed, 43 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bbf3935..762368a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,8 @@ - - # [0.4.0](https://github.com/KATT/tupleson/compare/0.3.1...0.4.0) (2023-09-30) - ### Features -* allow `number` in `nonce` callback ([#5](https://github.com/KATT/tupleson/issues/5)) ([d2fcb7f](https://github.com/KATT/tupleson/commit/d2fcb7f5175705bf1ec01fc67b8ed2feacdcac7d)) +- allow `number` in `nonce` callback ([#5](https://github.com/KATT/tupleson/issues/5)) ([d2fcb7f](https://github.com/KATT/tupleson/commit/d2fcb7f5175705bf1ec01fc67b8ed2feacdcac7d)) ## [0.3.1](https://github.com/KATT/tupleson/compare/0.3.0...0.3.1) (2023-09-30) @@ -30,4 +27,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 83d41ef0..558dd0a1 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,8 @@ A hackable JSON serializer/deserializer that allows you to serialize/deserialize /* eslint-disable eslint-comments/disable-enable-pair, @typescript-eslint/no-unused-vars, n/no-missing-import */ import { - createTupleson, + // Create serializer / deserializer + createTson, // Serialize `bigint` tsonBigint, // Serialize `Map`s @@ -52,7 +53,7 @@ import { tsonUnknownObjectGuard, } from "tupleson"; -const json = createTupleson({ +const json = createTson({ // 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", diff --git a/src/handlers/tsonBigint.test.ts b/src/handlers/tsonBigint.test.ts index 50533235..487dc49e 100644 --- a/src/handlers/tsonBigint.test.ts +++ b/src/handlers/tsonBigint.test.ts @@ -1,12 +1,12 @@ import { expect, test } from "vitest"; -import { createTupleson } from "../tson.js"; +import { createTson } from "../tson.js"; import { tsonBigint } from "./tsonBigint.js"; import { tsonMap } from "./tsonMap.js"; import { tsonSet } from "./tsonSet.js"; test("bigint", () => { - const t = createTupleson({ + const t = createTson({ types: [tsonMap, tsonSet, tsonBigint], }); diff --git a/src/handlers/tsonDate.test.ts b/src/handlers/tsonDate.test.ts index 7b549d12..310c82a0 100644 --- a/src/handlers/tsonDate.test.ts +++ b/src/handlers/tsonDate.test.ts @@ -1,10 +1,10 @@ import { expect, test } from "vitest"; -import { createTupleson } from "../tson.js"; +import { createTson } from "../tson.js"; import { tsonDate } from "./tsonDate.js"; test("Date", () => { - const ctx = createTupleson({ + const ctx = createTson({ types: [tsonDate], }); diff --git a/src/handlers/tsonMap.test.ts b/src/handlers/tsonMap.test.ts index 71e73e50..f71b4218 100644 --- a/src/handlers/tsonMap.test.ts +++ b/src/handlers/tsonMap.test.ts @@ -1,10 +1,10 @@ import { expect, test } from "vitest"; -import { createTupleson } from "../tson.js"; +import { createTson } from "../tson.js"; import { tsonMap } from "./tsonMap.js"; test("Map", () => { - const t = createTupleson({ + const t = createTson({ types: [tsonMap], }); diff --git a/src/handlers/tsonNumberGuard.test.ts b/src/handlers/tsonNumberGuard.test.ts index 4f70f9e9..ed347d1e 100644 --- a/src/handlers/tsonNumberGuard.test.ts +++ b/src/handlers/tsonNumberGuard.test.ts @@ -1,11 +1,11 @@ import { expect, test } from "vitest"; import { expectError } from "../testUtils.js"; -import { createTupleson } from "../tson.js"; +import { createTson } from "../tson.js"; import { tsonNumberGuard } from "./tsonNumberGuard.js"; test("number", () => { - const t = createTupleson({ + const t = createTson({ types: [tsonNumberGuard], }); diff --git a/src/handlers/tsonRegExp.test.ts b/src/handlers/tsonRegExp.test.ts index f22df134..2194e748 100644 --- a/src/handlers/tsonRegExp.test.ts +++ b/src/handlers/tsonRegExp.test.ts @@ -1,10 +1,10 @@ import { expect, test } from "vitest"; -import { createTupleson } from "../tson.js"; +import { createTson } from "../tson.js"; import { tsonRegExp } from "./index.js"; test("regex", () => { - const t = createTupleson({ + const t = createTson({ types: [tsonRegExp], }); diff --git a/src/handlers/tsonSet.test.ts b/src/handlers/tsonSet.test.ts index 00c1ad58..800bb3d2 100644 --- a/src/handlers/tsonSet.test.ts +++ b/src/handlers/tsonSet.test.ts @@ -1,10 +1,10 @@ import { expect, test } from "vitest"; -import { createTupleson } from "../tson.js"; +import { createTson } from "../tson.js"; import { tsonSet } from "./tsonSet.js"; test("Set", () => { - const t = createTupleson({ + const t = createTson({ types: [tsonSet], }); diff --git a/src/handlers/tsonURL.test.ts b/src/handlers/tsonURL.test.ts index ba70be89..9f2516d6 100644 --- a/src/handlers/tsonURL.test.ts +++ b/src/handlers/tsonURL.test.ts @@ -1,10 +1,10 @@ import { expect, test } from "vitest"; -import { createTupleson } from "../tson.js"; +import { createTson } from "../tson.js"; import { tsonURL } from "./tsonURL.js"; test("URL", () => { - const ctx = createTupleson({ + const ctx = createTson({ types: [tsonURL], }); diff --git a/src/handlers/tsonUndefined.test.ts b/src/handlers/tsonUndefined.test.ts index 21cfaf0a..4ed68a17 100644 --- a/src/handlers/tsonUndefined.test.ts +++ b/src/handlers/tsonUndefined.test.ts @@ -1,10 +1,10 @@ import { expect, test } from "vitest"; -import { createTupleson } from "../tson.js"; +import { createTson } from "../tson.js"; import { tsonUndefined } from "./tsonUndefined.js"; test("undefined", () => { - const ctx = createTupleson({ + const ctx = createTson({ types: [tsonUndefined], }); diff --git a/src/handlers/tsonUnknownObjectGuard.test.ts b/src/handlers/tsonUnknownObjectGuard.test.ts index 36270841..5c294f34 100644 --- a/src/handlers/tsonUnknownObjectGuard.test.ts +++ b/src/handlers/tsonUnknownObjectGuard.test.ts @@ -1,7 +1,7 @@ import { assert, expect, test } from "vitest"; import { expectError } from "../testUtils.js"; -import { createTupleson } from "../tson.js"; +import { createTson } from "../tson.js"; import { tsonSet } from "./tsonSet.js"; import { UnknownObjectGuardError, @@ -10,7 +10,7 @@ import { test("guard unwanted objects", () => { // Sets are okay, but not Maps - const t = createTupleson({ + const t = createTson({ types: [ tsonSet, // defined last so it runs last diff --git a/src/index.ts b/src/index.ts index c4c93f3d..a5521cdb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ export { - createTupleson, - tsonDeserializer, - tsonParser, - tsonSerializer, - tsonStringifier, + createTson, + createTsonDeserializer, + createTsonParser, + createTsonSerialize, + createTsonStringify, } from "./tson.js"; export * from "./handlers/index.js"; diff --git a/src/stringify.test.ts b/src/stringify.test.ts index 65311b52..c2aee40b 100644 --- a/src/stringify.test.ts +++ b/src/stringify.test.ts @@ -4,10 +4,10 @@ import { tsonBigint } from "./handlers/tsonBigint.js"; import { tsonMap } from "./handlers/tsonMap.js"; import { tsonSet } from "./handlers/tsonSet.js"; import { tsonUndefined } from "./handlers/tsonUndefined.js"; -import { createTupleson } from "./tson.js"; +import { createTson } from "./tson.js"; test("lets have a look at the stringified output", () => { - const t = createTupleson({ + const t = createTson({ types: [tsonMap, tsonSet, tsonBigint, tsonUndefined], }); diff --git a/src/tson.ts b/src/tson.ts index 421cd69b..fc92778a 100644 --- a/src/tson.ts +++ b/src/tson.ts @@ -28,7 +28,7 @@ type WalkerFactory = (nonce: TsonNonce) => WalkFn; type AnyTsonTransformerSerializeDeserialize = TsonTransformerSerializeDeserialize; -export function tsonDeserializer(opts: TsonOptions): TsonDeserializeFn { +export function createTsonDeserializer(opts: TsonOptions): TsonDeserializeFn { const typeByKey: Record = {}; for (const handler of opts.types) { @@ -61,21 +61,21 @@ export function tsonDeserializer(opts: TsonOptions): TsonDeserializeFn { walker(obj.nonce)(obj.json)) as TsonDeserializeFn; } -export function tsonParser(opts: TsonOptions): TsonParseFn { - const deserializer = tsonDeserializer(opts); +export function createTsonParser(opts: TsonOptions): TsonParseFn { + const deserializer = createTsonDeserializer(opts); return ((str: string) => deserializer(JSON.parse(str) as TsonSerialized)) as TsonParseFn; } -export function tsonStringifier(opts: TsonOptions): TsonStringifyFn { - const serializer = tsonSerializer(opts); +export function createTsonStringify(opts: TsonOptions): TsonStringifyFn { + const serializer = createTsonSerialize(opts); return ((obj: unknown, space?: number | string) => JSON.stringify(serializer(obj), null, space)) as TsonStringifyFn; } -export function tsonSerializer(opts: TsonOptions): TsonSerializeFn { +export function createTsonSerialize(opts: TsonOptions): TsonSerializeFn { const handlers = (() => { const types = opts.types.map((handler) => { type Serializer = ( @@ -186,9 +186,9 @@ function mapOrReturn( return input; } -export const createTupleson = (opts: TsonOptions) => ({ - deserialize: tsonDeserializer(opts), - parse: tsonParser(opts), - serialize: tsonSerializer(opts), - stringify: tsonStringifier(opts), +export const createTson = (opts: TsonOptions) => ({ + deserialize: createTsonDeserializer(opts), + parse: createTsonParser(opts), + serialize: createTsonSerialize(opts), + stringify: createTsonStringify(opts), }); diff --git a/src/types.test.ts b/src/types.test.ts index a8a8d274..818e1dd7 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -1,10 +1,10 @@ import { expectTypeOf, test } from "vitest"; import { tsonBigint } from "./handlers/tsonBigint.js"; -import { createTupleson } from "./tson.js"; +import { createTson } from "./tson.js"; test("types", () => { - const t = createTupleson({ + const t = createTson({ types: [tsonBigint], }); From 3d7f6b40f33b8841b6140be4769583603f176013 Mon Sep 17 00:00:00 2001 From: KATT Date: Sat, 30 Sep 2023 23:06:11 +0200 Subject: [PATCH 2/3] cool --- src/index.ts | 2 +- src/tson.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index a5521cdb..98f84ee1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ export { createTson, - createTsonDeserializer, + createTsonDeserialize, createTsonParser, createTsonSerialize, createTsonStringify, diff --git a/src/tson.ts b/src/tson.ts index fc92778a..8336881d 100644 --- a/src/tson.ts +++ b/src/tson.ts @@ -28,7 +28,7 @@ type WalkerFactory = (nonce: TsonNonce) => WalkFn; type AnyTsonTransformerSerializeDeserialize = TsonTransformerSerializeDeserialize; -export function createTsonDeserializer(opts: TsonOptions): TsonDeserializeFn { +export function createTsonDeserialize(opts: TsonOptions): TsonDeserializeFn { const typeByKey: Record = {}; for (const handler of opts.types) { @@ -62,7 +62,7 @@ export function createTsonDeserializer(opts: TsonOptions): TsonDeserializeFn { } export function createTsonParser(opts: TsonOptions): TsonParseFn { - const deserializer = createTsonDeserializer(opts); + const deserializer = createTsonDeserialize(opts); return ((str: string) => deserializer(JSON.parse(str) as TsonSerialized)) as TsonParseFn; @@ -187,7 +187,7 @@ function mapOrReturn( } export const createTson = (opts: TsonOptions) => ({ - deserialize: createTsonDeserializer(opts), + deserialize: createTsonDeserialize(opts), parse: createTsonParser(opts), serialize: createTsonSerialize(opts), stringify: createTsonStringify(opts), From 464d9dea0b4d54968237dd993e8257c9c535608a Mon Sep 17 00:00:00 2001 From: KATT Date: Sat, 30 Sep 2023 23:07:14 +0200 Subject: [PATCH 3/3] cool --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 558dd0a1..3a7559ee 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ import { tsonUnknownObjectGuard, } from "tupleson"; -const json = createTson({ +const tson = createTson({ // 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", @@ -63,7 +63,7 @@ const json = createTson({ ], }); -const result = json.stringify( +const result = tson.stringify( { foo: "bar", set: new Set([1, 2, 3]),