Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(text): unicode support and word splitting according to case #5447

Merged
merged 14 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 16 additions & 3 deletions text/_util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

const RE_ACRONYM = /\p{Lu}{2,}/gu; // e.g. ID, URL
kt3k marked this conversation as resolved.
Show resolved Hide resolved
const RE_CAPITALIZED_WORD = /\p{Lu}\p{Ll}+/gu; // e.g. Apple
const RE_LOWERCASED_WORD = /\P{Lu}+/gu; // e.g. apple, this will also match a sequence of unicode letters in languages without a concept of upper/lower case
const RE_DIGITS = /\p{N}+/gu; // e.g. 123
const RE_MATCH_WORD_BY_CASE = new RegExp(
`${RE_ACRONYM.source}|${RE_CAPITALIZED_WORD.source}|${RE_DIGITS.source}|${RE_LOWERCASED_WORD.source}`,
"gu",
);

function splitByCase(input: string): string[] {
const matches = input.match(RE_MATCH_WORD_BY_CASE);

return matches || [input];
}

export function splitToWords(input: string) {
input = input.replaceAll(/[^a-zA-Z0-9\s-_]/g, "");
if (/[\s-_]+/.test(input)) return input.split(/[\s-_]+/);
return input.split(/(?=[A-Z])+/);
return input.split(/[^\p{L}\p{N}]+/gu).filter(Boolean).flatMap(splitByCase);
}

export function capitalizeWord(word: string): string {
Expand Down
45 changes: 45 additions & 0 deletions text/_util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ Deno.test({
},
});

Deno.test({
name: "split() handles a delimiter sequence",
fn() {
const result = splitToWords("I am -> thirsty!");
const expected = ["I", "am", "thirsty"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles upper case delimiter",
fn() {
Expand All @@ -39,6 +48,42 @@ Deno.test({
},
});

Deno.test({
name: "split() handles casing",
fn() {
const result = splitToWords("denoIsAwesome");
const expected = ["deno", "Is", "Awesome"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles unicode",
fn() {
const result = splitToWords("шруберри IsAwesome");
const expected = ["шруберри", "Is", "Awesome"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles unicode casing",
fn() {
const result = splitToWords("шруберриШруберри");
const expected = ["шруберри", "Шруберри"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles languages without casing",
fn() {
const result = splitToWords("אין_על דינו");
const expected = ["אין", "על", "דינו"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles screaming snake case",
fn() {
Expand Down
6 changes: 6 additions & 0 deletions text/case_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ Deno.test("toPascalCase() trims whitespace", () => {
assertEquals(result, expected);
});

Deno.test("toPascalCase() converts a single word with Cyrillic letters", () => {
const input = "шруберри";
const expected = "Шруберри";
assertEquals(toPascalCase(input), expected);
});

Deno.test("toSnakeCase() handles an empty string", () => {
assertEquals(toSnakeCase(""), "");
});
Expand Down