Skip to content

Commit 85cbd0a

Browse files
committed
refactor: added JSDoc comments to exported functions in locales.ts
1 parent 401a172 commit 85cbd0a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

.changeset/big-moons-design.md

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

packages/spec/src/locales.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,19 @@ export const localeCodeSchema = Z.string().refine((value) => localeCodes.include
221221
message: "Invalid locale code",
222222
});
223223

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

255+
256+
/**
257+
* Determines the delimiter used in a locale code
258+
*
259+
* @param {string} locale - the locale string (e.g.,"en_US","en-GB")
260+
* @return { string | null} - The delimiter ("_" or "-") if found, otherwise `null`.
261+
*/
262+
263+
242264
export const getLocaleCodeDelimiter = (locale: string): string | null => {
243265
if (locale.includes("_")) {
244266
return "_";
@@ -249,6 +271,16 @@ export const getLocaleCodeDelimiter = (locale: string): string | null => {
249271
}
250272
};
251273

274+
/**
275+
* Replaces the delimiter in a locale string with the specified delimiter.
276+
*
277+
* @param {string}locale - The locale string (e.g.,"en_US", "en-GB").
278+
* @param {"-" | "_" | null} [delimiter] - The new delimiter to replace the existing one.
279+
* @returns {string} The locale string with the replaced delimiter, or the original locale if no delimiter is provided.
280+
*/
281+
282+
283+
252284
export const resolveOverridenLocale = (locale: string, delimiter?: "-" | "_" | null): string => {
253285
if (!delimiter) {
254286
return locale;
@@ -262,6 +294,15 @@ export const resolveOverridenLocale = (locale: string, delimiter?: "-" | "_" | n
262294
return locale.replace(currentDelimiter, delimiter);
263295
};
264296

265-
export function normalizeLocale(locale: string) {
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+
305+
306+
export const normalizeLocale = (locale: string): string => {
266307
return locale.replaceAll("_", "-").replace(/([a-z]{2,3}-)r/, "$1");
267308
}

0 commit comments

Comments
 (0)