@nauverse/make-url
Advanced tools
Comparing version 1.10.8 to 1.10.13
@@ -1,9 +0,9 @@ | ||
function g(a) { | ||
function p(a) { | ||
try { | ||
const r = new URL(a); | ||
if (r.hostname.split(".").length < 2) | ||
const n = new URL(a); | ||
if (n.hostname.split(".").length < 2) | ||
throw null; | ||
return { | ||
hasDomain: !0, | ||
domain: r.hostname | ||
domain: n.hostname | ||
}; | ||
@@ -17,8 +17,8 @@ } catch { | ||
} | ||
function S(a) { | ||
const r = a.trim().toLowerCase(); | ||
if (r.startsWith("//")) { | ||
const t = `https:${r}`; | ||
function D(a) { | ||
const n = a.trim().toLowerCase(); | ||
if (n.startsWith("//")) { | ||
const e = `https:${n}`; | ||
return { | ||
hasProtocol: g(t).hasDomain, | ||
hasProtocol: p(e).hasDomain, | ||
// Even if it contains a valid relative protocol string, we can not count it as valid since it does not contain a valid domain | ||
@@ -29,81 +29,82 @@ protocol: "relative" | ||
} | ||
const i = /^(http|https):\/\//.test(r); | ||
const i = /^(http|https):\/\//.test(n); | ||
return { | ||
hasProtocol: i, | ||
protocol: i ? r.split("://")[0] : "none" | ||
protocol: i ? n.split("://")[0] : "none" | ||
}; | ||
} | ||
function D(a, r) { | ||
return a.slice(r ? 1 : 0).filter((i) => i.trim() !== "").map((i, t) => t === 0 || i.startsWith(".") ? i : `/${i}`).join(""); | ||
function R(a, n) { | ||
return a.slice(n ? 1 : 0).filter((i) => i.trim() !== "").map((i, e) => e === 0 || i.startsWith(".") ? i : `/${i}`).join(""); | ||
} | ||
function R(a, r) { | ||
let i = 0, t = ""; | ||
const e = []; | ||
return a.forEach((o) => { | ||
if (o === "" && e.length > 0) { | ||
e.push(o); | ||
return; | ||
function U(a, n) { | ||
let i = 0, e = ""; | ||
const t = []; | ||
for (const o of a) { | ||
if (o === "" && t.length > 0) { | ||
t.push(o); | ||
continue; | ||
} | ||
if (i + o.length <= r) | ||
t += o, i += o.length; | ||
else if (i < r) { | ||
const n = r - i; | ||
t += o.slice(0, n), n < o.length && (e.push(t), t = "", e.push(o.slice(n))), i += n; | ||
if (i + o.length <= n) | ||
e += o, i += o.length; | ||
else if (i < n) { | ||
const r = n - i; | ||
e += o.slice(0, r), r < o.length && (t.push(e), e = "", t.push(o.slice(r))), i += r; | ||
} else | ||
t !== "" && (e.push(t), t = ""), e.push(o); | ||
}), t !== "" && (e.push(t), t = ""), e; | ||
e !== "" && (t.push(e), e = ""), t.push(o); | ||
} | ||
return e !== "" && (t.push(e), e = ""), t; | ||
} | ||
function U(a, r) { | ||
const i = D(a, r), t = g(`https://${i}`); | ||
if (!t.hasDomain) | ||
return a; | ||
const e = i.indexOf(t.domain) + t.domain.length, o = a.slice(r ? 1 : 0); | ||
let n = "", c = 0, f = []; | ||
return o.forEach((h) => { | ||
if (h === "") { | ||
c >= e && (f.length > 0 || (f.push(n), n = ""), f.push(h)); | ||
return; | ||
function L(a, n) { | ||
const i = R(a, n), e = p(`https://${i}`); | ||
if (!e.hasDomain) return a; | ||
const t = i.indexOf(e.domain) + e.domain.length, o = a.slice(n ? 1 : 0); | ||
let r = "", c = 0, f = []; | ||
for (const u of o) { | ||
if (u === "") { | ||
c >= t && (f.length > 0 || (f.push(r), r = ""), f.push(u)); | ||
continue; | ||
} | ||
if (c + h.length <= e) | ||
n += h, c += h.length; | ||
else if (c < e) { | ||
const s = e - c; | ||
n += h.slice(0, s), s < h.length && (f.push(n), n = "", f.push(h.slice(s))), c += s; | ||
if (c + u.length <= t) | ||
r += u, c += u.length; | ||
else if (c < t) { | ||
const s = t - c; | ||
r += u.slice(0, s), s < u.length && (f.push(r), r = "", f.push(u.slice(s))), c += s; | ||
} else | ||
n !== "" && (f.push(n), n = ""), f.push(h); | ||
}), n !== "" && (f.push(n), n = ""), r && a[0] && (f = [a[0], ...f]), f; | ||
r !== "" && (f.push(r), r = ""), f.push(u); | ||
} | ||
return r !== "" && (f.push(r), r = ""), n && a[0] && (f = [a[0], ...f]), f; | ||
} | ||
function L(a, r) { | ||
function w(a, n) { | ||
let i = [...a]; | ||
r.allowEmptyPathSegments || (i = i.filter( | ||
n.allowEmptyPathSegments || (i = i.filter( | ||
(c) => c.trim() !== "" | ||
)); | ||
const t = i.join(""), { hasProtocol: e, protocol: o } = S( | ||
t | ||
const e = i.join(""), { hasProtocol: t, protocol: o } = D( | ||
e | ||
); | ||
let n = i; | ||
if (e) { | ||
const c = t.indexOf( | ||
let r = i; | ||
if (t) { | ||
const c = e.indexOf( | ||
o === "relative" ? "//" : "://" | ||
) + (o === "relative" ? 2 : 3); | ||
n = R( | ||
r = U( | ||
i, | ||
c | ||
), n.length > 1 && (n[1] = (n[1] ?? "").replace(/^\/*/, "")); | ||
), r.length > 1 && (r[1] = (r[1] ?? "").replace(/^\/*/, "")); | ||
} | ||
return n = U(n, e), { | ||
array: n, | ||
hasProtocol: e, | ||
return r = L(r, t), { | ||
array: r, | ||
hasProtocol: t, | ||
protocol: o | ||
}; | ||
} | ||
function y(a) { | ||
const { array: r, hasProtocol: i } = a; | ||
function P(a) { | ||
const { array: n, hasProtocol: i } = a; | ||
if (i) { | ||
const t = r.length > 1 ? r.slice(1) : []; | ||
return `${r.length > 0 ? r[0] : ""}${t.join("/")}`; | ||
const e = n.length > 1 ? n.slice(1) : []; | ||
return `${n.length > 0 ? n[0] : ""}${e.join("/")}`; | ||
} | ||
return r.join("/"); | ||
return n.join("/"); | ||
} | ||
const E = { | ||
const $ = { | ||
forceProtocol: "auto", | ||
@@ -114,93 +115,97 @@ trailingSlash: "add", | ||
arraySerializer: "repeat" | ||
}, m = { | ||
}, d = { | ||
params: {}, | ||
hash: "", | ||
config: E | ||
config: $ | ||
}; | ||
let d = { | ||
...m.config | ||
let y = { | ||
...d.config | ||
}; | ||
function w() { | ||
return d; | ||
function v() { | ||
return y; | ||
} | ||
function $(a) { | ||
d = { | ||
...m.config, | ||
function F(a) { | ||
y = { | ||
...d.config, | ||
...a | ||
}; | ||
} | ||
function F(...a) { | ||
function E(...a) { | ||
if (a.length === 0 || a.length > 0 && typeof a[0] != "string") | ||
throw new Error("makeURL must receive at least one string item"); | ||
let r = { config: {} }; | ||
let n = { config: {} }; | ||
const i = []; | ||
a.forEach((s, l) => { | ||
a.forEach((s, h) => { | ||
if (typeof s == "string") | ||
i.push(s); | ||
else { | ||
if (l !== a.length - 1) | ||
if (h !== a.length - 1) | ||
throw new Error( | ||
"Params config object argument must be the last argument" | ||
); | ||
r = s; | ||
n = s; | ||
} | ||
}); | ||
const t = { | ||
...m, | ||
...r, | ||
const e = { | ||
...d, | ||
...n, | ||
config: { | ||
...d, | ||
...r == null ? void 0 : r.config | ||
...y, | ||
...n == null ? void 0 : n.config | ||
} | ||
}, e = L( | ||
}, t = w( | ||
i, | ||
t.config | ||
e.config | ||
); | ||
if (e.array = e.array.map( | ||
(s, l) => l === 0 && e.hasProtocol ? s : s.split("/").map((u) => encodeURIComponent(u).replace(/%3A/g, ":")).join("/") | ||
), !e.hasProtocol && t.config.forceProtocol !== "none") | ||
if (t.config.forceProtocol === "auto" || t.config.forceProtocol === "auto-insecure") { | ||
const s = t.config.forceProtocol === "auto"; | ||
g( | ||
`https://${y(e)}` | ||
).hasDomain && (e.array = [ | ||
if (t.array = t.array.map( | ||
(s, h) => h === 0 && t.hasProtocol ? s : s.split("/").map((l) => encodeURIComponent(l).replace(/%3A/g, ":")).join("/") | ||
), !t.hasProtocol && e.config.forceProtocol !== "none") | ||
if (e.config.forceProtocol === "auto" || e.config.forceProtocol === "auto-insecure") { | ||
const s = e.config.forceProtocol === "auto"; | ||
p( | ||
`https://${P(t)}` | ||
).hasDomain && (t.array = [ | ||
`${s ? "https" : "http"}://`, | ||
...e.array | ||
], e.hasProtocol = !0, e.protocol = s ? "https" : "http"); | ||
...t.array | ||
], t.hasProtocol = !0, t.protocol = s ? "https" : "http"); | ||
} else | ||
e.array = [ | ||
`${t.config.forceProtocol}://`, | ||
...e.array | ||
], e.hasProtocol = !0, e.protocol = t.config.forceProtocol; | ||
let o = y(e); | ||
if (t.config.trailingSlash === "add" && !o.endsWith("/") && (o += "/"), t.config.trailingSlash === "remove" && o.endsWith("/") && (o = o.slice(0, -1)), !t.config.allowEmptyPathSegments) { | ||
t.array = [ | ||
`${e.config.forceProtocol}://`, | ||
...t.array | ||
], t.hasProtocol = !0, t.protocol = e.config.forceProtocol; | ||
let o = P(t); | ||
if (e.config.trailingSlash === "add" && !o.endsWith("/") && (o += "/"), e.config.trailingSlash === "remove" && o.endsWith("/") && (o = o.slice(0, -1)), !e.config.allowEmptyPathSegments) { | ||
let s = ""; | ||
e.protocol === "relative" && (e.hasProtocol ? s = "//" : s = "/", o = o.slice(2)), o = `${s}${o.replace(new RegExp("(?<!:)\\/{2,}", "g"), "/")}`; | ||
t.protocol === "relative" && (t.hasProtocol ? s = "//" : s = "/", o = o.slice(2)), o = `${s}${o.replace(new RegExp("(?<!:)\\/{2,}", "g"), "/")}`; | ||
} | ||
const n = {}; | ||
Object.entries(t.params).forEach(([s, l]) => { | ||
let u = "", p = !1; | ||
typeof l == "string" ? u = l : typeof l == "number" ? u = l.toString() : Array.isArray(l) ? (p = !0, t.config.arraySerializer === "stringify" ? u = JSON.stringify(l) : t.config.arraySerializer === "repeat" ? u = l : u = l.join(",")) : typeof l == "object" ? u = JSON.stringify(l) : u = `${l}`, n[s] = { | ||
value: u, | ||
isArray: p | ||
const r = {}; | ||
for (const s of Object.entries(e.params)) { | ||
const [h, l] = s; | ||
let g = "", m = !1; | ||
typeof l == "string" ? g = l : typeof l == "number" ? g = l.toString() : Array.isArray(l) ? (m = !0, e.config.arraySerializer === "stringify" ? g = JSON.stringify(l) : e.config.arraySerializer === "repeat" ? g = l : g = l.join(",")) : typeof l == "object" ? g = JSON.stringify(l) : g = `${l}`, r[h] = { | ||
value: g, | ||
isArray: m | ||
}; | ||
}); | ||
} | ||
const c = new URLSearchParams(); | ||
Object.entries(n).forEach(([s, l]) => { | ||
for (const s of Object.entries(r)) { | ||
const [h, l] = s; | ||
if (l.isArray) { | ||
Array.isArray(l.value) ? l.value.forEach((A) => { | ||
c.append(s, A); | ||
}) : c.append(s, l.value); | ||
return; | ||
if (Array.isArray(l.value)) | ||
for (const S of l.value) | ||
c.append(h, S); | ||
else | ||
c.append(h, l.value); | ||
continue; | ||
} | ||
let u = !1; | ||
const p = new RegExp(`:${s}`, "g"), P = encodeURIComponent(l.value); | ||
o = o.replace(p, () => (u = !0, P)), u ? delete n[s] : c.append(s, l.value); | ||
}); | ||
let g = !1; | ||
const m = new RegExp(`:${h}`, "g"), A = encodeURIComponent(l.value); | ||
o = o.replace(m, () => (g = !0, A)), g ? delete r[h] : c.append(h, l.value); | ||
} | ||
const f = c.toString(); | ||
f && (o += `?${f}`); | ||
const h = t.hash.trim(); | ||
if (h !== "" && (o += `#${encodeURIComponent(h)}`), t.config.strict) | ||
const u = e.hash.trim(); | ||
if (u !== "" && (o += `#${encodeURIComponent(u)}`), e.config.strict) | ||
try { | ||
if (!g(o).hasDomain) | ||
if (!p(o).hasDomain) | ||
throw null; | ||
@@ -213,7 +218,7 @@ } catch { | ||
export { | ||
E as BASE_DEFAULT_MAKE_URL_CONFIG, | ||
w as getMakeURLDefaultConfig, | ||
F as makeURL, | ||
$ as setMakeURLDefaultConfig | ||
$ as BASE_DEFAULT_MAKE_URL_CONFIG, | ||
v as getMakeURLDefaultConfig, | ||
E as makeURL, | ||
F as setMakeURLDefaultConfig | ||
}; | ||
//# sourceMappingURL=make-url.js.map |
@@ -29,3 +29,3 @@ { | ||
], | ||
"version": "1.10.8", | ||
"version": "1.10.13", | ||
"type": "module", | ||
@@ -58,25 +58,18 @@ "main": "./dist/make-url.umd.cjs", | ||
"test:coverage": "vitest run --coverage", | ||
"lint": "eslint . --ext .ts --report-unused-disable-directives --max-warnings 0", | ||
"lint:fix": "eslint --fix . --ext .ts --report-unused-disable-directives --max-warnings 0", | ||
"lint": "biome check", | ||
"lint:fix": "biome check --write", | ||
"prepare": "husky" | ||
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "1.8.3", | ||
"@commitlint/cli": "19.3.0", | ||
"@commitlint/config-conventional": "19.2.2", | ||
"@types/eslint": "8.56.10", | ||
"@types/node": "20.12.12", | ||
"@typescript-eslint/eslint-plugin": "7.9.0", | ||
"@typescript-eslint/parser": "7.9.0", | ||
"@types/node": "20.14.9", | ||
"@vitest/coverage-v8": "1.6.0", | ||
"eslint": "8.57.0", | ||
"eslint-config-prettier": "9.1.0", | ||
"eslint-plugin-prettier": "5.1.3", | ||
"husky": "9.0.11", | ||
"prettier": "3.2.5", | ||
"typescript": "5.4.5", | ||
"vite": "5.2.11", | ||
"typescript": "5.5.2", | ||
"vite": "5.3.2", | ||
"vite-plugin-dts": "3.9.1", | ||
"vitest": "1.6.0" | ||
}, | ||
"peerDependencies": {} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
119860
10
296