Skip to content

Commit ee578f4

Browse files
authored
Merge pull request #5530 from NomicFoundation/add-camel-to-kebab
Add camelToKebabCase to hardhat-utils
2 parents 9aac754 + 5fa4bb1 commit ee578f4

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

v-next/core/test/internal/global-options.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ describe("Global Options", () => {
322322
},
323323
]);
324324

325-
setEnvVar("HARDHAT_GLOBAL_OPTION3", "5n");
325+
setEnvVar("HARDHAT_GLOBAL_OPTION_3", "5n");
326326

327327
const globalOptions = resolveGlobalOptions(
328328
{
@@ -420,7 +420,7 @@ describe("Global Options", () => {
420420
},
421421
]);
422422

423-
setEnvVar("HARDHAT_GLOBAL_OPTION1", "not a boolean");
423+
setEnvVar("HARDHAT_GLOBAL_OPTION_1", "not a boolean");
424424

425425
assertThrowsHardhatError(
426426
() => resolveGlobalOptions({}, globalOptionDefinitions),

v-next/hardhat-utils/src/string.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,15 @@ export function kebabToCamelCase(str: string): string {
4848
* @returns The snake_case string.
4949
*/
5050
export function camelToSnakeCase(str: string): string {
51-
return str.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
51+
return str.replace(/[A-Z0-9]/g, (match) => `_${match.toLowerCase()}`);
52+
}
53+
54+
/**
55+
* Converts a camelCase string to kebab-case.
56+
*
57+
* @param str The camelCase string to convert.
58+
* @returns The kebab-case string.
59+
*/
60+
export function camelToKebabCase(str: string): string {
61+
return str.replace(/[A-Z0-9]/g, (match) => `-${match.toLowerCase()}`);
5262
}

v-next/hardhat-utils/test/string.ts

+26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
capitalize,
77
kebabToCamelCase,
88
camelToSnakeCase,
9+
camelToKebabCase,
910
} from "../src/string.js";
1011

1112
describe("string", () => {
@@ -59,5 +60,30 @@ describe("string", () => {
5960
"camel_c_a_s_e_s_t_r_i_n_g",
6061
);
6162
});
63+
assert.equal(camelToSnakeCase("camelcasestring1"), "camelcasestring_1");
64+
assert.equal(
65+
camelToSnakeCase("camel1Case2String"),
66+
"camel_1_case_2_string",
67+
);
68+
assert.equal(camelToSnakeCase("camelC1A2S3E4"), "camel_c_1_a_2_s_3_e_4");
69+
assert.equal(camelToSnakeCase("camel123"), "camel_1_2_3");
70+
});
71+
72+
describe("camelToKebabCase", () => {
73+
it("Should convert a camelCase string to kebab-case", () => {
74+
assert.equal(camelToKebabCase("camelcasestring"), "camelcasestring");
75+
assert.equal(camelToKebabCase("camelCaseString"), "camel-case-string");
76+
assert.equal(
77+
camelToKebabCase("camelCASESTRING"),
78+
"camel-c-a-s-e-s-t-r-i-n-g",
79+
);
80+
assert.equal(camelToKebabCase("camelcasestring1"), "camelcasestring-1");
81+
assert.equal(
82+
camelToKebabCase("camel1Case2String"),
83+
"camel-1-case-2-string",
84+
);
85+
assert.equal(camelToKebabCase("camelC1A2S3E4"), "camel-c-1-a-2-s-3-e-4");
86+
assert.equal(camelToKebabCase("camel123"), "camel-1-2-3");
87+
});
6288
});
6389
});

v-next/hardhat/src/internal/cli/helpers/utils.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { ArgumentType } from "@ignored/hardhat-vnext-core/config";
22
import type { GlobalOptionDefinitions } from "@ignored/hardhat-vnext-core/types/global-options";
33
import type { Task } from "@ignored/hardhat-vnext-core/types/tasks";
44

5+
import { camelToKebabCase } from "@ignored/hardhat-vnext-utils/string";
6+
57
export const GLOBAL_NAME_PADDING = 6;
68

79
interface ArgumentDescriptor {
@@ -15,7 +17,7 @@ export function parseGlobalOptions(
1517
globalOptionDefinitions: GlobalOptionDefinitions,
1618
): ArgumentDescriptor[] {
1719
return [...globalOptionDefinitions].map(([, { option }]) => ({
18-
name: formatOptionName(option.name),
20+
name: toCommandLineOption(option.name),
1921
description: option.description,
2022
}));
2123
}
@@ -62,7 +64,7 @@ export function parseOptions(task: Task): {
6264

6365
for (const [optionName, option] of task.options) {
6466
options.push({
65-
name: formatOptionName(optionName),
67+
name: toCommandLineOption(optionName),
6668
description: option.description,
6769
type: option.type,
6870
});
@@ -79,15 +81,8 @@ export function parseOptions(task: Task): {
7981
return { options, positionalArguments };
8082
}
8183

82-
export function formatOptionName(str: string): string {
83-
return `--${str
84-
.split("")
85-
.map((letter, idx) => {
86-
return letter.toUpperCase() === letter
87-
? `${idx !== 0 ? "-" : ""}${letter.toLowerCase()}`
88-
: letter;
89-
})
90-
.join("")}`;
84+
export function toCommandLineOption(optionName: string): string {
85+
return `--${camelToKebabCase(optionName)}`;
9186
}
9287

9388
export function getLongestNameLength(tasks: Array<{ name: string }>): number {

v-next/hardhat/test/internal/cli/helpers/utils.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
parseTasks,
1010
parseSubtasks,
1111
parseOptions,
12-
formatOptionName,
12+
toCommandLineOption,
1313
getLongestNameLength,
1414
getSection,
1515
getUsageString,
@@ -179,10 +179,11 @@ describe("utils", function () {
179179
});
180180
});
181181

182-
describe("formatOptionName", function () {
183-
it("should format option names", function () {
184-
assert.equal(formatOptionName("option"), "--option");
185-
assert.equal(formatOptionName("anotherOption"), "--another-option");
182+
describe("toCommandLineOption", function () {
183+
it("should convert a camelCase option name to a kebab-case command line option", function () {
184+
assert.equal(toCommandLineOption("option"), "--option");
185+
assert.equal(toCommandLineOption("anotherOption"), "--another-option");
186+
assert.equal(toCommandLineOption("anotherOption1"), "--another-option-1");
186187
});
187188
});
188189

0 commit comments

Comments
 (0)