Skip to content

Commit

Permalink
fix: snakecase conversion for strings with numbers (#8111)
Browse files Browse the repository at this point in the history
  • Loading branch information
julius-welink committed Aug 25, 2021
1 parent 22676a0 commit 749511d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/util/StringUtils.ts
Expand Up @@ -22,7 +22,7 @@ export function snakeCase(str: string): string{
// ABc -> a_bc
.replace(/([A-Z])([A-Z])([a-z])/g, "$1_$2$3")
// aC -> a_c
.replace(/([a-z])([A-Z])/g, "$1_$2")
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
.toLowerCase();
}

Expand Down
8 changes: 8 additions & 0 deletions test/functional/util/StringUtils.ts
Expand Up @@ -48,6 +48,14 @@ describe("StringUtils", () => {
expect(actual).to.be.equal(expected, `Failed for Input: ${input}`);
});

it("should correctly convert strings with numbers", () => {
const input = "device1Status";
const expected = "device1_status";
const actual = snakeCase(input);

expect(actual).to.be.equal(expected, `Failed for Input: ${input}`);
});

it("should match the examples given in the older implementation", () => {
// Pulled from https://regex101.com/r/QeSm2I/1
const examples = {
Expand Down

0 comments on commit 749511d

Please sign in to comment.