UNPKG

com-tools

Version:

com-tools 提供了一些通用的工具函数;

600 lines (599 loc) 16.5 kB
var w = Object.defineProperty; var P = (e, t, n) => t in e ? w(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n; var p = (e, t, n) => P(e, typeof t != "symbol" ? t + "" : t, n); function G(e, t) { return JSON.stringify(e) == JSON.stringify(t); } function A({ loopCall: e, complete: t, stepComplete: n, thisValue: r, total: i = Number.POSITIVE_INFINITY, step: o = 50, delay: s = 0 }) { let u = 0, f = 0, m = !1; function l(v) { v && (m = v, t && t.call(r, m, u, f, i)); } function a() { let v = u < i; return !v && t && t.call(r, !1, u - 1, f, i), v; } let y = null; function c() { f++; let v = Math.min(u + o, i); function h() { return u < v; } for (; h() && !m; ) l(e.call(r, u, f, i)), u++; n && l(n.call(r, u - 1, f, i)), a() && !m && (y = setTimeout(c, s)); } function d(v) { v || (v = !0), clearTimeout(y), l(v); } return a() && c(), d; } let x = { /** * multipleLoop(option)=> stopLoop() * 多次遍历、分批循环;可以把一个大遍历分成若干个小遍历来完成; * @param option : {loopCall,complete,stepComplete,thisValue,step,delay} 选项对象 * @property option.loopCall : (currentValue,index,stepCount,arr)=>stopInfo : any 必选;每次循环的回调函数;入参 currentValue : Item 当前index对应数组元素; 入参 index : number 表示当前循环的 index,从0开始;入参 stepCount : number 表示已经遍历的批数、周期数;入参 arr:Array 当前被循环的数组; 返回 stopInfo : any 停止循环并返回停止相关的信息; * @property option.stepComplete ? : (index,stepCount,arr)=>stopInfo : any 可选;每批循环完成时的回调函数;入参 index : number 表示当前循环的 index,从0开始;入参 stepCount : number 表示已经遍历的批数、周期数;入参 arr: Array 被循环的数组,即当前数组; 返回 stopInfo : any 停止循环并返回停止相关的信息; * @property option.complete ?: (stopInfo,index,stepCount,arr)=>Void 可选;循环结束时的回调函数;入参 stopInfo : any 停止循环遍历时停止信息;入参 index : number 表示最后一次循环的 index,如果值为-1 表示没有进行过循环值终止了;入参 stepCount : number 表示已经遍历的批数、周期数;入参 arr: Array 被循环的数组,即当前数组; * @property option.thisValue ? : any 可选;默认值:当前数组; loopCall、complete、stepComplete 回调函数的this的值; * @property option.step ? : number 可选; 默认值: 50 ; 设置每次遍历的循环次数; * @property option.delay ? : Timestamp 可选;默认值 :0 ; 设置再次遍历的间隔时间; * @returns stopLoop : (stopInfo)=>Void 停止循环的函数;调用该函数,会终止正在进行的循环; 入参 stopInfo : any 停止循环的相关信息 */ multipleLoop: { configurable: !0, writable: !0, enumerable: !1, value: function({ loopCall: e, complete: t, stepComplete: n, thisValue: r, step: i, delay: o }) { r && (r = this); let s = { loopCall: (u, f) => e.call(r, this[u], u, f, this), total: this.length, step: i, delay: o }; return t && (s.complete = (u, f, m) => t.call(r, u, f, m, this)), n && (s.stepComplete = (u, f) => n.call(r, u, f, this)), A(s); } } }; Object.defineProperties(Array.prototype, x); function U(e, t, n) { n == null && (n = e); let r = []; for (let o of e) r.push(o); return r.filter(function(o) { let s = this.indexOf(o); return t.call(n, o, s, e); }, r); } function j(e, t, n) { let { ready: r = t + "Ready", noEvent: i, event: o = t + "Change", newValueKey: s = "value", oldValueKey: u = "oldValue", getDefault: f } = n || {}, m = "_" + r, l = "_" + t; if (Object.defineProperty(e, r, { configurable: !0, enumerable: !0, get: function() { let c = this || window; if (!c[m]) { let d = c[l]; d ? c[m] = Promise.resolve(d) : c[m] = E(); } return c[m]; }, set: function(c) { let d = this || window; d[m] = c; } }), f) var a = function() { let c = this || window; return !c[l] && f && (c[t] = f.call(c, c)), c[l]; }; else a = function() { return (this || window)[l]; }; if (i) var y = function(c) { let d = this || window; if (c && c !== d[l]) { d[l] = c; let v = d[r]; v.resolve ? v.resolve(c) : d[r] = Promise.resolve(c); } }; else y = function(c) { let d = this || window; if (c && c !== d[l]) { let v = d[l]; d[l] = c; let h = d[r]; h.resolve ? h.resolve(c) : d[r] = Promise.resolve(c); let O = new Event(o, { bubbles: !0 }); O[s] = c, O[u] = v, window.dispatchEvent(O); } }; return Object.defineProperty(e, t, { configurable: !0, enumerable: !0, get: a, set: y }), e; } function z(e, t, n) { var r = t; return Array.isArray(t) && (r = t.reduce(function(i, o) { return i[o] = n, i; }, {})), Object.keys(r).forEach(function(i) { let o = r[i]; j(e, i, o); }), e; } function W(e, t, n, r) { let i = "_" + t; if (r) var o = n; else n ? o = function() { let s = this || window; return !s[i] && n && (s[t] = n.call(s, s)), s[i]; } : o = function() { return (this || window)[i]; }; Object.defineProperty(e, t, { configurable: !0, enumerable: !0, get: o }); } function K() { var e = /* @__PURE__ */ new Date(); return e.getTime().toString() + Math.random(); } function E(e, t) { let n = typeof e == "function"; n && t == null && (t = !0); var r = {}; function i() { u.resolve = void 0, u.reject = void 0, u.clearAdditions = void 0; } function o(f) { i(), r.resolve(f); } function s(f) { i(), r.reject(f); } var u = new Promise(function(f, m) { if (r.resolve = f, r.reject = m, n) if (t) setTimeout(e, 0, o, s); else return e(o, s); }); return u.clearAdditions = i, u.resolve = o, u.reject = s, u; } function S(e, t, n, r) { var { get: i = !0, set: o = !0, configurable: s = !0, enumerable: u = !0, getDefault: f, setDefault: m } = r || {}, l = { configurable: s, enumerable: u }; return i && (l.get = function() { var a = t[n]; return a === void 0 ? f : a; }), o && (l.set = function(a) { a = a === void 0 ? m : a, t[n] = a; }), Object.defineProperty(e, n, l), e; } function Z(e, t, n, r) { var i = n; return Array.isArray(n) && (i = n.reduce(function(o, s) { return o[s] = r, o; }, {})), Object.keys(i).forEach(function(o) { let s = i[o]; S(e, t, o, s); }), e; } function Y(e) { return new Function(`"use strict"; return (${e})`)(); } function H(e) { const t = F(e); return t.prototype = Object.create(e.prototype), t.prototype.constructor = t, t; } function F(e) { return L(e.toString(), e.name, !0); } const $ = /(^\s*(async\s+)?function\s*(\s|\*)\s*)[A-Za-z_$]+[\w$]*(\s*\()/, k = /^[A-Za-z_$]+[\w$]*$/; function L(e, t, n) { const r = $, i = k, o = r.test(e), s = t && i.test(t); if (o && s && !n && (e = e.replace(r, `$1${t}$4`)), !o && s) var u = `"use strict"; var ${t} = ${e} ; return ${t}`; else u = `"use strict"; return (${e})`; return new Function(u)(); } function Q(e) { return function() { return new.target ? new e(...arguments) : e.apply(this, arguments); }; } function X(e, t) { return e > 0 ? new Promise((n) => { setTimeout(n, e, t); }) : Promise.resolve(t); } function q(e, t, n) { if (n) for (const r in t) e[r] == null && (e[r] = t[r]); else for (const r in t) e[r] === void 0 && (e[r] = t[r]); return e; } class V { constructor() { //决议状态,表示是否决议 p(this, "_decided", !1); //存储监听器的数组 p(this, "listeners", []); //决议的值 p(this, "_value"); } get decided() { return this._decided; } get value() { return this._value; } set value(t) { this._value = t, this.resolve(); } /** * 执行回调,并且为了提高性能,会让决议之后的 then 方法直接执行回调 */ resolve() { this._decided = !0; const t = this.value; for (const n of this.listeners) n(t); this.listeners = [], this.then = function(n) { n(this.value); }; } /** * 监听决议,当被决议后,会自动回调 cb,并回传最新的 this.value * @param cb */ then(t) { this.listeners.push(t); } /** * 创建一个直接决议的 Decide,并且决议的值是 value * @param value */ static resolve(t) { const n = new this(); return n.value = t, n; } } function I(e, t) { if (t.length === 0) return e; for (let n = 0; n < t.length; n++) { if (e == null) return; e = e[t[n]]; } return e; } function g(e, t, n) { t.length === 0 && Object.assign(e, n); const r = t.length - 1; for (let i = 0; i < r; i++) { const o = t[i]; e[o] == null && (e[o] = typeof t[i + 1] == "number" ? [] : {}), e = e[o]; } e[t[r]] = n; } function M(e, t, n) { const r = typeof n == "function" ? n : () => n; let i = I(e, t); return i === void 0 && (i = r(), g(e, t, i)), i; } function ee(e, t, n) { const r = typeof n == "function" ? n : () => n; let i = I(e, t); return i === void 0 && (i = r(), i instanceof Promise ? i.then((o) => g(e, t, o)) : g(e, t, i)), i; } function T(e, t) { if (t.length === 0) return !0; const n = t.length - 1; for (let r = 0; r < n; r++) { if (e == null) return !1; e = e[t[r]]; } return e == null ? !1 : delete e[t[n]]; } function te(e) { return Object.keys(e).forEach((t) => e[t] === void 0 && delete e[t]), e; } function ne(e) { return Object.keys(e).forEach((t) => e[t] == null && delete e[t]), e; } function re(e, t) { const n = {}; for (const r of t) n[r] = e[r]; return n; } function ie(e, t) { for (const n of t) delete e[n]; return e; } function oe(e, t) { const n = {}; for (const r of t) { const i = I(e, r); g(n, r, i); } return n; } function se(e, t) { for (const n of t) T(e, n); return e; } function ue(e, t, n) { t ?? (t = "/"), n ?? (n = "-"); for (let r = e.length - 1; r >= 0 && e[r] == null; r--) e.length = r; return e.map((r) => r ?? n).join(t); } function ce(e, t, n = "/") { return e.split(n, t + 1)[t]; } function fe(e, t, n, r = "/") { const i = e.split(r); return i[t] = n, i.join(r); } function le(e, t, n = "/") { const r = []; for (const i of e) { const o = i.split(n, t + 1)[t]; o && r.push(o); } return r; } function de(e, t) { return t.some((n) => e.includes(n)); } function me(e, t) { return t.some((n) => n.test(e)); } function ve(e, t) { return t.some(function(n) { return typeof n == "string" ? e.includes(n) : n.test(e); }); } function ae(e, t, n) { if (t === n) return e; const [r] = e.splice(t, 1); return e.splice(n, 0, r), e; } function ye(e, t, n) { const r = e[t], i = e.filter((o, s) => s !== t); return i.splice(n, 0, r), i; } function he(e, t, n) { const r = t.map(function(i, o) { return { item: e[i], to: n[o] }; }); for (const { item: i } of r) { const o = e.indexOf(i); e.splice(o, 1); } for (const { item: i, to: o } of r) e.splice(o, 0, i); return e; } function pe(e, t, n) { const r = t.map((o) => e[o]), i = e.filter((o, s) => !t.includes(s)); for (let o = 0; o < t.length; o++) i.splice(n[o], 0, r[o]); return i; } function ge(e, t) { const n = "/"; let r = typeof t == "string" ? t.length : t ?? 0; const i = Math.min(r, 1), o = [n, ...e, n], s = JSON.stringify(o, null, t), u = s.indexOf(n) + 3 + i, f = s.lastIndexOf(n) - 1 - r; return s.slice(u, f); } function be(e, t, n) { let r = JSON.stringify(e, null, t) + ","; return t && (n ?? (n = ` `)), n && (r += n), r; } function D(e, t, n, r) { r == null && (r = e); let i = e.filter(function(o, s) { return t.includes(s); }); return i.forEach((o) => { let s = e.indexOf(o); n.call(r, o, s, e); }), i; } function _(e, t, n, r) { r == null && (r = e); let i = [], o = t.map((s) => (i.push(s), e.indexOf(s))); return i.forEach((s) => { let u = e.indexOf(s); n.call(r, s, u, e); }), o; } function Oe(e, t, n) { return n == null && (n = e), e.slice().filter((i) => { const o = e.indexOf(i); return t.call(n, i, o, e); }); } function Ie(e, t, n, r = 1) { let i = e.splice(t, r); return e.splice(n, 0, ...i), i; } function _e(e, t, ...n) { let r = C(e, ...n); return e.splice(t, 0, ...r), r; } function Ne(e, t, ...n) { let r = N(e, ...n); return e.splice(t, 0, ...n), r; } function we(e, t, n, r) { let i = J(e, n, r); return e.splice(t, 0, ...i), i; } function R(e, t, n = 0, r) { r || (r = function(o, s) { return o === s; }); let i = e.filter((o) => r.call(e, o, t)); return N(e, ...i), e.splice(n, 0, t), i; } function Pe(e, t, n, r) { return t.reduceRight((i, o) => { let s = R(e, o, n, r); return i.unshift(...s), i; }, []); } function Ae(e, t, n) { n == null && (n = t); let r = n + 1 - t; return e.splice(t, r); } function C(e, ...t) { return D(e, t, function(n, r, i) { i.splice(r, 1); }, e); } function N(e, ...t) { return _(e, t, function(n, r, i) { i.splice(r, 1); }, e); } function J(e, t, n) { n === void 0 && (n = e); let r = e.filter((i, o, s) => t.call(n, i, o, s)); return _(e, r, function(i, o, s) { s.splice(o, 1); }, e), r; } function xe(e, t, n) { return n == null && (n = e), e.reduce(function(r, i, o, s) { return t.call(n, i, o, s) && r.push(o), r; }, []); } function je(e, t, n) { return e.length !== t.length ? !1 : (n || (n = function(r, i) { return r === i; }), e.every((r, i) => n(r, t[i]))); } function Ee(e, t) { const n = { members: [] }, r = e.length; for (let i = 0; i < r; i++) { const o = e[i], s = t(o, i, e), u = M(n, s, () => ({ members: [] })); (u.members ?? (u.members = [])).push(o); } return n; } function Se(e) { return Object.keys(e).filter((n) => b(e[n])); } function Fe(e) { return Object.values(e).filter((n) => b(n)); } function $e(e) { return Object.entries(e).filter(([n, r]) => b(r)); } function b(e) { return e && !Array.isArray(e) && typeof e == "object"; } function ke(e) { return Object.values(e).some((n) => b(n)); } function Le(e) { const t = []; let n = [e]; for (let r = 0; r < n.length; r++) { const i = n[r], { members: o, ...s } = i; o && t.push(...o), n.push(...Object.values(s)); } return { members: t, nodes: n }; } export { V as Decide, Ae as array_delete, C as array_deleteIndexs, N as array_deleteItems, J as array_deleteUseTest, xe as array_filterIndexs, R as array_insertItem, Pe as array_insertItemList, je as array_isEqual, Ie as array_move, Ne as array_moveItemsTo, _e as array_moveTo, we as array_moveToUseTest, Oe as array_safelyFilter, D as array_safelyOperateIndexs, _ as array_safelyOperateItems, q as assignDefaultValues, Y as betterEval, H as copyConstructor, F as copyFunction, E as createControllablePromise, L as createFunctionBy, Q as createSingleton, K as createUniqueIdentifier, z as defineListenableProperties, j as defineListenableProperty, W as defineListenablePropertyGetter, Z as defineProxyProperties, S as defineProxyProperty, T as deleteMember, Le as flatGroupTree, I as getMember, ee as getMemberWithAsyncDefault, M as getMemberWithDefault, ce as getPathPart, $e as getSubGroupEntries, Se as getSubGroupNames, Fe as getSubGroupNodes, le as getValidPathParts, Ee as groupTreeBy, ke as hasSubGroups, ve as includesPatterns, me as includesRegExps, de as includesStrings, G as isEqualByJSON, b as isGroupTree, ae as moveItemInArray, ye as moveItemInNewArray, he as moveItemsInArray, pe as moveItemsInNewArray, A as multipleLoop, ie as omitMembers, se as omitMembersByPath, ue as pathString, re as pickMembers, oe as pickMembersByPath, ne as removeNull, te as removeUndefined, fe as replacePathPart, U as safelyIterate, g as setMember, ge as toJSONArrayFragment, be as toJSONItemFragment, X as waitWhile };