Skip to content

Commit

Permalink
Fix code quality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiodxa-silverback committed Sep 17, 2024
1 parent 729c566 commit dbb8019
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
13 changes: 9 additions & 4 deletions src/lib/get-client-locales.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
});
});
});
14 changes: 7 additions & 7 deletions src/lib/get-client-locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
);

Expand Down
5 changes: 3 additions & 2 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
});
}
Expand Down

0 comments on commit dbb8019

Please sign in to comment.