UNPKG

@acrool/js-utils

Version:

Common javascript utils methods for project development

75 lines (74 loc) 2.15 kB
import { r as c } from "../regPattern-7f640e9d.js"; function u(e = {}) { const o = []; return Object.keys(e).forEach((n) => { const t = e[n]; typeof t < "u" && o.push(`${encodeURIComponent(n)}=${encodeURIComponent(t)}`); }), o.length > 0 ? `${o.join("&")}` : ""; } function s(e = "") { const o = decodeURIComponent(e).replace(/^.*\?/, "").split("&"), n = {}; if (o[0] !== "") return o.forEach((t) => { const r = t.split("="); r[1] && (n[r[0]] = r[1]); }), Object.keys(n).length > 0 ? n : void 0; } function p(e) { if (e) { const n = new RegExp(c.protocolDomain).exec(e); if (n !== null) return n[0]; } return ""; } function g(e) { const n = new RegExp(c.domain).exec(e); if (n && n[0]) { const t = n[0].split("."); if (t.length === 2) return n[0]; if (t.length === 3) return [t[1], t[2]].join("."); } return ""; } function a(e) { const n = new RegExp(c.domain).exec(e); if (n && n[0]) { const t = n[0].split("."); if (t.length === 3) return t[0]; } return ""; } function l(e) { const o = []; if (e.length === 0) return ""; if (typeof e[0] != "string") throw new TypeError("Url must be a string. Received " + e[0]); e[0].match(/^[^/:]+:\/*$/) && e.length > 1 && (e[0] = e.shift() + e[0]), e[0].match(/^file:\/\/\//) ? e[0] = e[0].replace(/^([^/:]+):\/*/, "$1:///") : e[0] = e[0].replace(/^([^/:]+):\/*/, "$1://"); for (let r = 0; r < e.length; r++) { let i = e[r]; if (typeof i != "string") throw new TypeError("Url must be a string. Received " + i); i !== "" && (r > 0 && (i = i.replace(/^[\/]+/, "")), r < e.length - 1 ? i = i.replace(/[\/]+$/, "") : i = i.replace(/[\/]+$/, "/"), o.push(i)); } let n = o.join("/"); n = n.replace(/\/(\?|&|#[^!])/g, "$1"); const t = n.split("?"); return n = t.shift() + (t.length > 0 ? "?" : "") + t.join("&"), n; } function h(...e) { const o = Array.from(Array.isArray(e[0]) ? e[0] : e); return l(o); } export { s as decodeQueryString, u as encodeQueryString, g as getMainDomain, p as getProtocolDomain, a as getSubDomain, h as urlJoin };