generated from moontaiworks/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handy transformer for an object of properties
- Loading branch information
1 parent
6d1b0ee
commit 710981c
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Type } from "@sinclair/typebox"; | ||
import { beforeAll, beforeEach, expect, it, vi } from "vitest"; | ||
|
||
const getOption = vi.fn().mockReturnValue({ type: "mocked" }); | ||
|
||
let component: typeof import("./get-options"); | ||
|
||
beforeAll(async () => { | ||
vi.doMock("./get-option", () => ({ getOption })); | ||
component = await import("./get-options"); | ||
}); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("should pass schemas of props in a TObject to get-option", () => { | ||
const schema = Type.Object({ | ||
foo: Type.String(), | ||
}); | ||
const result = component.getOptions(schema); | ||
expect(getOption).toBeCalledWith(schema.properties.foo); | ||
expect(result).toEqual({ foo: { type: "mocked" } }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Static, TObject } from "@sinclair/typebox"; | ||
import type { Options } from "yargs"; | ||
|
||
import { getOption } from "./get-option"; | ||
|
||
export function getOptions<Schema extends TObject>( | ||
schema: Schema, | ||
): Record<keyof Static<Schema>, Options> { | ||
const options = Object.entries(schema.properties).reduce( | ||
(acc, [key, value]) => { | ||
const option = getOption(value); | ||
const result = { | ||
[key]: option, | ||
}; | ||
|
||
Object.assign(acc, result); | ||
|
||
return acc; | ||
}, | ||
{} as Record<keyof Static<Schema>, Options>, | ||
); | ||
|
||
return options; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Type } from "@sinclair/typebox"; | ||
import { beforeAll, beforeEach, expect, it, vi } from "vitest"; | ||
|
||
let component: typeof import("@/index"); | ||
|
||
beforeAll(async () => { | ||
component = await import("@/index"); | ||
}); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("should transform schemas of props in a TObject to yargs options", () => { | ||
const schema = Type.Object({ | ||
page: Type.Number({ description: "page number" }), | ||
size: Type.Number({ description: "page size", default: 10 }), | ||
query: Type.Optional(Type.String()), | ||
sort: Type.Optional( | ||
Type.Array(Type.Union([Type.Literal("id"), Type.Literal("createdAt")])), | ||
), | ||
order: Type.Union([Type.Literal("asc"), Type.Literal("desc")], { | ||
default: "asc", | ||
}), | ||
pretty: Type.Boolean({ description: "pretty print" }), | ||
}); | ||
const result = component.getOptions(schema); | ||
expect(result).toEqual({ | ||
page: { type: "number", requiresArg: true, description: "page number" }, | ||
size: { | ||
type: "number", | ||
requiresArg: false, | ||
default: 10, | ||
description: "page size", | ||
}, | ||
query: { type: "string", requiresArg: false }, | ||
sort: { type: "array", requiresArg: false, choices: ["id", "createdAt"] }, | ||
order: { | ||
type: "string", | ||
requiresArg: false, | ||
choices: ["asc", "desc"], | ||
default: "asc", | ||
}, | ||
pretty: { type: "boolean", requiresArg: true, description: "pretty print" }, | ||
}); | ||
}); |