UNPKG

@jsenv/href

Version:

url helpers not relying on URL constructor

28 lines (21 loc) 806 B
import { hrefToScheme } from "../hrefToScheme/hrefToScheme.js" export const hrefToPathname = (href) => { return ressourceToPathname(hrefToRessource(href)) } const hrefToRessource = (href) => { const scheme = hrefToScheme(href) if (scheme === "file") { return href.slice("file://".length) } if (scheme === "https" || scheme === "http") { // remove origin const afterProtocol = href.slice(scheme.length + "://".length) const pathnameSlashIndex = afterProtocol.indexOf("/", "://".length) return afterProtocol.slice(pathnameSlashIndex) } return href.slice(scheme.length + 1) } const ressourceToPathname = (ressource) => { const searchSeparatorIndex = ressource.indexOf("?") return searchSeparatorIndex === -1 ? ressource : ressource.slice(0, searchSeparatorIndex) }