diff --git a/src/http.ts b/src/http.ts index b9a003d..654de43 100644 --- a/src/http.ts +++ b/src/http.ts @@ -5,7 +5,7 @@ import { parseAcceptLanguage, pathLanguageParser, toLocale, - validateLanguageTag, + validateLangTag, } from './shared.ts' import { ACCEPT_LANGUAGE_HEADER } from './constants.ts' @@ -145,7 +145,7 @@ export function getLocaleWithGetter(getter: () => string): Intl.Locale { export function validateLocale(locale: string | Intl.Locale): void { if ( !(isLocale(locale) || - typeof locale === 'string' && validateLanguageTag(locale)) + typeof locale === 'string' && validateLangTag(locale)) ) { throw new SyntaxError(`locale is invalid: ${locale.toString()}`) } diff --git a/src/index.ts b/src/index.ts index f7a8bf5..09982a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ export { normalizeLanguageName, parseAcceptLanguage, registerPathLanguageParser, - validateLanguageTag, + validateLangTag, } from './shared.ts' export { getPathLanguage, diff --git a/src/shared.test.ts b/src/shared.test.ts index e319e5f..5f24e44 100644 --- a/src/shared.test.ts +++ b/src/shared.test.ts @@ -6,7 +6,7 @@ import { parseAcceptLanguage, pathLanguageParser, toLocale, - validateLanguageTag, + validateLangTag, } from './shared.ts' describe('isLocale', () => { @@ -65,13 +65,13 @@ describe('parseAcceptLanguage', () => { }) }) -describe('validateLanguageTag', () => { +describe('validateLangTag', () => { test('valid', () => { - expect(validateLanguageTag('en-US')).toBe(true) + expect(validateLangTag('en-US')).toBe(true) }) test('invalid', () => { - expect(validateLanguageTag('j')).toBe(false) + expect(validateLangTag('j')).toBe(false) }) }) diff --git a/src/shared.ts b/src/shared.ts index 219b1d3..70bf362 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -33,19 +33,6 @@ export function toLocale(val: string | Intl.Locale): Intl.Locale { return isLocale(val) ? val : new Intl.Locale(val) } -/** - * parse `accept-language` header string - * - * @param {string} value The accept-language header string - * - * @returns {Array} The array of language tags, if `*` (any language) or empty string is detected, return an empty array. - */ -export function parseAcceptLanguage(value: string): string[] { - return value.split(',').map((tag) => tag.split(';')[0]).filter((tag) => - !(tag === '*' || tag === '') - ) -} - /** * validate the language tag whether is a well-formed {@link https://datatracker.ietf.org/doc/html/rfc4646#section-2.1 | BCP 47 language tag}. * @@ -53,7 +40,7 @@ export function parseAcceptLanguage(value: string): string[] { * * @returns {boolean} Returns `true` if the language tag is valid, else `false`. */ -export function validateLanguageTag(lang: string): boolean { +export function validateLangTag(lang: string): boolean { try { // TODO: if we have a better way to validate the language tag, we should use it. new Intl.Locale(lang) @@ -63,6 +50,19 @@ export function validateLanguageTag(lang: string): boolean { } } +/** + * parse `accept-language` header string + * + * @param {string} value The accept-language header string + * + * @returns {Array} The array of language tags, if `*` (any language) or empty string is detected, return an empty array. + */ +export function parseAcceptLanguage(value: string): string[] { + return value.split(',').map((tag) => tag.split(';')[0]).filter((tag) => + !(tag === '*' || tag === '') + ) +} + /** * nomralize the language name *