From 68f49fa4936a5ee165a56a5c599aea3c113def9a Mon Sep 17 00:00:00 2001 From: moontai0724 Date: Mon, 7 Oct 2024 11:33:04 +0800 Subject: [PATCH] feat: string transformer --- src/transformers/string.spec.ts | 79 +++++++++++++++++++++++++++++++++ src/transformers/string.ts | 22 +++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/transformers/string.spec.ts create mode 100644 src/transformers/string.ts diff --git a/src/transformers/string.spec.ts b/src/transformers/string.spec.ts new file mode 100644 index 0000000..bbac65b --- /dev/null +++ b/src/transformers/string.spec.ts @@ -0,0 +1,79 @@ +import { Type } from "@sinclair/typebox"; +import { expect, it } from "vitest"; +import type { Options } from "yargs"; + +import { getStringOption } from "./string"; + +it("should transform TString to yargs option", () => { + const schema = Type.String(); + + const result = getStringOption(schema); + + expect(result).toEqual({ + type: "string", + requiresArg: true, + }); +}); + +it("should transform TString with truthy default value to yargs option", () => { + const schema = Type.String({ default: "foo" }); + + const result = getStringOption(schema); + + expect(result).toEqual({ + type: "string", + requiresArg: false, + default: "foo", + }); +}); + +it("should transform TString with falsy default value to yargs option", () => { + const schema = Type.String({ default: "" }); + + const result = getStringOption(schema); + + expect(result).toEqual({ + type: "string", + requiresArg: false, + default: "", + }); +}); + +it("should transform TString with description to yargs option", () => { + const schema = Type.String({ description: "foo" }); + + const result = getStringOption(schema); + + expect(result).toEqual({ + type: "string", + requiresArg: true, + description: "foo", + }); +}); + +it("should transform TString with override to yargs option", () => { + const schema = Type.String(); + const overwrite: Options = { + requiresArg: false, + alias: "aliased", + }; + + const result = getStringOption(schema, overwrite); + + expect(result).toEqual({ + type: "string", + requiresArg: false, + alias: "aliased", + }); +}); + +it("should detect if it is optional", () => { + const schema = Type.Optional(Type.String()); + + const result = getStringOption(schema); + + expect(result).toEqual({ + type: "string", + requiresArg: false, + }); +}); diff --git a/src/transformers/string.ts b/src/transformers/string.ts new file mode 100644 index 0000000..8af6cab --- /dev/null +++ b/src/transformers/string.ts @@ -0,0 +1,22 @@ +import { type TString, TypeGuard } from "@sinclair/typebox"; +import type { Options } from "yargs"; + +export function getStringOption( + schema: TString, + override: Options = {}, +): Options { + const hasDefaultValue = schema.default !== undefined; + const options = { + type: "string" as const, + requiresArg: !TypeGuard.IsOptional(schema) && !hasDefaultValue, + }; + + if (hasDefaultValue) + Object.assign(options, { default: schema.default as string }); + if (schema.description) + Object.assign(options, { description: schema.description }); + + Object.assign(options, override); + + return options; +}