Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add camelToKebabCase to hardhat-utils #5530

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions v-next/core/test/internal/global-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ describe("Global Options", () => {
},
]);

setEnvVar("HARDHAT_GLOBAL_OPTION3", "5n");
setEnvVar("HARDHAT_GLOBAL_OPTION_3", "5n");

const globalOptions = resolveGlobalOptions(
{
Expand Down Expand Up @@ -420,7 +420,7 @@ describe("Global Options", () => {
},
]);

setEnvVar("HARDHAT_GLOBAL_OPTION1", "not a boolean");
setEnvVar("HARDHAT_GLOBAL_OPTION_1", "not a boolean");

assertThrowsHardhatError(
() => resolveGlobalOptions({}, globalOptionDefinitions),
Expand Down
12 changes: 11 additions & 1 deletion v-next/hardhat-utils/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,15 @@ export function kebabToCamelCase(str: string): string {
* @returns The snake_case string.
*/
export function camelToSnakeCase(str: string): string {
return str.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
return str.replace(/[A-Z0-9]/g, (match) => `_${match.toLowerCase()}`);
}

/**
* Converts a camelCase string to kebab-case.
*
* @param str The camelCase string to convert.
* @returns The kebab-case string.
*/
export function camelToKebabCase(str: string): string {
return str.replace(/[A-Z0-9]/g, (match) => `-${match.toLowerCase()}`);
}
26 changes: 26 additions & 0 deletions v-next/hardhat-utils/test/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
capitalize,
kebabToCamelCase,
camelToSnakeCase,
camelToKebabCase,
} from "../src/string.js";

describe("string", () => {
Expand Down Expand Up @@ -59,5 +60,30 @@ describe("string", () => {
"camel_c_a_s_e_s_t_r_i_n_g",
);
});
assert.equal(camelToSnakeCase("camelcasestring1"), "camelcasestring_1");
assert.equal(
camelToSnakeCase("camel1Case2String"),
"camel_1_case_2_string",
);
assert.equal(camelToSnakeCase("camelC1A2S3E4"), "camel_c_1_a_2_s_3_e_4");
assert.equal(camelToSnakeCase("camel123"), "camel_1_2_3");
});

describe("camelToKebabCase", () => {
it("Should convert a camelCase string to kebab-case", () => {
assert.equal(camelToKebabCase("camelcasestring"), "camelcasestring");
assert.equal(camelToKebabCase("camelCaseString"), "camel-case-string");
assert.equal(
camelToKebabCase("camelCASESTRING"),
"camel-c-a-s-e-s-t-r-i-n-g",
);
assert.equal(camelToKebabCase("camelcasestring1"), "camelcasestring-1");
assert.equal(
camelToKebabCase("camel1Case2String"),
"camel-1-case-2-string",
);
assert.equal(camelToKebabCase("camelC1A2S3E4"), "camel-c-1-a-2-s-3-e-4");
assert.equal(camelToKebabCase("camel123"), "camel-1-2-3");
});
});
});
17 changes: 6 additions & 11 deletions v-next/hardhat/src/internal/cli/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { ArgumentType } from "@ignored/hardhat-vnext-core/config";
import type { GlobalOptionDefinitions } from "@ignored/hardhat-vnext-core/types/global-options";
import type { Task } from "@ignored/hardhat-vnext-core/types/tasks";

import { camelToKebabCase } from "@ignored/hardhat-vnext-utils/string";

export const GLOBAL_NAME_PADDING = 6;

interface ArgumentDescriptor {
Expand All @@ -15,7 +17,7 @@ export function parseGlobalOptions(
globalOptionDefinitions: GlobalOptionDefinitions,
): ArgumentDescriptor[] {
return [...globalOptionDefinitions].map(([, { option }]) => ({
name: formatOptionName(option.name),
name: toCommandLineOption(option.name),
description: option.description,
}));
}
Expand Down Expand Up @@ -62,7 +64,7 @@ export function parseOptions(task: Task): {

for (const [optionName, option] of task.options) {
options.push({
name: formatOptionName(optionName),
name: toCommandLineOption(optionName),
description: option.description,
type: option.type,
});
Expand All @@ -79,15 +81,8 @@ export function parseOptions(task: Task): {
return { options, positionalArguments };
}

export function formatOptionName(str: string): string {
return `--${str
.split("")
.map((letter, idx) => {
return letter.toUpperCase() === letter
? `${idx !== 0 ? "-" : ""}${letter.toLowerCase()}`
: letter;
})
.join("")}`;
export function toCommandLineOption(optionName: string): string {
return `--${camelToKebabCase(optionName)}`;
}

export function getLongestNameLength(tasks: Array<{ name: string }>): number {
Expand Down
11 changes: 6 additions & 5 deletions v-next/hardhat/test/internal/cli/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
parseTasks,
parseSubtasks,
parseOptions,
formatOptionName,
toCommandLineOption,
getLongestNameLength,
getSection,
getUsageString,
Expand Down Expand Up @@ -179,10 +179,11 @@ describe("utils", function () {
});
});

describe("formatOptionName", function () {
it("should format option names", function () {
assert.equal(formatOptionName("option"), "--option");
assert.equal(formatOptionName("anotherOption"), "--another-option");
describe("toCommandLineOption", function () {
it("should convert a camelCase option name to a kebab-case command line option", function () {
assert.equal(toCommandLineOption("option"), "--option");
assert.equal(toCommandLineOption("anotherOption"), "--another-option");
assert.equal(toCommandLineOption("anotherOption1"), "--another-option-1");
});
});

Expand Down