diff --git a/package.json b/package.json index 34a9844..44a53fa 100644 --- a/package.json +++ b/package.json @@ -55,13 +55,7 @@ }, "funding": "https://github.com/sponsors/sergiodxa", "homepage": "https://github.com/sergiodxa/remix-i18next#readme", - "keywords": [ - "remix", - "i18n", - "i18next", - "ssr", - "csr" - ], + "keywords": ["remix", "i18n", "i18next", "ssr", "csr"], "license": "MIT", "peerDependenciesMeta": { "@remix-run/cloudflare": { diff --git a/src/lib/get-client-locales.test.ts b/src/lib/get-client-locales.test.ts index eacba30..2aa34fd 100644 --- a/src/lib/get-client-locales.test.ts +++ b/src/lib/get-client-locales.test.ts @@ -2,9 +2,14 @@ import { describe, expect, test } from "bun:test"; import * as getClientLocales from "./get-client-locales.js"; describe(getClientLocales.getClientLocales.name, () => { - test('should not throw on invalid locales', () => { + test("should not throw on invalid locales", () => { let headers = new Headers(); - headers.set("Accept-Language", "cs-CZ,cs;q=0.9,true;q=0.8,en-US;q=0.7,en;q=0.6"); - expect(() => getClientLocales.getClientLocales(headers)).not.toThrowError(RangeError) + headers.set( + "Accept-Language", + "cs-CZ,cs;q=0.9,true;q=0.8,en-US;q=0.7,en;q=0.6", + ); + expect(() => getClientLocales.getClientLocales(headers)).not.toThrowError( + RangeError, + ); }); -}); \ No newline at end of file +}); diff --git a/src/lib/get-client-locales.ts b/src/lib/get-client-locales.ts index bcfb207..829a0d9 100644 --- a/src/lib/get-client-locales.ts +++ b/src/lib/get-client-locales.ts @@ -29,10 +29,10 @@ export function getClientLocales(requestOrHeaders: Request | Headers): Locales { if (!acceptLanguage) return undefined; let parsedLocales = parse(acceptLanguage) - .filter((lang) => lang.code !== "*") - .map(formatLanguageString); + .filter((lang) => lang.code !== "*") + .map(formatLanguageString); - let validLocales: string[] = [] + let validLocales: string[] = []; for (let locale of parsedLocales) { try { @@ -41,13 +41,13 @@ export function getClientLocales(requestOrHeaders: Request | Headers): Locales { // If we get here, the locale is valid validLocales.push(locale); - } catch {} + } catch { + // We want to ignore errors here + } } let locale = pick( - Intl.DateTimeFormat.supportedLocalesOf( - validLocales, - ), + Intl.DateTimeFormat.supportedLocalesOf(validLocales), acceptLanguage, ); diff --git a/src/lib/parser.ts b/src/lib/parser.ts index 1b89e2e..1238f37 100644 --- a/src/lib/parser.ts +++ b/src/lib/parser.ts @@ -1,6 +1,7 @@ import { formatLanguageString } from "./format-language-string.js"; -let REGEX = /[ ]*((([a-zA-Z]+(-[a-zA-Z0-9]+){0,2})|\*)(;[ ]*q=[0-1](\.[0-9]+)?[ ]*)?)*/g; +let REGEX = + /[ ]*((([a-zA-Z]+(-[a-zA-Z0-9]+){0,2})|\*)(;[ ]*q=[0-1](\.[0-9]+)?[ ]*)?)*/g; export interface Language { code: string; @@ -39,7 +40,7 @@ export function parse(acceptLanguage?: string): Language[] { region: hasScript ? ietf[2] : ietf[1], quality: bits[1] ? // biome-ignore lint/style/noNonNullAssertion: We know this is not null - Number.parseFloat(bits[1]!.split("=")[1]!) ?? 1.0 + (Number.parseFloat(bits[1]!.split("=")[1]!) ?? 1.0) : 1.0, }); }