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 interface
getOption
to transform all accepted types
- Loading branch information
1 parent
2da08a9
commit 432082d
Showing
5 changed files
with
237 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from "@/calculator"; | ||
export * from "./transformers"; |
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,105 @@ | ||
import { Type } from "@sinclair/typebox"; | ||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; | ||
import type { Options } from "yargs"; | ||
|
||
const getArrayOption = vi.fn().mockReturnValue({ type: "mocked" }); | ||
const getBooleanOption = vi.fn().mockReturnValue({ type: "mocked" }); | ||
const getNumberOption = vi.fn().mockReturnValue({ type: "mocked" }); | ||
const getStringOption = vi.fn().mockReturnValue({ type: "mocked" }); | ||
|
||
let component: typeof import("./get-option"); | ||
|
||
beforeAll(async () => { | ||
vi.doMock("./array", () => ({ getArrayOption })); | ||
vi.doMock("./boolean", () => ({ getBooleanOption })); | ||
vi.doMock("./number", () => ({ getNumberOption })); | ||
vi.doMock("./string", () => ({ getStringOption })); | ||
component = await import("./get-option"); | ||
}); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("array", () => { | ||
it("should pass schema to array transformer", () => { | ||
const schema = Type.Array(Type.Unknown()); | ||
const result = component.getOption(schema); | ||
expect(getArrayOption).toBeCalledWith(schema, {}); | ||
expect(result).toEqual({ type: "mocked" }); | ||
}); | ||
|
||
it("should pass override to array transformer", () => { | ||
const schema = Type.Array(Type.Unknown()); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(getArrayOption).toBeCalledWith(schema, override); | ||
expect(result).toEqual({ type: "mocked", alias: "aliased" }); | ||
}); | ||
}); | ||
|
||
describe("boolean", () => { | ||
it("should pass schema to boolean transformer", () => { | ||
const schema = Type.Boolean(); | ||
const result = component.getOption(schema); | ||
expect(getBooleanOption).toBeCalledWith(schema, {}); | ||
expect(result).toEqual({ type: "mocked" }); | ||
}); | ||
|
||
it("should pass override to boolean transformer", () => { | ||
const schema = Type.Boolean(); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(getBooleanOption).toBeCalledWith(schema, override); | ||
expect(result).toEqual({ type: "mocked", alias: "aliased" }); | ||
}); | ||
}); | ||
|
||
describe("number", () => { | ||
it("should pass schema to number transformer", () => { | ||
const schema = Type.Number(); | ||
const result = component.getOption(schema); | ||
expect(getNumberOption).toBeCalledWith(schema, {}); | ||
expect(result).toEqual({ type: "mocked" }); | ||
}); | ||
|
||
it("should pass override to number transformer", () => { | ||
const schema = Type.Number(); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(getNumberOption).toBeCalledWith(schema, override); | ||
expect(result).toEqual({ type: "mocked", alias: "aliased" }); | ||
}); | ||
}); | ||
|
||
describe("string", () => { | ||
it("should pass schema to string transformer", () => { | ||
const schema = Type.String(); | ||
const result = component.getOption(schema); | ||
expect(getStringOption).toBeCalledWith(schema, {}); | ||
expect(result).toEqual({ type: "mocked" }); | ||
}); | ||
|
||
it("should pass override to string transformer", () => { | ||
const schema = Type.String(); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(getStringOption).toBeCalledWith(schema, override); | ||
expect(result).toEqual({ type: "mocked", alias: "aliased" }); | ||
}); | ||
}); | ||
|
||
describe("unaccepted type", () => { | ||
it("should return empty object when type is not supported", () => { | ||
const schema = Type.BigInt(); | ||
const result = component.getOption(schema); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it("should still be able to override", () => { | ||
const schema = Type.BigInt(); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(result).toEqual({ alias: "aliased" }); | ||
}); | ||
}); |
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,22 @@ | ||
import { type TSchema, TypeGuard } from "@sinclair/typebox"; | ||
import type { Options } from "yargs"; | ||
|
||
import { getArrayOption } from "./array"; | ||
import { getBooleanOption } from "./boolean"; | ||
import { getNumberOption } from "./number"; | ||
import { getStringOption } from "./string"; | ||
|
||
export function getOption(schema: TSchema, override: Options = {}) { | ||
const option: Options = (() => { | ||
if (TypeGuard.IsNumber(schema)) return getNumberOption(schema, override); | ||
if (TypeGuard.IsString(schema)) return getStringOption(schema, override); | ||
if (TypeGuard.IsBoolean(schema)) return getBooleanOption(schema, override); | ||
if (TypeGuard.IsArray(schema)) return getArrayOption(schema, override); | ||
|
||
return {}; | ||
})(); | ||
|
||
Object.assign(option, override); | ||
|
||
return option; | ||
} |
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,5 @@ | ||
export * from "./array"; | ||
export * from "./boolean"; | ||
export * from "./get-option"; | ||
export * from "./number"; | ||
export * from "./string"; |
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,104 @@ | ||
import { Type } from "@sinclair/typebox"; | ||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; | ||
import type { Options } from "yargs"; | ||
|
||
let component: typeof import("@/index"); | ||
|
||
beforeAll(async () => { | ||
component = await import("@/index"); | ||
}); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("array", () => { | ||
it("should pass schema to array transformer", () => { | ||
const schema = Type.Array(Type.String()); | ||
const result = component.getOption(schema); | ||
expect(result).toEqual({ type: "array", requiresArg: true }); | ||
}); | ||
|
||
it("should pass override to array transformer", () => { | ||
const schema = Type.Array(Type.String()); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(result).toEqual({ | ||
type: "array", | ||
requiresArg: true, | ||
alias: "aliased", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("boolean", () => { | ||
it("should pass schema to boolean transformer", () => { | ||
const schema = Type.Boolean(); | ||
const result = component.getOption(schema); | ||
expect(result).toEqual({ type: "boolean", requiresArg: true }); | ||
}); | ||
|
||
it("should pass override to boolean transformer", () => { | ||
const schema = Type.Boolean(); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(result).toEqual({ | ||
type: "boolean", | ||
requiresArg: true, | ||
alias: "aliased", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("number", () => { | ||
it("should pass schema to number transformer", () => { | ||
const schema = Type.Number(); | ||
const result = component.getOption(schema); | ||
expect(result).toEqual({ type: "number", requiresArg: true }); | ||
}); | ||
|
||
it("should pass override to number transformer", () => { | ||
const schema = Type.Number(); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(result).toEqual({ | ||
type: "number", | ||
requiresArg: true, | ||
alias: "aliased", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("string", () => { | ||
it("should pass schema to string transformer", () => { | ||
const schema = Type.String(); | ||
const result = component.getOption(schema); | ||
expect(result).toEqual({ type: "string", requiresArg: true }); | ||
}); | ||
|
||
it("should pass override to string transformer", () => { | ||
const schema = Type.String(); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: true, | ||
alias: "aliased", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("unaccepted type", () => { | ||
it("should return empty object when type is not supported", () => { | ||
const schema = Type.BigInt(); | ||
const result = component.getOption(schema); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it("should still be able to override", () => { | ||
const schema = Type.BigInt(); | ||
const override: Options = { alias: "aliased" }; | ||
const result = component.getOption(schema, override); | ||
expect(result).toEqual({ alias: "aliased" }); | ||
}); | ||
}); |