All files / packages/utils/src transformer.ts

84.61% Statements 11/13
50% Branches 2/4
100% Functions 3/3
84.61% Lines 11/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46          332x 332x           332x 1328x 1328x           903382x 26699x   18088x       1355x       16733x             332x              
import { keys } from "remeda";
import { SuperJSON } from "superjson";
 
import type { TemporalMapping } from "~utils/temporal";
 
const superJSONInstance = new SuperJSON({ dedupe: true });
const temporalEntries = {
	plainTime: Temporal.PlainTime,
	plainDate: Temporal.PlainDate,
	plainDateTime: Temporal.PlainDateTime,
	zonedDateTime: Temporal.ZonedDateTime,
} satisfies Record<keyof TemporalMapping, unknown>;
for (const key of keys(temporalEntries)) {
	const Class = temporalEntries[key];
	superJSONInstance.registerCustom<
		TemporalMapping[keyof TemporalMapping],
		string
	>(
		{
			isApplicable: (input): input is TemporalMapping[typeof key] =>
				input instanceof Class,
			serialize: (input) => input.toString(),
			deserialize: (input) => {
				switch (key) {
					case "plainTime":
						return Temporal.PlainTime.from(input);
					case "plainDate":
						return Temporal.PlainDate.from(input);
					case "plainDateTime":
						return Temporal.PlainDateTime.from(input);
					case "zonedDateTime":
						return Temporal.ZonedDateTime.from(input);
				}
			},
		},
		key,
	);
}
export const transformer = {
	serialize: superJSONInstance.serialize.bind(superJSONInstance),
	deserialize: superJSONInstance.deserialize.bind(superJSONInstance),
};
export type TransformerResult = ReturnType<
	(typeof superJSONInstance)["serialize"]
>;