UNPKG

com-tools

Version:

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

348 lines (347 loc) 10.6 kB
var w = Object.defineProperty; var N = (e, t, n) => t in e ? w(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n; var b = (e, t, n) => N(e, typeof t != "symbol" ? t + "" : t, n); function R(e, t) { return JSON.stringify(e) == JSON.stringify(t); } function $({ loopCall: e, complete: t, stepComplete: n, thisValue: i, total: r = Number.POSITIVE_INFINITY, step: u = 50, delay: f = 0 }) { let s = 0, c = 0, a = !1; function l(v) { v && (a = v, t && t.call(i, a, s, c, r)); } function h() { let v = s < r; return !v && t && t.call(i, !1, s - 1, c, r), v; } let y = null; function o() { c++; let v = Math.min(s + u, r); function m() { return s < v; } for (; m() && !a; ) l(e.call(i, s, c, r)), s++; n && l(n.call(i, s - 1, c, r)), h() && !a && (y = setTimeout(o, f)); } function d(v) { v || (v = !0), clearTimeout(y), l(v); } return h() && o(), d; } let E = { /** * 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: i, step: r, delay: u }) { i && (i = this); let f = { loopCall: (s, c) => e.call(i, this[s], s, c, this), total: this.length, step: r, delay: u }; return t && (f.complete = (s, c, a) => t.call(i, s, c, a, this)), n && (f.stepComplete = (s, c) => n.call(i, s, c, this)), $(f); } } }; Object.defineProperties(Array.prototype, E); function M(e, t, n) { n == null && (n = e); let i = []; for (let u of e) i.push(u); return i.filter(function(u) { let f = this.indexOf(u); return t.call(n, u, f, e); }, i); } function P(e, t, n) { let { ready: i = t + "Ready", noEvent: r, event: u = t + "Change", newValueKey: f = "value", oldValueKey: s = "oldValue", getDefault: c } = n || {}, a = "_" + i, l = "_" + t; if (Object.defineProperty(e, i, { configurable: !0, enumerable: !0, get: function() { let o = this || window; if (!o[a]) { let d = o[l]; d ? o[a] = Promise.resolve(d) : o[a] = A(); } return o[a]; }, set: function(o) { let d = this || window; d[a] = o; } }), c) var h = function() { let o = this || window; return !o[l] && c && (o[t] = c.call(o, o)), o[l]; }; else h = function() { return (this || window)[l]; }; if (r) var y = function(o) { let d = this || window; if (o && o !== d[l]) { d[l] = o; let v = d[i]; v.resolve ? v.resolve(o) : d[i] = Promise.resolve(o); } }; else y = function(o) { let d = this || window; if (o && o !== d[l]) { let v = d[l]; d[l] = o; let m = d[i]; m.resolve ? m.resolve(o) : d[i] = Promise.resolve(o); let g = new Event(u, { bubbles: !0 }); g[f] = o, g[s] = v, window.dispatchEvent(g); } }; return Object.defineProperty(e, t, { configurable: !0, enumerable: !0, get: h, set: y }), e; } function S(e, t, n) { var i = t; return Array.isArray(t) && (i = t.reduce(function(r, u) { return r[u] = n, r; }, {})), Object.keys(i).forEach(function(r) { let u = i[r]; P(e, r, u); }), e; } function T(e, t, n, i) { let r = "_" + t; if (i) var u = n; else n ? u = function() { let f = this || window; return !f[r] && n && (f[t] = n.call(f, f)), f[r]; } : u = function() { return (this || window)[r]; }; Object.defineProperty(e, t, { configurable: !0, enumerable: !0, get: u }); } function x() { var e = /* @__PURE__ */ new Date(); return e.getTime().toString() + Math.random(); } function A(e, t) { let n = typeof e == "function"; n && t == null && (t = !0); var i = {}; function r() { s.resolve = void 0, s.reject = void 0, s.clearAdditions = void 0; } function u(c) { r(), i.resolve(c); } function f(c) { r(), i.reject(c); } var s = new Promise(function(c, a) { if (i.resolve = c, i.reject = a, n) if (t) setTimeout(e, 0, u, f); else return e(u, f); }); return s.clearAdditions = r, s.resolve = u, s.reject = f, s; } function j(e, t, n, i) { var { get: r = !0, set: u = !0, configurable: f = !0, enumerable: s = !0, getDefault: c, setDefault: a } = i || {}, l = { configurable: f, enumerable: s }; return r && (l.get = function() { var h = t[n]; return h === void 0 ? c : h; }), u && (l.set = function(h) { h = h === void 0 ? a : h, t[n] = h; }), Object.defineProperty(e, n, l), e; } function C(e, t, n, i) { var r = n; return Array.isArray(n) && (r = n.reduce(function(u, f) { return u[f] = i, u; }, {})), Object.keys(r).forEach(function(u) { let f = r[u]; j(e, t, u, f); }), e; } function J(e) { return new Function(`"use strict"; return (${e})`)(); } function k(e) { const t = F(e); return t.prototype = Object.create(e.prototype), t.prototype.constructor = t, t; } function F(e) { return p(e.toString(), e.name, !0); } const I = /(^\s*(async\s+)?function\s*(\s|\*)\s*)[A-Za-z_$]+[\w$]*(\s*\()/, L = /^[A-Za-z_$]+[\w$]*$/; function p(e, t, n) { const i = I, r = L, u = i.test(e), f = t && r.test(t); if (u && f && !n && (e = e.replace(i, `$1${t}$4`)), !u && f) var s = `"use strict"; var ${t} = ${e} ; return ${t}`; else s = `"use strict"; return (${e})`; return new Function(s)(); } function q(e) { return function() { return new.target ? new e(...arguments) : e.apply(this, arguments); }; } class z { constructor() { //决议状态,表示是否决议 b(this, "_decided", !1); //存储监听器的数组 b(this, "listeners", []); //决议的值 b(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 _(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 O(e, t, n) { t.length === 0 && Object.assign(e, n); const i = t.length - 1; for (let r = 0; r < i; r++) { const u = t[r]; e[u] == null && (e[u] = typeof t[r + 1] == "number" ? [] : {}), e = e[u]; } e[t[i]] = n; } function K(e, t, n) { const i = typeof n == "function" ? n : () => n; let r = _(e, t); return r === void 0 && (r = i(), O(e, t, r)), r; } function U(e, t, n) { const i = typeof n == "function" ? n : () => n; let r = _(e, t); return r === void 0 && (r = i(), r instanceof Promise ? r.then((u) => O(e, t, u)) : O(e, t, r)), r; } function W(e, t) { if (t.length === 0) return !0; const n = t.length - 1; for (let i = 0; i < n; i++) { if (e == null) return !1; e = e[t[i]]; } return e == null ? !1 : delete e[t[n]]; } function Z(e) { return Object.keys(e).forEach((t) => e[t] === void 0 && delete e[t]), e; } function B(e) { return Object.keys(e).forEach((t) => e[t] == null && delete e[t]), e; } function Y(e, t) { return t.some((n) => e.includes(n)); } function H(e, t) { return t.some((n) => n.test(e)); } function Q(e, t) { return t.some(function(n) { return typeof n == "string" ? e.includes(n) : n.test(e); }); } export { z as Decide, J as betterEval, k as copyConstructor, F as copyFunction, A as createControllablePromise, p as createFunctionBy, q as createSingleton, x as createUniqueIdentifier, S as defineListenableProperties, P as defineListenableProperty, T as defineListenablePropertyGetter, C as defineProxyProperties, j as defineProxyProperty, W as deleteMember, _ as getMember, U as getMemberWithAsyncDefault, K as getMemberWithDefault, Q as includesPatterns, H as includesRegExps, Y as includesStrings, R as isEqualByJSON, $ as multipleLoop, B as removeNull, Z as removeUndefined, M as safelyIterate, O as setMember };