Skip to content

Commit 36de7ef

Browse files
committed
feat: i18n support
1 parent 45f9ca4 commit 36de7ef

3 files changed

Lines changed: 30 additions & 26 deletions

File tree

src/case.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { getWords } from './.internal/getWords'
88
* capitalizeInitial('hello'); // 'Hello'
99
* capitalizeInitial(':> hello'); // ':> Hello'
1010
*/
11-
function capitalizeInitial(str: string): string {
12-
return str.replace(/\b\w/, (char: string) => char.toUpperCase())
11+
function capitalizeInitial(str: string, locales?: Intl.LocalesArgument): string {
12+
return str.replace(/\b\w/, (char: string) => char.toLocaleUpperCase(locales))
1313
}
1414

1515
/**
@@ -22,8 +22,8 @@ function capitalizeInitial(str: string): string {
2222
* capitalizeWords('Sphinx of black quartz:-judge my vow');
2323
* // 'Sphinx Of Black Quartz:-Judge My Vow'
2424
*/
25-
function capitalizeWords(str: string): string {
26-
return str.replace(/\b\w/g, (char: string) => char.toUpperCase())
25+
function capitalizeWords(str: string, locales?: Intl.LocalesArgument): string {
26+
return str.replace(/\b\w/g, (char: string) => char.toLocaleUpperCase(locales))
2727
}
2828

2929
/**
@@ -150,9 +150,9 @@ function isPascalCase(str: string): boolean {
150150
* snakeCase('from-kebab-case'); // 'from_kebab_case'
151151
* snakeCase('snake Case With Numbers123', true); // 'snake_case_with_numbers_one_two_three'
152152
*/
153-
function snakeCase(str: string, inWords = false): string {
154-
if (isCamelCase(str)) return getWords(str, 'camel').join('_').toLowerCase()
155-
if (isPascalCase(str)) return getWords(str, 'pascal').join('_').toLowerCase()
153+
function snakeCase(str: string, inWords = false, locales?: Intl.LocalesArgument): string {
154+
if (isCamelCase(str)) return getWords(str, 'camel').join('_').toLocaleLowerCase(locales)
155+
if (isPascalCase(str)) return getWords(str, 'pascal').join('_').toLocaleLowerCase(locales)
156156

157157
const numbersInWords = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
158158
if (inWords) {
@@ -161,7 +161,7 @@ function snakeCase(str: string, inWords = false): string {
161161
}
162162
}
163163

164-
return getWords(str).join('_').toLowerCase()
164+
return getWords(str).join('_').toLocaleLowerCase(locales)
165165
}
166166

167167
/**
@@ -174,9 +174,9 @@ function snakeCase(str: string, inWords = false): string {
174174
* kebabCase('from_snake_case'); // 'from-snake-case'
175175
* kebabCase('kebab Case With Numbers123', true); // 'kebab-case-with-numbers-one-two-three'
176176
*/
177-
function kebabCase(str: string, inWords = false): string {
178-
if (isCamelCase(str)) return getWords(str, 'camel').join('-').toLowerCase()
179-
if (isPascalCase(str)) return getWords(str, 'pascal').join('-').toLowerCase()
177+
function kebabCase(str: string, inWords = false, locales?: Intl.LocalesArgument): string {
178+
if (isCamelCase(str)) return getWords(str, 'camel').join('-').toLocaleLowerCase(locales)
179+
if (isPascalCase(str)) return getWords(str, 'pascal').join('-').toLocaleLowerCase(locales)
180180

181181
const numbersInWords = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
182182
if (inWords) {
@@ -185,7 +185,7 @@ function kebabCase(str: string, inWords = false): string {
185185
}
186186
}
187187

188-
return getWords(str).join('-').toLowerCase()
188+
return getWords(str).join('-').toLocaleLowerCase(locales)
189189
}
190190

191191
/**
@@ -197,14 +197,16 @@ function kebabCase(str: string, inWords = false): string {
197197
* camelCase('Test CaSe ExamplE'); // 'testCaseExample'
198198
* camelCase('camel Case With Numbers123'); // 'camelCaseWithNumbers'
199199
*/
200-
function camelCase(str: string): string {
200+
function camelCase(str: string, locales?: Intl.LocalesArgument): string {
201201
str = str.replace(/[^a-zA-Z]/g, ' ')
202202
if (isCamelCase(str)) return str
203-
if (isPascalCase(str)) return str.charAt(0).toLowerCase() + str.substring(1)
203+
if (isPascalCase(str)) return str.charAt(0).toLocaleLowerCase(locales) + str.substring(1)
204204
return getWords(str).reduce((prev: string, curVal: string, i: number): string =>
205205
i !== 1
206-
? prev + curVal.charAt(0).toUpperCase() + curVal.substring(1).toLowerCase()
207-
: prev.toLowerCase() + curVal.charAt(0).toUpperCase() + curVal.substring(1).toLowerCase()
206+
? prev + curVal.charAt(0).toLocaleUpperCase(locales) + curVal.substring(1).toLocaleLowerCase(locales)
207+
: prev.toLocaleLowerCase(locales) +
208+
curVal.charAt(0).toLocaleUpperCase(locales) +
209+
curVal.substring(1).toLocaleLowerCase(locales)
208210
)
209211
}
210212

@@ -217,13 +219,13 @@ function camelCase(str: string): string {
217219
* pascalCase('Test CaSe ExamplE'); // 'TestCaseExample'
218220
* pascalCase('pasCal Case With Numbers123'); // 'PascalCaseWithNumbers'
219221
*/
220-
function pascalCase(str: string): string {
222+
function pascalCase(str: string, locales?: Intl.LocalesArgument): string {
221223
str = str.replace(/[^a-zA-Z]/g, ' ')
222224
if (isPascalCase(str)) return str
223-
if (isCamelCase(str)) return str.charAt(0).toUpperCase() + str.substring(1)
225+
if (isCamelCase(str)) return str.charAt(0).toLocaleUpperCase(locales) + str.substring(1)
224226
return getWords(str).reduce(
225227
(prev: string, curVal: string): string =>
226-
prev + curVal.charAt(0).toUpperCase() + curVal.substring(1).toLowerCase(),
228+
prev + curVal.charAt(0).toLocaleUpperCase(locales) + curVal.substring(1).toLocaleLowerCase(locales),
227229

228230
''
229231
)

src/compare.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function compare(str1: string, str2: string): boolean {
3030
* @description
3131
* The `looseCompare` function performs a case-insensitive loose comparison between two strings.
3232
*/
33-
function looseCompare(str1: string, str2: string): boolean {
34-
return str1.toLowerCase() === str2.toLowerCase()
33+
function looseCompare(str1: string, str2: string, locales?: Intl.LocalesArgument): boolean {
34+
return str1.toLocaleLowerCase(locales) === str2.toLocaleLowerCase(locales)
3535
}
3636

3737
export { compare, looseCompare }

src/regionMatchers.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,18 @@ function regionMatch(str1: StringRegion | string, str2: StringRegion | string, s
7575
* const str6 = { str: 'HelLo there', start: 0, end: 5 };
7676
* looseRegionMatch(str5, str6); // true
7777
*/
78+
/* eslint-disable-next-line @typescript-eslint/max-params -- locales parameter enhances user accessibility and internationalization support */
7879
function looseRegionMatch(
7980
str1: StringRegion | string,
8081
str2: StringRegion | string,
8182
start?: number,
82-
end?: number
83+
end?: number,
84+
locales?: Intl.LocalesArgument
8385
): boolean {
84-
if (typeof str1 === 'string') str1 = str1.toLowerCase()
85-
else str1.str = str1.str.toLowerCase()
86-
if (typeof str2 === 'string') str2 = str2.toLowerCase()
87-
else str2.str = str2.str.toLowerCase()
86+
if (typeof str1 === 'string') str1 = str1.toLocaleLowerCase(locales)
87+
else str1.str = str1.str.toLocaleLowerCase(locales)
88+
if (typeof str2 === 'string') str2 = str2.toLocaleLowerCase(locales)
89+
else str2.str = str2.str.toLocaleLowerCase(locales)
8890

8991
return regionMatch(str1, str2, start, end)
9092
}

0 commit comments

Comments
 (0)