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

Commit

Permalink
feat: createTypeson -> createTson (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Sep 30, 2023
1 parent ae83273 commit 3613716
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 46 deletions.
7 changes: 2 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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))
- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3))
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,7 +53,7 @@ import {
tsonUnknownObjectGuard,
} from "tupleson";

const json = createTupleson({
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",
Expand All @@ -62,7 +63,7 @@ const json = createTupleson({
],
});

const result = json.stringify(
const result = tson.stringify(
{
foo: "bar",
set: new Set([1, 2, 3]),
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/tsonBigint.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/tsonDate.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/tsonMap.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/tsonNumberGuard.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/tsonRegExp.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/tsonSet.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/tsonURL.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/tsonUndefined.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/tsonUnknownObjectGuard.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export {
createTupleson,
tsonDeserializer,
tsonParser,
tsonSerializer,
tsonStringifier,
createTson,
createTsonDeserialize,
createTsonParser,
createTsonSerialize,
createTsonStringify,
} from "./tson.js";

export * from "./handlers/index.js";
4 changes: 2 additions & 2 deletions src/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
});

Expand Down
22 changes: 11 additions & 11 deletions src/tson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type WalkerFactory = (nonce: TsonNonce) => WalkFn;
type AnyTsonTransformerSerializeDeserialize =
TsonTransformerSerializeDeserialize<any, any>;

export function tsonDeserializer(opts: TsonOptions): TsonDeserializeFn {
export function createTsonDeserialize(opts: TsonOptions): TsonDeserializeFn {
const typeByKey: Record<string, AnyTsonTransformerSerializeDeserialize> = {};

for (const handler of opts.types) {
Expand Down Expand Up @@ -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 = createTsonDeserialize(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 = (
Expand Down Expand Up @@ -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: createTsonDeserialize(opts),
parse: createTsonParser(opts),
serialize: createTsonSerialize(opts),
stringify: createTsonStringify(opts),
});
4 changes: 2 additions & 2 deletions src/types.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});

Expand Down

0 comments on commit 3613716

Please sign in to comment.