fix: snakecase conversion for strings with numbers (#8111)

This commit is contained in:
julius-welink 2021-08-25 03:01:45 +03:00 committed by GitHub
parent 22676a04c3
commit 749511d981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -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();
}

View File

@ -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 = {