Skip to content

Commit c14fc3e

Browse files
committed
🐛 fix kebab case to handle upper case chars till the end
1 parent 23a8b9f commit c14fc3e

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

index.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,10 @@ Deno.test("toKebabCase", () => {
12321232
assertEquals(b, "chota_nagpur");
12331233
const c = toKebabCase("deccan___plateau", /_+/g, ".");
12341234
assertEquals(c, "deccan.plateau");
1235+
const d = toKebabCase("Some text_with-mixed CASE");
1236+
assertEquals(d, "some-text-with-mixed-case");
1237+
const e = toKebabCase("IAmListeningToFMWhileLoadingDifferentURL");
1238+
assertEquals(e, "i-am-listening-to-fm-while-loading-different-url");
12351239
});
12361240

12371241

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ function _toTitleCase(x: string, re: RegExp | null=null): string {
13811381
export function toKebabCase(x: string, re: RegExp | null=null, sep: string="-"): string {
13821382
const words = x.split(re || /[^0-9A-Za-z]+/g).filter(IDENTITY);
13831383
for (let i=0, I=words.length; i<I; ++i) {
1384-
words[i] = words[i].replace(/[A-Z]+/g, m => m.length===1? sep + m : sep + m.slice(0, -1) + sep + m.slice(-1));
1384+
words[i] = words[i].replace(/[A-Z]+/g, (m, off) => m.length===1 || off + m.length===words[i].length? sep + m : sep + m.slice(0, -1) + sep + m.slice(-1));
13851385
if (words[i].startsWith(sep)) words[i] = words[i].slice(sep.length);
13861386
}
13871387
return words.join(sep).toLowerCase();

0 commit comments

Comments
 (0)