Small refactor of accept-language header splitting

This commit is contained in:
simojenki
2021-08-14 18:09:27 +10:00
parent ab73569393
commit 368274c65b
2 changed files with 20 additions and 5 deletions

View File

@@ -1,3 +1,6 @@
import * as A from "fp-ts/Array";
import { pipe } from "fp-ts/lib/function";
import { option as O } from "fp-ts";
import _ from "underscore";
export type LANG = "en-US" | "nl-NL";
@@ -105,10 +108,21 @@ const translations: Record<LANG, Record<KEY, string>> = {
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(",").map(it => it.trim()) : [];
};
export const asLANGs = (acceptLanguageHeader: string | undefined) =>
pipe(
acceptLanguageHeader,
O.fromNullable,
O.map((it) => it.split(";")),
O.map((it) => it.shift() || ""),
O.map((it) =>
pipe(
it.split(","),
A.map((it) => it.trim()),
A.filter((it) => it != "")
)
),
O.getOrElseW(() => [])
);
export type I8N = (...langs: string[]) => Lang;

View File

@@ -268,7 +268,8 @@ export const asGenre = (genreName: string) => ({
const maybeAsGenre = (genreName: string | undefined): Genre | undefined =>
pipe(
O.fromNullable(genreName),
genreName,
O.fromNullable,
O.map(asGenre),
O.getOrElseW(() => undefined)
);