Skip to content

Commit b71bf63

Browse files
authored
refactor: added JSDoc comments to exported functions in locales.ts (#589)
1 parent 0191bf9 commit b71bf63

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

.changeset/new-buses-carry.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ node_modules
1414

1515
# Testing
1616
coverage
17+
.idea
1718

1819
# Turbo
1920
.turbo

packages/spec/src/locales.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@ export const localeCodeSchema = Z.string().refine((value) => localeCodes.include
222222
message: "Invalid locale code",
223223
});
224224

225+
226+
/**
227+
* Resolves a locale code to its full locale representation.
228+
*
229+
* If the provided locale code is already a full locale code, it returns as is.
230+
* If the provided locale code is a short locale code, it returns the first corresponding full locale.
231+
* If the locale code is not found, it throws an error.
232+
*
233+
* @param {localeCodes} value - The locale code to resolve (either short or full)
234+
* @return {LocaleCodeFull} The resolved full locale code
235+
* @throws {Error} If the provided locale code is invalid.
236+
*/
237+
238+
225239
export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => {
226240
const existingFullLocaleCode = Object.values(localeMap)
227241
.flat()
@@ -240,6 +254,15 @@ export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => {
240254
throw new Error(`Invalid locale code: ${value}`);
241255
};
242256

257+
/**
258+
* Determines the delimiter used in a locale code
259+
*
260+
* @param {string} locale - the locale string (e.g.,"en_US","en-GB")
261+
* @return { string | null} - The delimiter ("_" or "-") if found, otherwise `null`.
262+
*/
263+
264+
265+
243266
export const getLocaleCodeDelimiter = (locale: string): LocaleDelimiter => {
244267
if (locale.includes("_")) {
245268
return "_";
@@ -250,6 +273,14 @@ export const getLocaleCodeDelimiter = (locale: string): LocaleDelimiter => {
250273
}
251274
};
252275

276+
/**
277+
* Replaces the delimiter in a locale string with the specified delimiter.
278+
*
279+
* @param {string}locale - The locale string (e.g.,"en_US", "en-GB").
280+
* @param {"-" | "_" | null} [delimiter] - The new delimiter to replace the existing one.
281+
* @returns {string} The locale string with the replaced delimiter, or the original locale if no delimiter is provided.
282+
*/
283+
253284
export const resolveOverriddenLocale = (locale: string, delimiter?: LocaleDelimiter): string => {
254285
if (!delimiter) {
255286
return locale;
@@ -263,6 +294,14 @@ export const resolveOverriddenLocale = (locale: string, delimiter?: LocaleDelimi
263294
return locale.replace(currentDelimiter, delimiter);
264295
};
265296

297+
/**
298+
* Normalizes a locale string by replacing underscores with hyphens
299+
* and removing the "r" in certain regional codes (e.g., "fr-rCA" → "fr-CA")
300+
*
301+
* @param {string} locale - The locale string (e.g.,"en_US", "en-GB").
302+
* @return {string} The normalized locale string.
303+
*/
304+
266305
export function normalizeLocale(locale: string): string {
267306
return locale.replaceAll("_", "-").replace(/([a-z]{2,3}-)r/, "$1");
268307
}

0 commit comments

Comments
 (0)