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

Commit

Permalink
chore: no remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Oct 7, 2023
1 parent 5d793be commit c60dc36
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/sync/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import {
type WalkFn = (value: unknown) => unknown;
type WalkerFactory = (nonce: TsonNonce) => WalkFn;

function createTsonSerializeInner(opts: TsonOptions) {
function getHandlers(opts: TsonOptions) {
type Handler = (typeof opts.types)[number];

const byPrimitive: Partial<
Record<TsonAllTypes, Extract<Handler, TsonTypeTesterPrimitive>>
> = {};
const byKey: Extract<Handler, TsonTypeTesterCustom>[] = [];
const nonPrimitives: Extract<Handler, TsonTypeTesterCustom>[] = [];

for (const handler of opts.types) {
if (handler.primitive) {
Expand All @@ -34,11 +34,13 @@ function createTsonSerializeInner(opts: TsonOptions) {

byPrimitive[handler.primitive] = handler;
} else {
byKey.push(handler);
nonPrimitives.push(handler);
}
}

return [byKey, byPrimitive] as const;
const nonceFn: GetNonce = opts.nonce ? (opts.nonce as GetNonce) : getNonce;

return [nonceFn, nonPrimitives, byPrimitive] as const;
}

export function createTsonStringify(opts: TsonOptions): TsonStringifyFn {
Expand All @@ -49,7 +51,7 @@ export function createTsonStringify(opts: TsonOptions): TsonStringifyFn {
}

export function createTsonSerialize(opts: TsonOptions): TsonSerializeFn {
const [nonPrimitive, byPrimitive] = createTsonSerializeInner(opts);
const [getNonce, nonPrimitive, byPrimitive] = getHandlers(opts);

const walker: WalkerFactory = (nonce) => {
const seen = new WeakSet();
Expand Down Expand Up @@ -110,11 +112,8 @@ export function createTsonSerialize(opts: TsonOptions): TsonSerializeFn {
return walk;
};

const nonceFn: GetNonce = opts.nonce ? (opts.nonce as GetNonce) : getNonce;

return ((obj): TsonSerialized => {
const nonce = nonceFn();

const nonce = getNonce();
const json = walker(nonce)(obj);

return {
Expand Down

0 comments on commit c60dc36

Please sign in to comment.