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

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Sep 30, 2023
1 parent f5107a9 commit 8147119
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/tson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,45 +98,44 @@ export function tsonSerializer(opts: TsonOptions): TsonSerializeFn {
});
type Handler = (typeof types)[number];

const handlerPerPrimitive: Partial<
const byPrimitive: Partial<
Record<TsonAllTypes, Extract<Handler, TsonTypeTesterPrimitive>>
> = {};
const customTypeHandlers: Extract<Handler, TsonTypeTesterCustom>[] = [];
const nonPrimitive: Extract<Handler, TsonTypeTesterCustom>[] = [];

for (const handler of types) {
if (handler.primitive) {
if (handlerPerPrimitive[handler.primitive]) {
if (byPrimitive[handler.primitive]) {
throw new Error(
`Multiple handlers for primitive ${handler.primitive} found`,
);
}

handlerPerPrimitive[handler.primitive] = handler;
byPrimitive[handler.primitive] = handler;
} else {
customTypeHandlers.push(handler);
nonPrimitive.push(handler);
}
}

return {
custom: customTypeHandlers,
primitive: handlerPerPrimitive,
};
return [nonPrimitive, byPrimitive] as const;
})();
const maybeNonce = opts.nonce;

const [nonPrimitive, byPrimitive] = handlers;

const walker: WalkerFactory = (nonce) => {
const walk: WalkFn = (value) => {
const type = typeof value;

const primitiveHandler = handlers.primitive[type];
const primitiveHandler = byPrimitive[type];
if (
primitiveHandler &&
(!primitiveHandler.test || primitiveHandler.test(value))
) {
return primitiveHandler.$serialize(value, nonce, walk);
}

for (const handler of handlers.custom) {
for (const handler of nonPrimitive) {
if (handler.test(value)) {
return handler.$serialize(value, nonce, walk);
}
Expand Down

0 comments on commit 8147119

Please sign in to comment.