From ab73569393603c76480ffafc56c822ffc5fff3b7 Mon Sep 17 00:00:00 2001 From: simojenki Date: Sat, 14 Aug 2021 17:47:59 +1000 Subject: [PATCH] Trim langs from accept header --- src/i8n.ts | 2 +- tests/i8n.test.ts | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/i8n.ts b/src/i8n.ts index a1d2157..bd9e1fd 100644 --- a/src/i8n.ts +++ b/src/i8n.ts @@ -107,7 +107,7 @@ export const randomLang = () => _.shuffle(["en-US", "nl-NL"])[0]!; export const asLANGs = (acceptLanguageHeader: string | undefined) => { 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; diff --git a/tests/i8n.test.ts b/tests/i8n.test.ts index 39a5789..d804a6d 100644 --- a/tests/i8n.test.ts +++ b/tests/i8n.test.ts @@ -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", () => {