diff --git a/libs/core-functions-lib/__tests__/strings.test.ts b/libs/core-functions-lib/__tests__/strings.test.ts index 0513c4ba0..6ee5e3361 100644 --- a/libs/core-functions-lib/__tests__/strings.test.ts +++ b/libs/core-functions-lib/__tests__/strings.test.ts @@ -59,6 +59,13 @@ const dataExtra: Record = { "$Camel##Case#": "$camel##case#", }; +const dataDigits: Record = { + // underscore must be inserted between a digit and a following capital letter + field1Name: "field1_name", + address2Line: "address2_line", + plan9FromOuterSpace: "plan9_from_outer_space", +}; + test("test idToSnakeCaseFast", async () => { for (const [value, expected] of Object.entries(data)) { const res = idToSnakeCaseFast(value); @@ -79,3 +86,16 @@ test("test idToSnakeCaseRegex", async () => { expect(res).toEqual(expected); } }); + +test("test idToSnakeCaseFast with digits", async () => { + for (const [value, expected] of Object.entries(dataDigits)) { + const res = idToSnakeCaseFast(value); + expect(res).toEqual(expected); + } +}); + +test("test idToSnakeCaseFast matches idToSnakeCaseRegex on digit boundaries", async () => { + for (const value of Object.keys(dataDigits)) { + expect(idToSnakeCaseFast(value)).toEqual(idToSnakeCaseRegex(value)); + } +}); diff --git a/libs/functions/src/lib/strings.ts b/libs/functions/src/lib/strings.ts index bd41ef283..0f5325d05 100644 --- a/libs/functions/src/lib/strings.ts +++ b/libs/functions/src/lib/strings.ts @@ -22,8 +22,8 @@ export function idToSnakeCaseFast(id: string) { concatIndex = i + 1; } // needUnderscore is used in case next char is a capital latin letter - // we add underscore only between latin letters - needUnderscore = (c >= aCode && c <= zCode) || (c >= ACode && c <= ZCode); + // we add underscore after a latin letter or a digit + needUnderscore = (c >= aCode && c <= zCode) || (c >= ACode && c <= ZCode) || (c >= zeroCode && c <= nineCode); } if (concatIndex == 0) { return id;