UNPKG

fp-ts-std

Version:

The missing pseudo-standard library for fp-ts.

36 lines (35 loc) 1.88 kB
import * as Eq_ from "fp-ts/Eq"; import * as O from "fp-ts/Option"; import { flow, pipe } from "fp-ts/function"; import { over, pack, unpack } from "./Newtype"; import * as URL from "./URL"; import * as Params from "./URLSearchParams"; const phonyBase = new globalThis.URL("https://urlpath.fp-ts-std.samhh.com"); const ensurePhonyBase = x => pipe(phonyBase, URL.clone, b => { b.pathname = x.pathname; b.search = Params.toString(x.searchParams); b.hash = x.hash; return b; }); const hasPhonyBase = x => x.origin === phonyBase.origin; export const isURLPath = (u) => URL.isURL(u) && u.origin === phonyBase.origin; export const clone = over(URL.clone); export const fromURL = (x) => pipe(new globalThis.URL(x.href, phonyBase), ensurePhonyBase, (pack)); export const toURL = (origin) => (path) => pipe(origin, URL.setPathname(getPathname(path)), URL.setParams(getParams(path)), URL.setHash(getHash(path))); export const fromString = (x) => pipe(O.tryCatch(() => new globalThis.URL(`${phonyBase.origin}${x}`)), O.filter(hasPhonyBase), O.getOrElse(() => new globalThis.URL(`${phonyBase.origin}/${x}`)), (pack)); export const fromPathname = (x) => { const y = URL.clone(phonyBase); y.pathname = x; return pack(y); }; export const toString = flow(unpack, x => x.pathname + x.search + x.hash); export const getPathname = flow(unpack, URL.getPathname); export const modifyPathname = (f) => over(URL.modifyPathname(f)); export const setPathname = (x) => over(URL.setPathname(x)); export const getParams = flow(unpack, URL.getParams); export const modifyParams = (f) => over(URL.modifyParams(f)); export const setParams = (x) => over(URL.setParams(x)); export const getHash = flow(unpack, URL.getHash); export const modifyHash = (f) => over(URL.modifyHash(f)); export const setHash = (x) => over(URL.setHash(x)); export const Eq = Eq_.contramap(unpack)(URL.Eq);