UNPKG

fp-ts-std

Version:

The missing pseudo-standard library for fp-ts.

50 lines (49 loc) 2.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Eq = exports.setHash = exports.modifyHash = exports.getHash = exports.setParams = exports.modifyParams = exports.getParams = exports.setPathname = exports.modifyPathname = exports.getPathname = exports.toString = exports.fromPathname = exports.fromString = exports.toURL = exports.fromURL = exports.clone = exports.isURLPath = void 0; const Eq_ = require("fp-ts/Eq"); const O = require("fp-ts/Option"); const function_1 = require("fp-ts/function"); const Newtype_1 = require("./Newtype"); const URL = require("./URL"); const Params = require("./URLSearchParams"); const phonyBase = new globalThis.URL("https://urlpath.fp-ts-std.samhh.com"); const ensurePhonyBase = x => (0, function_1.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; const isURLPath = (u) => URL.isURL(u) && u.origin === phonyBase.origin; exports.isURLPath = isURLPath; exports.clone = (0, Newtype_1.over)(URL.clone); const fromURL = (x) => (0, function_1.pipe)(new globalThis.URL(x.href, phonyBase), ensurePhonyBase, (Newtype_1.pack)); exports.fromURL = fromURL; const toURL = (origin) => (path) => (0, function_1.pipe)(origin, URL.setPathname((0, exports.getPathname)(path)), URL.setParams((0, exports.getParams)(path)), URL.setHash((0, exports.getHash)(path))); exports.toURL = toURL; const fromString = (x) => (0, function_1.pipe)(O.tryCatch(() => new globalThis.URL(`${phonyBase.origin}${x}`)), O.filter(hasPhonyBase), O.getOrElse(() => new globalThis.URL(`${phonyBase.origin}/${x}`)), (Newtype_1.pack)); exports.fromString = fromString; const fromPathname = (x) => { const y = URL.clone(phonyBase); y.pathname = x; return (0, Newtype_1.pack)(y); }; exports.fromPathname = fromPathname; exports.toString = (0, function_1.flow)(Newtype_1.unpack, x => x.pathname + x.search + x.hash); exports.getPathname = (0, function_1.flow)(Newtype_1.unpack, URL.getPathname); const modifyPathname = (f) => (0, Newtype_1.over)(URL.modifyPathname(f)); exports.modifyPathname = modifyPathname; const setPathname = (x) => (0, Newtype_1.over)(URL.setPathname(x)); exports.setPathname = setPathname; exports.getParams = (0, function_1.flow)(Newtype_1.unpack, URL.getParams); const modifyParams = (f) => (0, Newtype_1.over)(URL.modifyParams(f)); exports.modifyParams = modifyParams; const setParams = (x) => (0, Newtype_1.over)(URL.setParams(x)); exports.setParams = setParams; exports.getHash = (0, function_1.flow)(Newtype_1.unpack, URL.getHash); const modifyHash = (f) => (0, Newtype_1.over)(URL.modifyHash(f)); exports.modifyHash = modifyHash; const setHash = (x) => (0, Newtype_1.over)(URL.setHash(x)); exports.setHash = setHash; exports.Eq = Eq_.contramap(Newtype_1.unpack)(URL.Eq);