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): use locale-independent letter case methods (#6016) #6204

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions text/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ export function splitToWords(input: string) {
}

export function capitalizeWord(word: string): string {
return word
? word?.[0]?.toLocaleUpperCase() + word.slice(1).toLocaleLowerCase()
: word;
return word ? word[0]!.toUpperCase() + word.slice(1).toLowerCase() : word;
}
44 changes: 43 additions & 1 deletion text/_util_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "@std/assert";
import { splitToWords } from "./_util.ts";
import { capitalizeWord, splitToWords } from "./_util.ts";

Deno.test({
name: "split() returns an empty array for an empty string",
Expand Down Expand Up @@ -155,3 +155,45 @@ Deno.test({
assertEquals(result, expected);
},
});

Deno.test({
name: "capitalizeWord() handles empty string",
fn() {
assertEquals(capitalizeWord(""), "");
},
});

Deno.test({
name: "capitalizeWord() handles single char",
fn() {
assertEquals(capitalizeWord("a"), "A");
},
});

Deno.test({
name: "capitalizeWord() handles multiple chars",
fn() {
assertEquals(capitalizeWord("aa"), "Aa");
},
});

Deno.test({
name: "capitalizeWord() handles non-Latin text with letter case",
fn() {
assertEquals(capitalizeWord("γράφω"), "Γράφω");
},
});

Deno.test({
name: "capitalizeWord() handles non-Latin text without letter case (no-op)",
fn() {
assertEquals(capitalizeWord("文字"), "文字");
},
});

Deno.test({
name: "capitalizeWord() handles non-BMP text (no-op)",
fn() {
assertEquals(capitalizeWord("💩"), "💩");
},
});
2 changes: 1 addition & 1 deletion text/to_camel_case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import { capitalizeWord, splitToWords } from "./_util.ts";
export function toCamelCase(input: string): string {
input = input.trim();
const [first = "", ...rest] = splitToWords(input);
return [first.toLocaleLowerCase(), ...rest.map(capitalizeWord)].join("");
return [first.toLowerCase(), ...rest.map(capitalizeWord)].join("");
}
2 changes: 1 addition & 1 deletion text/to_kebab_case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import { splitToWords } from "./_util.ts";
*/
export function toKebabCase(input: string): string {
input = input.trim();
return splitToWords(input).join("-").toLocaleLowerCase();
return splitToWords(input).join("-").toLowerCase();
}
2 changes: 1 addition & 1 deletion text/to_snake_case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import { splitToWords } from "./_util.ts";
*/
export function toSnakeCase(input: string): string {
input = input.trim();
return splitToWords(input).join("_").toLocaleLowerCase();
return splitToWords(input).join("_").toLowerCase();
}
2 changes: 1 addition & 1 deletion text/unstable_to_constant_case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import { splitToWords } from "./_util.ts";
*/
export function toConstantCase(input: string): string {
input = input.trim();
return splitToWords(input).join("_").toLocaleUpperCase();
return splitToWords(input).join("_").toUpperCase();
}