diff --git a/src/i8n.ts b/src/i8n.ts index bd9e1fd..538894a 100644 --- a/src/i8n.ts +++ b/src/i8n.ts @@ -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> = { 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; diff --git a/src/navidrome.ts b/src/navidrome.ts index 7e01c1b..06479e3 100644 --- a/src/navidrome.ts +++ b/src/navidrome.ts @@ -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) );