Skip to content

Commit f27eb45

Browse files
committed
🐛 with toSlugCase()
1 parent c14fc3e commit f27eb45

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
longestCommonSuffix,
4646
longestUncommonInfixes,
4747
toKebabCase,
48+
toSlugCase,
4849
toSuperscript,
4950
tverskyDistance,
5051
} from "./index.ts";
@@ -1241,6 +1242,20 @@ Deno.test("toKebabCase", () => {
12411242

12421243

12431244

1245+
Deno.test("toSlugCase", () => {
1246+
const a = toSlugCase("Malwa Plateau");
1247+
assertEquals(a, "malwa-plateau");
1248+
const b = toSlugCase("Curaçao São Tomé & Príncipe!");
1249+
assertEquals(b, "curacao-sao-tome-principe");
1250+
const c = toSlugCase("你好世界 hello world");
1251+
assertEquals(c, "hello-world");
1252+
// const d = toSlugCase("Æther & Œuvre — résumé", null, "_");
1253+
// assertEquals(d, "aether_oeuvre_resume");
1254+
});
1255+
1256+
1257+
1258+
12441259
Deno.test("toSuperscript", () => {
12451260
const a = toSuperscript("hello world");
12461261
assertEquals(a, "ʰᵉˡˡᵒ ʷᵒʳˡᵈ");

index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,21 @@ export function toCamelCase(x: string, re: RegExp | null=null, upper: boolean=fa
14451445
export function toPascalCase(x: string, re: RegExp | null=null): string {
14461446
return toCamelCase(x, re, true);
14471447
}
1448+
1449+
1450+
/**
1451+
* Convert a string to slug-case (URL-friendly kebab-case).
1452+
* @param x a string
1453+
* @param re word separator pattern [/[^0-9A-Za-z]+/g]
1454+
* @param sep separator to join with [-]
1455+
* @returns slug-case | slug<join>case
1456+
*/
1457+
export function toSlugCase(x: string, re: RegExp | null = null, sep: string = "-"): string {
1458+
x = x.normalize("NFKD").replace(/[\u0300-\u036f]/g, ""); // Remove accents
1459+
// deno-lint-ignore no-control-regex
1460+
return toKebabCase(x.replace(/[^\x00-\x7F]/g, ""), re, sep); // Remove non-ASCII chars
1461+
}
1462+
export {toSlugCase as slugify};
14481463
//#endregion
14491464

14501465

0 commit comments

Comments
 (0)