@hopeio/utils
Version:
javascript utils
232 lines (231 loc) • 6.44 kB
JavaScript
var f = Object.defineProperty;
var p = (n, e, t) => e in n ? f(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
var h = (n, e, t) => p(n, typeof e != "symbol" ? e + "" : e, t);
import { g } from "./index-D5v913EJ.js";
import d from "axios";
function T(n, e) {
n = n + "=";
const r = decodeURIComponent(e).split(";");
for (let s = 0; s < r.length; s++) {
let i = r[s];
for (; i.charAt(0) === ""; )
i = i.substring(1);
if (i.indexOf(n) === 0)
return i.substring(n.length, i.length);
}
return "";
}
async function L(n) {
try {
return [null, await n()];
} catch (e) {
return [e, null];
}
}
function O(n) {
const e = new Array(2);
return n().catch((t) => {
e[0] = t;
}).then((t) => {
e[1] = t;
}), e;
}
function M(n) {
let e, t;
return n().catch((r) => e = r).then((r) => t = r), [e, t];
}
function U(n = "", e = {}) {
return fetch(n, {
method: "POST",
// *GET, POST, PUT, DELETE, etc.
mode: "cors",
// no-cors, cors, *same-origin
cache: "no-cache",
// *default, no-cache, reload, force-cache, only-if-cached
headers: {
"Content-Type": "application/json; charset=utf-8"
},
redirect: "follow",
// manual, *follow, error
referrer: "no-referrer",
// no-referrer, *client
body: JSON.stringify(e)
// body data type must match "Content-Type" header
}).then((t) => t.json());
}
const P = () => typeof window < "u" ? window : g;
function x(n) {
return Object.entries(n).map(([e, t]) => Array.isArray(t) ? t.map((r, s) => encodeURIComponent(e) + "[" + s + "]=" + encodeURIComponent(r)).join("&") : encodeURIComponent(e) + "=" + encodeURIComponent(t)).join("&");
}
function R(n, e) {
return n.reduce((t, r) => {
const s = e(r);
return t.has(s) ? t.get(s).push(r) : t.set(s, [r]), t;
}, /* @__PURE__ */ new Map());
}
function _(n, e) {
return n.reduce((t, r) => {
const s = e(r);
return t.has(s) ? t.get(s).push(r) : t.set(s, [r]), t;
}, /* @__PURE__ */ new Map());
}
const y = (n) => {
if (!Array.isArray(n))
return console.warn("tree must be an array"), [];
if (!n || n.length === 0) return [];
const e = [];
for (const t of n)
t.children && t.children.length > 0 && y(t.children), e.push(t.uniqueId);
return e;
}, m = (n, e = []) => {
if (!Array.isArray(n))
return console.warn("menuTree must be an array"), [];
if (!n || n.length === 0) return [];
for (const [t, r] of n.entries())
r.children && r.children.length === 1 && delete r.children, r.id = t, r.parentId = e.length ? e[e.length - 1] : null, r.pathList = [...e, r.id], r.uniqueId = r.pathList.length > 1 ? r.pathList.join("-") : r.pathList[0], r.children && r.children.length > 0 && m(r.children, r.pathList);
return n;
}, b = (n, e = []) => {
if (!Array.isArray(n))
return console.warn("tree must be an array"), [];
if (!n || n.length === 0) return [];
for (const [t, r] of n.entries())
r.id = t, r.parentId = e.length ? e[e.length - 1] : null, r.pathList = [...e, r.id], r.children && r.children.length > 0 && b(r.children, r.pathList);
return n;
}, C = (n, e) => {
if (!Array.isArray(n))
return console.warn("menuTree must be an array"), [];
if (!n || n.length === 0) return [];
const t = n.find((s) => s.uniqueId === e);
if (t) return t;
const r = n.filter((s) => s.children).map((s) => s.children).flat(1);
return C(r, e);
}, w = (n, e, t) => {
if (!Array.isArray(n))
return console.warn("menuTree must be an array"), [];
if (!n || n.length === 0) return [];
for (const r of n) {
const s = r.children && r.children.length > 0;
r.uniqueId === e && Object.prototype.toString.call(t) === "[object Object]" && Object.assign(r, t), s && w(r.children, e, t);
}
return n;
}, k = (n, e, t, r) => {
if (!Array.isArray(n))
return console.warn("data must be an array"), [];
const s = {
id: e || "id",
parentId: t || "parentId",
childrenList: r || "children"
}, i = {}, l = {}, c = [];
for (const o of n) {
const a = o[s.parentId];
i[a] == null && (i[a] = []), l[o[s.id]] = o, i[a].push(o);
}
for (const o of n) {
const a = o[s.parentId];
l[a] == null && c.push(o);
}
for (const o of c)
u(o);
function u(o) {
if (i[o[s.id]] !== null && (o[s.childrenList] = i[o[s.id]]), o[s.childrenList])
for (const a of o[s.childrenList])
u(a);
}
return c;
};
function H(n, e) {
for (const t of e)
n.set(t.id, t);
return n;
}
class S {
constructor() {
h(this, "_map", /* @__PURE__ */ new Map());
}
append(e) {
for (const t of e)
this._map.set(t.id, t);
}
get(e) {
return this._map.get(e);
}
set(e, t) {
this._map.set(e, t);
}
has(e) {
return this._map.has(e);
}
}
class I {
constructor(e) {
/** 保存当前`Axios`实例对象 */
h(this, "defaults", d.defaults);
h(this, "instance", d);
h(this, "interceptors", {
request: d.interceptors.request,
response: d.interceptors.response
});
e && (this.instance = d.create(e), this.defaults = this.instance.defaults, this.interceptors = {
request: this.instance.interceptors.request,
response: this.instance.interceptors.response
});
}
/** 通用请求工具函数 */
request(e, t, r) {
const s = {
method: e,
url: t,
...r
};
return new Promise((i, l) => {
this.instance.request(s).then((c) => {
i(c.data);
}).catch((c) => {
l(c);
});
});
}
/** 单独抽离的`post`工具函数 */
post(e, t) {
return this.request("post", e, t);
}
/** 单独抽离的`get`工具函数 */
get(e, t) {
return this.request("get", e, t);
}
put(e, t) {
return this.request("put", e, t);
}
delete(e, t) {
return this.request("delete", e, t);
}
}
const B = new I();
function N(n) {
return fetch(n, {
method: "HEAD",
mode: "cors"
}).then((e) => (console.log(e), !0)).catch((e) => (console.log(e), !1));
}
export {
I as Http,
S as ObjMap,
w as appendFieldByUniqueId,
H as appendObjMap,
_ as arrayToMap,
R as arrayToMapArrValue,
b as buildHierarchyTree,
m as deleteChildren,
M as errCatch,
L as errCatch1,
O as errCatch2,
y as extractPathList,
T as getCookie,
P as getGlobal,
C as getNodeByUniqueId,
k as handleTree,
B as http,
U as post,
N as supportIPv6,
x as toUrlParams
};