diff --git a/text/_util.ts b/text/_util.ts index 5ca9572c956d..5b85123a075e 100644 --- a/text/_util.ts +++ b/text/_util.ts @@ -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; } diff --git a/text/_util_test.ts b/text/_util_test.ts index 155174d0fcef..42223ca5aef7 100644 --- a/text/_util_test.ts +++ b/text/_util_test.ts @@ -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", @@ -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("💩"), "💩"); + }, +}); diff --git a/text/to_camel_case.ts b/text/to_camel_case.ts index 8e8c8ce44530..4da608528245 100644 --- a/text/to_camel_case.ts +++ b/text/to_camel_case.ts @@ -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(""); } diff --git a/text/to_kebab_case.ts b/text/to_kebab_case.ts index a749d2da9c44..446e19fbdc5d 100644 --- a/text/to_kebab_case.ts +++ b/text/to_kebab_case.ts @@ -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(); } diff --git a/text/to_snake_case.ts b/text/to_snake_case.ts index 609fdddd2f2a..4b42b6d3d593 100644 --- a/text/to_snake_case.ts +++ b/text/to_snake_case.ts @@ -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(); } diff --git a/text/unstable_to_constant_case.ts b/text/unstable_to_constant_case.ts index 9e585921a346..a9760116e759 100644 --- a/text/unstable_to_constant_case.ts +++ b/text/unstable_to_constant_case.ts @@ -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(); }