Skip to content

Commit 7a3f123

Browse files
committed
Ref: moving cleaning into makeTransformer() helper.
1 parent 9c56bb6 commit 7a3f123

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

zod-plugin/src/remap.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,20 @@ type Mapper = <T extends Record<string, unknown>>(
2727
subject: T,
2828
) => { [P in string | keyof T]: T[keyof T] };
2929

30-
/** @todo naming */
31-
const ensure = (tool: Record<string, string>) => {
32-
const targets = Object.values(tool);
30+
const makeTransformer = (mapping: Partial<Record<string, string>>) => {
31+
const clean = R.reject(R.isNil, mapping); // rejecting undefined
32+
const targets = Object.values(clean);
3333
if (new Set(targets).size !== targets.length)
34-
throw new Error("remap(): duplicate target keys", { cause: tool });
35-
return tool;
34+
throw new Error("remap(): duplicate target keys", { cause: mapping });
35+
return R.renameKeys(clean);
3636
};
3737

3838
/** Used by runtime (bound) */
3939
export const remap = function (
4040
this: z.ZodObject,
41-
tool: Partial<Record<string, string>> | Mapper,
41+
tool: Parameters<typeof makeTransformer>[0] | Mapper,
4242
) {
43-
const transformer =
44-
typeof tool === "function"
45-
? tool
46-
: R.renameKeys(ensure(R.reject(R.isNil, tool))); // rejecting undefined
43+
const transformer = typeof tool === "function" ? tool : makeTransformer(tool);
4744
const nextShape = transformer(
4845
R.map(R.invoker(0, "clone"), this._zod.def.shape), // immutable, changed from R.clone due to failure
4946
);

0 commit comments

Comments
 (0)