Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: change to validateLangTag from validateLanguageTag #19

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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