Skip to content

Instantly share code, notes, and snippets.

@jsg2021
Created September 6, 2022 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsg2021/c9f80309e3a55c1133be800dd96ee805 to your computer and use it in GitHub Desktop.
Save jsg2021/c9f80309e3a55c1133be800dd96ee805 to your computer and use it in GitHub Desktop.
url replacement
export const NULL_PROTO = "file:/";
const isDefault = (x, y) =>
!`${x}`.startsWith("file:") &&
(!y || y === NULL_PROTO || !`${y}`.startsWith("file:"));
const filterString = (str, defaulted) =>
defaulted ? str.replace(/^file:\/\//, "") : str;
class LooseURL extends URL {
static NULL_PROTO = NULL_PROTO;
constructor(uri, ref = NULL_PROTO) {
let defaulted = isDefault(uri, ref);
let parsed;
try {
parsed = new URL(uri, new URL(ref, NULL_PROTO));
} catch {
parsed = new URL("file://null");
defaulted = true;
}
super(parsed.toString());
this._defaulted = defaulted;
}
get protocol() {
const t = super.protocol;
return this._defaulted && t === "file:" ? null : t;
}
set protocol(scheme) {
if (!this.host) {
throw new Error("Setting protocol with no host... set host first.");
}
super.protocol = scheme;
}
get origin() {
const base = super.origin;
return this._defaulted && /^file:/.test(base) ? "null" : base;
}
toString() {
return filterString(super.toString(), this._defaulted);
}
}
export const parse = (uri, b) => new LooseURL(uri, b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment