diff --git a/src/util/StringUtils.ts b/src/util/StringUtils.ts index b378d7516b..88daf1ff16 100644 --- a/src/util/StringUtils.ts +++ b/src/util/StringUtils.ts @@ -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(); } diff --git a/test/functional/util/StringUtils.ts b/test/functional/util/StringUtils.ts index 35cb4ca301..2386d96b5e 100644 --- a/test/functional/util/StringUtils.ts +++ b/test/functional/util/StringUtils.ts @@ -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 = {