Skip to content

Commit 850e9e0

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

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

index.test.ts

Lines changed: 27 additions & 8 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";
@@ -1228,14 +1229,32 @@ Deno.test("longestUncommonInfixes", () => {
12281229
Deno.test("toKebabCase", () => {
12291230
const a = toKebabCase("Malwa Plateau");
12301231
assertEquals(a, "malwa-plateau");
1231-
const b = toKebabCase("::chota::nagpur::", null, "_");
1232-
assertEquals(b, "chota_nagpur");
1233-
const c = toKebabCase("deccan___plateau", /_+/g, ".");
1234-
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");
1232+
const b = toKebabCase("malwaPlateau");
1233+
assertEquals(b, "malwa-plateau");
1234+
const c = toKebabCase("::chota::nagpur::", null, "_");
1235+
assertEquals(c, "chota_nagpur");
1236+
const d = toKebabCase("deccan___plateau", /_+/g, ".");
1237+
assertEquals(d, "deccan.plateau");
1238+
const e = toKebabCase("Some text_with-mixed CASE");
1239+
assertEquals(e, "some-text-with-mixed-case");
1240+
const f = toKebabCase("someTextWithMixedCase");
1241+
assertEquals(f, "some-text-with-mixed-case");
1242+
const g = toKebabCase("IAmListeningToFMWhileLoadingDifferentURL");
1243+
assertEquals(g, "i-am-listening-to-fm-while-loading-different-url");
1244+
});
1245+
1246+
1247+
1248+
1249+
Deno.test("toSlugCase", () => {
1250+
const a = toSlugCase("Malwa Plateau");
1251+
assertEquals(a, "malwa-plateau");
1252+
const b = toSlugCase("Curaçao São Tomé & Príncipe!");
1253+
assertEquals(b, "curacao-sao-tome-principe");
1254+
const c = toSlugCase("你好世界 hello world");
1255+
assertEquals(c, "hello-world");
1256+
// const d = toSlugCase("Æther & Œuvre — résumé", null, "_");
1257+
// assertEquals(d, "aether_oeuvre_resume");
12391258
});
12401259

12411260

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)