Skip to content

Commit

Permalink
fix: trim leading spaces in a command name
Browse files Browse the repository at this point in the history
  • Loading branch information
nktknshn committed Jun 13, 2023
1 parent 8d97493 commit e656088
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/command/commands/basic/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { GetCommandNameFromDesc } from "./type-command-name";
// dprint-ignore
export const command = <const TCommandDesc extends readonly string[] | string, TArgv extends EmptyRecord>(
// XXX the line above requires typescript ^5.0
commandDesc: TCommandDesc,
commandDesc: TCommandDesc,
description: string,
// XXX try to avoid using `as` here
builder: YargsCommandBuilder<TArgv> = (yargs) => yargs as y.Argv<TArgv>
Expand Down
1 change: 1 addition & 0 deletions src/command/commands/basic/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @returns the command name
*/
export const getCommandNameFromDesc = (desc: string): string => {
desc = desc.trimStart();
return desc.includes(" ")
? desc.split(" ")[0]
: desc;
Expand Down
6 changes: 5 additions & 1 deletion src/command/commands/basic/type-command-name.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
type StripSpaces<T extends string> = T extends ` ${infer U}` ? StripSpaces<U>
: T;

/**
* @description
*/
export type GetCommandNameString<TCommandDesc extends string> =
TCommandDesc extends `${infer TCommandName} ${string}` ? TCommandName
StripSpaces<TCommandDesc> extends `${infer TCommandName} ${string}`
? TCommandName
: TCommandDesc;

/**
Expand Down
18 changes: 18 additions & 0 deletions test/builder/test-spaces-in-command-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { buildAndParseUnsafeR, comm, subs } from "../../src";

describe("builder trims leading spaces", () => {
test("basic command", () => {
const cmd1 = comm(" a ", "cmd", _ => _);
expectTypeOf(cmd1.commandName).toEqualTypeOf<"a">();
expect(cmd1.commandName).toBe("a");
});

test("subs command", () => {
const cmd1 = subs(" a ", "cmd", [
comm("b", "cmd", _ => _),
]);

expectTypeOf(cmd1.command.commandName).toEqualTypeOf<"a">();
expect(cmd1.command.commandName).toBe("a");
});
});

0 comments on commit e656088

Please sign in to comment.