@jsenv/href
Version:
url helpers not relying on URL constructor
20 lines (14 loc) • 526 B
JavaScript
import { hrefToScheme } from "../hrefToScheme/hrefToScheme.js"
export const hrefToOrigin = (href) => {
const scheme = hrefToScheme(href)
if (scheme === "file") {
return "file://"
}
if (scheme === "http" || scheme === "https") {
const secondProtocolSlashIndex = scheme.length + "://".length
const pathnameSlashIndex = href.indexOf("/", secondProtocolSlashIndex)
if (pathnameSlashIndex === -1) return href
return href.slice(0, pathnameSlashIndex)
}
return href.slice(0, scheme.length + 1)
}