Skip to content

Commit 37f012b

Browse files
authored
chore(packages/spec): add precise types for locales.ts (#580)
1 parent 70a4db0 commit 37f012b

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

.changeset/funny-shoes-scream.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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ const localeMap = {
201201
export type LocaleCodeShort = keyof typeof localeMap;
202202
export type LocaleCodeFull = (typeof localeMap)[LocaleCodeShort][number];
203203
export type LocaleCode = LocaleCodeShort | LocaleCodeFull;
204+
export type LocaleDelimiter = "-" | "_" | null;
204205

205206
export const localeCodesShort = Object.keys(localeMap) as LocaleCodeShort[];
206207
export const localeCodesFull = Object.values(localeMap).flat() as LocaleCodeFull[];
@@ -239,7 +240,7 @@ export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => {
239240
throw new Error(`Invalid locale code: ${value}`);
240241
};
241242

242-
export const getLocaleCodeDelimiter = (locale: string): string | null => {
243+
export const getLocaleCodeDelimiter = (locale: string): LocaleDelimiter => {
243244
if (locale.includes("_")) {
244245
return "_";
245246
} else if (locale.includes("-")) {
@@ -249,7 +250,7 @@ export const getLocaleCodeDelimiter = (locale: string): string | null => {
249250
}
250251
};
251252

252-
export const resolveOverriddenLocale = (locale: string, delimiter?: "-" | "_" | null): string => {
253+
export const resolveOverriddenLocale = (locale: string, delimiter?: LocaleDelimiter): string => {
253254
if (!delimiter) {
254255
return locale;
255256
}
@@ -262,6 +263,6 @@ export const resolveOverriddenLocale = (locale: string, delimiter?: "-" | "_" |
262263
return locale.replace(currentDelimiter, delimiter);
263264
};
264265

265-
export function normalizeLocale(locale: string) {
266+
export function normalizeLocale(locale: string): string {
266267
return locale.replaceAll("_", "-").replace(/([a-z]{2,3}-)r/, "$1");
267268
}

0 commit comments

Comments
 (0)