Skip to content

Commit 144bb56

Browse files
committed
Handle edge case in toKebabCase.
1 parent e6990b1 commit 144bb56

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

django_unicorn/static/js/utils.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,15 @@ export function toKebabCase(str) {
9090
return "";
9191
}
9292

93-
return str
94-
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
95-
.map((x) => x.toLowerCase())
96-
.join("-");
93+
const match = str.match(
94+
/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g
95+
);
96+
97+
if (!match) {
98+
return str;
99+
}
100+
101+
return match.map((x) => x.toLowerCase()).join("-");
97102
}
98103

99104
/**

tests/js/utils/toKebabCase.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ test("Null to empty string", (t) => {
1616
test("Empty string to empty string", (t) => {
1717
t.is(toKebabCase(""), "");
1818
});
19+
20+
test("One space string to empty string", (t) => {
21+
t.is(toKebabCase(" "), " ");
22+
});

0 commit comments

Comments
 (0)