Skip to content

Commit

Permalink
fix(text): use locale-independent letter case methods (denoland#6204)
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-rowe authored Nov 28, 2024
1 parent 9904412 commit 9daae05
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
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();
}

0 comments on commit 9daae05

Please sign in to comment.