Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libs/core-functions-lib/__tests__/strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ const dataExtra: Record<string, string> = {
"$Camel##Case#": "$camel##case#",
};

const dataDigits: Record<string, string> = {
// 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);
Expand All @@ -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));
}
});
4 changes: 2 additions & 2 deletions libs/functions/src/lib/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down