From 2aa9d5395cc876195359a8d598a7bea1a5bac5b3 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Wed, 20 Sep 2023 12:34:51 +0900 Subject: [PATCH 1/2] feat: add `parseAcceptLanguage` --- deno/mod.ts | 1 + mod.ts | 3 +++ src/index.ts | 13 +++++++++++++ test/index.test.ts | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 mod.ts diff --git a/deno/mod.ts b/deno/mod.ts index fc5653f..d9e6183 100644 --- a/deno/mod.ts +++ b/deno/mod.ts @@ -1 +1,2 @@ +// TODO: we must change package structure for deno export * from '../src/index.ts' diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..7f67017 --- /dev/null +++ b/mod.ts @@ -0,0 +1,3 @@ +import { isLocale } from 'https://deno.land/x/intlify_utils' + +console.log('isLocale', isLocale(new Intl.Locale('en-US'))) diff --git a/src/index.ts b/src/index.ts index 0e9b1b3..50ecb93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,3 +11,16 @@ const toTypeString = (value: unknown): string => objectToString.call(value) export function isLocale(val: unknown): val is Intl.Locale { return toTypeString(val) === '[object Intl.Locale]' } + +/** + * 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 === '') + ) +} diff --git a/test/index.test.ts b/test/index.test.ts index ad5cc2a..e2930d0 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from 'vitest' -import { isLocale } from '../src/index.ts' +import { isLocale, parseAcceptLanguage } from '../src/index.ts' describe('isLocale', () => { test('Locale instance', () => { @@ -10,3 +10,34 @@ describe('isLocale', () => { expect(isLocale('en-US')).toBe(false) }) }) + +describe('parseAcceptLanguage', () => { + test('basic: ja,en-US;q=0.7,en;q=0.3', () => { + expect(parseAcceptLanguage('ja,en-US;q=0.7,en;q=0.3')).toEqual([ + 'ja', + 'en-US', + 'en', + ]) + }) + + test('q-factor nothing: ja,en-US', () => { + expect(parseAcceptLanguage('ja,en-US')).toEqual([ + 'ja', + 'en-US', + ]) + }) + + test('single: ja', () => { + expect(parseAcceptLanguage('ja')).toEqual([ + 'ja', + ]) + }) + + test('any language: *', () => { + expect(parseAcceptLanguage('*')).toEqual([]) + }) + + test('empty: ""', () => { + expect(parseAcceptLanguage('')).toEqual([]) + }) +}) From 1b7c191e163cbbe54681df0d0ce1a64c49bd3f5c Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Wed, 20 Sep 2023 20:02:39 +0900 Subject: [PATCH 2/2] feat: support framework/runtime aginostic `getAcceptLanguages` --- build.config.ts | 16 +++++++- bun.lockb | Bin 127064 -> 129687 bytes deno/mod.ts | 1 - package.json | 16 ++++++++ src/h3.test.ts | 46 +++++++++++++++++++++++ src/h3.ts | 21 +++++++++++ src/http.ts | 8 ++++ src/index.ts | 27 +------------ src/node.test.ts | 30 +++++++++++++++ src/node.ts | 16 ++++++++ test/index.test.ts => src/shared.test.ts | 12 +++++- src/shared.ts | 43 +++++++++++++++++++++ src/web.test.ts | 21 +++++++++++ src/web.ts | 15 ++++++++ 14 files changed, 243 insertions(+), 29 deletions(-) create mode 100644 src/h3.test.ts create mode 100644 src/h3.ts create mode 100644 src/http.ts create mode 100644 src/node.test.ts create mode 100644 src/node.ts rename test/index.test.ts => src/shared.test.ts (75%) create mode 100644 src/shared.ts create mode 100644 src/web.test.ts create mode 100644 src/web.ts diff --git a/build.config.ts b/build.config.ts index d20a7a9..3527223 100644 --- a/build.config.ts +++ b/build.config.ts @@ -5,5 +5,19 @@ export default defineBuildConfig({ rollup: { emitCJS: true, }, - entries: ['./src/index.ts'], + entries: [ + { + input: './src/index.ts', + }, + { + input: './src/h3.ts', + }, + { + input: './src/node.ts', + }, + { + input: './src/web.ts', + }, + ], + externals: ['h3'],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B;7M>0d>a&=#}<=_tm7$#20*z*Ax#de#R#G<^@e4?Y0gd3!)Z5CYbLKLc*Chrv8B2VfI= zG=D_+D&U`JIZ$&;;?0&@iY2iq+yV zGjh3b*>D@MU~9w&KwCjsARO8fM1ly=95e&mfi0AJ<85HapbY7V_)_Q!&>nFE+722G zf)HQA@2Czi=;WQMGt>ug9oz+YhvVAr4Y;+r-s6E2e%`_Rf*9Cc0RFT*&=rwU(4J5y zhy`(g8@3yiuRafhuR#xH`Wz817-mWUY={l^0^WNGrYbCx1~ z-4LUEsR*X+E8(9#)QFLy&=!ztyd_0&sP})Go~||HjXh)E`xrgw5Y?VNh)FdnnZx@} zP8r#=lhWp`+JhV&qoSj5I~bE{gfBxI(^8EmmZ6QJRAcQj(K38ds`=ef=m^vKQSI7mM^cR|%g`G+%!pWy-ZUR>ytEv>Up3t5wL-KHwf@6OE1Wq$ z?`{9`8w*epefp_afj;P9vs7dA3KX(5INbYRSSQQ3H`sA+)d91)&g^LLBI{xdS;XhQ z0Ar;gqHtF>-Zw;it$8YO$A#j0Pm1;Zi$54E+w^WKV`hNgt8_}5n diff --git a/deno/mod.ts b/deno/mod.ts index d9e6183..fc5653f 100644 --- a/deno/mod.ts +++ b/deno/mod.ts @@ -1,2 +1 @@ -// TODO: we must change package structure for deno export * from '../src/index.ts' diff --git a/package.json b/package.json index ad68377..ff39842 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,21 @@ "import": "./dist/index.mjs", "require": "./dist/index.cjs" }, + "./h3": { + "types": "./dist/h3.d.ts", + "import": "./dist/h3.mjs", + "require": "./dist/h3.cjs" + }, + "./node": { + "types": "./dist/node.d.ts", + "import": "./dist/node.mjs", + "require": "./dist/node.cjs" + }, + "./web": { + "types": "./dist/web.d.ts", + "import": "./dist/web.mjs", + "require": "./dist/web.cjs" + }, "./dist/*": "./dist/*", "./package.json": "./package.json" }, @@ -67,6 +82,7 @@ "@vitest/coverage-v8": "^0.34.4", "bumpp": "^9.2.0", "gh-changelogen": "^0.2.8", + "h3": "^1.8.1", "lint-staged": "^14.0.0", "typescript": "^5.2.2", "unbuild": "^2.0.0", diff --git a/src/h3.test.ts b/src/h3.test.ts new file mode 100644 index 0000000..d5f3a04 --- /dev/null +++ b/src/h3.test.ts @@ -0,0 +1,46 @@ +import { describe, expect, test } from 'vitest' +import { getAcceptLanguages } from './h3.ts' + +import type { H3Event } from 'h3' + +describe('getAcceptLanguages', () => { + test('basic', () => { + const eventMock = { + node: { + req: { + method: 'GET', + headers: { + 'accept-language': 'en-US,en;q=0.9,ja;q=0.8', + }, + }, + }, + } as H3Event + expect(getAcceptLanguages(eventMock)).toEqual(['en-US', 'en', 'ja']) + }) + + test('any language', () => { + const eventMock = { + node: { + req: { + method: 'GET', + headers: { + 'accept-language': '*', + }, + }, + }, + } as H3Event + expect(getAcceptLanguages(eventMock)).toEqual([]) + }) + + test('empty', () => { + const eventMock = { + node: { + req: { + method: 'GET', + headers: {}, + }, + }, + } as H3Event + expect(getAcceptLanguages(eventMock)).toEqual([]) + }) +}) diff --git a/src/h3.ts b/src/h3.ts new file mode 100644 index 0000000..c63d42b --- /dev/null +++ b/src/h3.ts @@ -0,0 +1,21 @@ +import { getAcceptLanguagesFromGetter } from './http.ts' +import { getHeaders } from 'h3' + +import type { H3Event } from 'h3' + +/** + * get accpet languages + * + * @description parse `accept-language` header string + * + * @param {H3Event} event The {@link H3Event | H3} event + * + * @returns {Array} The array of language tags, if `*` (any language) or empty string is detected, return an empty array. + */ +export function getAcceptLanguages(event: H3Event): string[] { + const getter = () => { + const headers = getHeaders(event) + return headers['accept-language'] + } + return getAcceptLanguagesFromGetter(getter) +} diff --git a/src/http.ts b/src/http.ts new file mode 100644 index 0000000..9e48954 --- /dev/null +++ b/src/http.ts @@ -0,0 +1,8 @@ +import { parseAcceptLanguage } from './shared.ts' + +export function getAcceptLanguagesFromGetter( + getter: () => string | null | undefined, +): string[] { + const acceptLanguage = getter() + return acceptLanguage ? parseAcceptLanguage(acceptLanguage) : [] +} diff --git a/src/index.ts b/src/index.ts index 50ecb93..34e93b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,26 +1 @@ -const objectToString = Object.prototype.toString -const toTypeString = (value: unknown): string => objectToString.call(value) - -/** - * check whether the value is a {@link Intl.Locale} instance - * - * @param {unknown} val The locale value - * - * @returns {boolean} Returns `true` if the value is a {@link Intl.Locale} instance, else `false`. - */ -export function isLocale(val: unknown): val is Intl.Locale { - return toTypeString(val) === '[object Intl.Locale]' -} - -/** - * 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 === '') - ) -} +export * from './shared.ts' diff --git a/src/node.test.ts b/src/node.test.ts new file mode 100644 index 0000000..04d59a1 --- /dev/null +++ b/src/node.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, test } from 'vitest' +import { getAcceptLanguages } from './node.ts' +import { IncomingMessage } from 'node:http' + +describe('getAcceptLanguages', () => { + test('basic', () => { + const mockRequest = { + headers: { + 'accept-language': 'en-US,en;q=0.9,ja;q=0.8', + }, + } as IncomingMessage + expect(getAcceptLanguages(mockRequest)).toEqual(['en-US', 'en', 'ja']) + }) + + test('any language', () => { + const mockRequest = { + headers: { + 'accept-language': '*', + }, + } as IncomingMessage + expect(getAcceptLanguages(mockRequest)).toEqual([]) + }) + + test('empty', () => { + const mockRequest = { + headers: {}, + } as IncomingMessage + expect(getAcceptLanguages(mockRequest)).toEqual([]) + }) +}) diff --git a/src/node.ts b/src/node.ts new file mode 100644 index 0000000..2d1bb6c --- /dev/null +++ b/src/node.ts @@ -0,0 +1,16 @@ +import { IncomingMessage } from 'node:http' +import { getAcceptLanguagesFromGetter } from './http.ts' + +/** + * get accpet languages + * + * @description parse `accept-language` header string + * + * @param {IncomingMessage} event The {@link IncomingMessage | request} + * + * @returns {Array} The array of language tags, if `*` (any language) or empty string is detected, return an empty array. + */ +export function getAcceptLanguages(req: IncomingMessage) { + const getter = () => req.headers['accept-language'] + return getAcceptLanguagesFromGetter(getter) +} diff --git a/test/index.test.ts b/src/shared.test.ts similarity index 75% rename from test/index.test.ts rename to src/shared.test.ts index e2930d0..85d2923 100644 --- a/test/index.test.ts +++ b/src/shared.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from 'vitest' -import { isLocale, parseAcceptLanguage } from '../src/index.ts' +import { isLocale, parseAcceptLanguage, validateLanguageTag } from './shared.ts' describe('isLocale', () => { test('Locale instance', () => { @@ -41,3 +41,13 @@ describe('parseAcceptLanguage', () => { expect(parseAcceptLanguage('')).toEqual([]) }) }) + +describe('validateLanguageTag', () => { + test('valid', () => { + expect(validateLanguageTag('en-US')).toBe(true) + }) + + test('invalid', () => { + expect(validateLanguageTag('j')).toBe(false) + }) +}) diff --git a/src/shared.ts b/src/shared.ts new file mode 100644 index 0000000..980dfdb --- /dev/null +++ b/src/shared.ts @@ -0,0 +1,43 @@ +const objectToString = Object.prototype.toString +const toTypeString = (value: unknown): string => objectToString.call(value) + +/** + * check whether the value is a {@link Intl.Locale} instance + * + * @param {unknown} val The locale value + * + * @returns {boolean} Returns `true` if the value is a {@link Intl.Locale} instance, else `false`. + */ +export function isLocale(val: unknown): val is Intl.Locale { + return toTypeString(val) === '[object Intl.Locale]' +} + +/** + * 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}. + * + * @param {string} lang a language tag + * + * @returns {boolean} Returns `true` if the language tag is valid, else `false`. + */ +export function validateLanguageTag(lang: string): boolean { + try { + // TODO: if we have a better way to validate the language tag, we should use it. + new Intl.Locale(lang) + return true + } catch { + return false + } +} diff --git a/src/web.test.ts b/src/web.test.ts new file mode 100644 index 0000000..aa8ebe0 --- /dev/null +++ b/src/web.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, test } from 'vitest' +import { getAcceptLanguages } from './web.ts' + +describe('getAcceptLanguages', () => { + test('basic', () => { + const mockRequest = new Request('https://example.com') + mockRequest.headers.set('accept-language', 'en-US,en;q=0.9,ja;q=0.8') + expect(getAcceptLanguages(mockRequest)).toEqual(['en-US', 'en', 'ja']) + }) + + test('any language', () => { + const mockRequest = new Request('https://example.com') + mockRequest.headers.set('accept-language', '*') + expect(getAcceptLanguages(mockRequest)).toEqual([]) + }) + + test('empty', () => { + const mockRequest = new Request('https://example.com') + expect(getAcceptLanguages(mockRequest)).toEqual([]) + }) +}) diff --git a/src/web.ts b/src/web.ts new file mode 100644 index 0000000..19a9e69 --- /dev/null +++ b/src/web.ts @@ -0,0 +1,15 @@ +import { getAcceptLanguagesFromGetter } from './http.ts' + +/** + * get accpet languages + * + * @description parse `accept-language` header string + * + * @param {Request} event The {@link Request | request} + * + * @returns {Array} The array of language tags, if `*` (any language) or empty string is detected, return an empty array. + */ +export function getAcceptLanguages(req: Request) { + const getter = () => req.headers.get('accept-language') + return getAcceptLanguagesFromGetter(getter) +}