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

Handle additional whitespaces and don't throw exception on invalid locale #211

Merged
merged 2 commits into from
Sep 17, 2024
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
10 changes: 10 additions & 0 deletions src/lib/get-client-locales.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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', () => {
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)
});
});
20 changes: 17 additions & 3 deletions src/lib/get-client-locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,25 @@ export function getClientLocales(requestOrHeaders: Request | Headers): Locales {
// if the header is not defined, return undefined
if (!acceptLanguage) return undefined;

let parsedLocales = parse(acceptLanguage)
.filter((lang) => lang.code !== "*")
.map(formatLanguageString);

let validLocales: string[] = []

for (let locale of parsedLocales) {
try {
// This will throw on invalid locales
new Intl.Locale(locale);

// If we get here, the locale is valid
validLocales.push(locale);
} catch {}
}

let locale = pick(
Intl.DateTimeFormat.supportedLocalesOf(
parse(acceptLanguage)
.filter((lang) => lang.code !== "*")
.map(formatLanguageString),
validLocales,
),
acceptLanguage,
);
Expand Down
9 changes: 9 additions & 0 deletions src/lib/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ describe(parser.parse.name, () => {
{ code: "en", quality: 0.4, script: null },
{ code: "*", quality: 0.1, script: null },
]);

let result2 = parser.parse("zh-CN, zh; q=0.9, en; q=0.8, ko; q=0.7");

expect(result2).toEqual([
{ code: "zh", region: "CN", quality: 1.0, script: null },
{ code: "zh", quality: 0.9, script: null },
{ code: "en", quality: 0.8, script: null },
{ code: "ko", quality: 0.7, script: null },
]);
});

test("should sort based on quality value", () => {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 All @@ -25,6 +25,9 @@ export function parse(acceptLanguage?: string): Language[] {

for (let m of strings) {
if (!m) continue;

m = m.trim();

let bits = m.split(";");
let ietf = bits[0]?.split("-") ?? [];
let hasScript = ietf.length === 3;
Expand Down