Skip to content

Commit

Permalink
BREAKING CHANGE: change to validateLangTag from validateLanguageTag (#19
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kazupon committed Sep 29, 2023
1 parent a77f3e9 commit 0fa17f3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
parseAcceptLanguage,
pathLanguageParser,
toLocale,
validateLanguageTag,
validateLangTag,
} from './shared.ts'
import { ACCEPT_LANGUAGE_HEADER } from './constants.ts'

Expand Down Expand Up @@ -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()}`)
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export {
normalizeLanguageName,
parseAcceptLanguage,
registerPathLanguageParser,
validateLanguageTag,
validateLangTag,
} from './shared.ts'
export {
getPathLanguage,
Expand Down
8 changes: 4 additions & 4 deletions src/shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
parseAcceptLanguage,
pathLanguageParser,
toLocale,
validateLanguageTag,
validateLangTag,
} from './shared.ts'

describe('isLocale', () => {
Expand Down Expand Up @@ -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)
})
})

Expand Down
28 changes: 14 additions & 14 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,14 @@ 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<string>} 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}.
*
* @param {string} lang a language tag
*
* @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)
Expand All @@ -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<string>} 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
*
Expand Down

0 comments on commit 0fa17f3

Please sign in to comment.