Trim langs from accept header

This commit is contained in:
simojenki
2021-08-14 17:47:59 +10:00
parent 0d21c34243
commit ab73569393
2 changed files with 10 additions and 1 deletions

View File

@@ -107,7 +107,7 @@ export const randomLang = () => _.shuffle(["en-US", "nl-NL"])[0]!;
export const asLANGs = (acceptLanguageHeader: string | undefined) => { export const asLANGs = (acceptLanguageHeader: string | undefined) => {
const z = acceptLanguageHeader?.split(";")[0]; const z = acceptLanguageHeader?.split(";")[0];
return z && z != "" ? z.split(",") : []; return z && z != "" ? z.split(",").map(it => it.trim()) : [];
}; };
export type I8N = (...langs: string[]) => Lang; export type I8N = (...langs: string[]) => Lang;

View File

@@ -21,6 +21,15 @@ describe("i8n", () => {
]); ]);
}); });
}); });
describe("when there are multiple in the accept-langauge header with spaces", () => {
it("should split them out and return them", () => {
expect(asLANGs("es-ES, es, en-US;q=0.9, en;q=0.8")).toEqual([
"es-ES",
"es",
"en-US",
]);
});
});
}); });
describe("langs", () => { describe("langs", () => {