UNPKG

@opensig/opendesign

Version:

262 lines (261 loc) 6.36 kB
"use strict"; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const is = require("./is.js"); const log$1 = require("./log.js"); const log = new log$1.Log("helper"); function debounce(fn, wait = 0, leading = true, trailing = false) { let handler = 0; let hasTrailingCall = false; return (...args) => { if (!is.isClient) { log.error("[debounce] 此函数应仅在客户端环境使用。"); } if (leading) { if (handler === 0) { fn(...args); } else { hasTrailingCall = true; } } clearTimeout(handler); handler = window.setTimeout(() => { if (!leading || hasTrailingCall && trailing) { fn(...args); } handler = 0; hasTrailingCall = false; }, wait); }; } function debounceRAF(fn) { let handle = 0; const rlt = (...args) => { if (!is.isClient) { log.error("[debounceRAF] 此函数依赖 requestAnimationFrame,仅可在客户端环境使用。"); } if (handle) { cancelAnimationFrame(handle); } handle = requestAnimationFrame(() => { fn(...args); handle = 0; }); }; rlt.cancel = () => { cancelAnimationFrame(handle); handle = 0; }; return rlt; } function throttleRAF(fn) { let handle = 0; const rlt = (...args) => { if (handle) { return; } if (!is.isClient) { log.error("[throttleRAF] 此函数依赖 requestAnimationFrame,仅可在客户端环境使用。"); } handle = requestAnimationFrame(() => { fn(...args); handle = 0; }); }; rlt.cancel = () => { cancelAnimationFrame(handle); handle = 0; }; return rlt; } class ColorPool { constructor(pool) { __publicField(this, "pool"); __publicField(this, "tmpPool"); this.pool = pool; this.tmpPool = [...pool]; } /** * 返回指定位置颜色,或者从颜色池随机返回一个颜色 * @param index * @returns */ pick(index) { if (index !== void 0) { return this.pool[index % this.pool.length]; } const { length } = this.tmpPool; if (length === 0) { this.tmpPool = [...this.pool]; } const idx = Math.floor(Math.random() * length); const color = this.tmpPool[idx]; this.tmpPool.splice(idx, 1); return color; } } function uniqueId(prefix = "", length = 8) { const gen = (len) => { if (len <= 11) { return Math.random().toString(36).slice(2, 2 + len).padEnd(len, "0"); } else { return gen(11) + gen(len - 11); } }; return prefix ? `${prefix}-${gen(length)}` : gen(length); } function chunk(arr = [], size = 1) { return Array.from( { length: Math.ceil(arr.length / size) }, (_v, i) => arr.slice(i * size, i * size + size) ); } async function asyncSome(array, judgeFn) { for (const iterator of array) { try { if (await judgeFn(iterator)) { return true; } } catch { return false; } } return false; } function getValueByPath(obj, path) { if (!obj || !path) { return; } const keys = path.split("."); if (keys.length === 0) { return; } let temp = obj; for (let i = 0; i < keys.length; i++) { if (!is.isObject(temp)) { return; } temp = temp[keys[i]]; if (i === keys.length - 1) { return temp; } } } function setValueByPath(obj, path, value) { if (!obj || !path) { return; } const keys = path.split("."); if (keys.length === 0) { return; } let temp = obj; for (let i = 0; i < keys.length; i++) { if (!is.isObject(temp)) { throw new TypeError(`Cannot set properties of non-object (setting '${keys[i]}')!`); } const k = keys[i]; if (i === keys.length - 1) { temp[k] = value; } else { if (is.isUndefined(temp[k])) { temp[k] = Number(keys[i + 1]) ? [] : {}; } temp = temp[k]; } } } function moveToFirst(arr, item) { const idx = arr.indexOf(item); if (idx > 0) { const tmp = [...arr]; tmp.splice(idx, 1); tmp.unshift(item); return tmp; } return arr; } function formateToString(val) { if (is.isUndefined(val) || is.isNull(val) || typeof val === "number" && isNaN(val) || is.isPlainObject(val)) { return ""; } return String(val); } function requestImage(src) { return new Promise((resolve, reject) => { const onImgLoaded = () => { resolve(src); }; const onImgError = (e) => { reject(e); }; const img = new Image(); img.onload = onImgLoaded; img.onerror = onImgError; img.src = src; }); } function pick(source, keys) { const result = {}; keys.forEach((key) => { if (key in source) { result[key] = source[key]; } }); return result; } function performTask(tasks, sheduler) { let runingIndex = 0; function _runTask() { sheduler((toContinue) => { while (runingIndex < tasks.length && toContinue(runingIndex)) { tasks[runingIndex++](); } if (runingIndex < tasks.length) { _runTask(); } }); } _runTask(); } function idlePerformTask(tasks) { const sheduler = (runChunk) => { requestIdleCallback((idle) => { runChunk(() => idle.timeRemaining() > 0); }); }; performTask(tasks, sheduler); } function promiseWithResolvers() { let resolve; let reject; const promise = new Promise((res, rej) => { resolve = res; reject = rej; }); return { promise, resolve, reject }; } exports.ColorPool = ColorPool; exports.asyncSome = asyncSome; exports.chunk = chunk; exports.debounce = debounce; exports.debounceRAF = debounceRAF; exports.formateToString = formateToString; exports.getValueByPath = getValueByPath; exports.idlePerformTask = idlePerformTask; exports.moveToFirst = moveToFirst; exports.performTask = performTask; exports.pick = pick; exports.promiseWithResolvers = promiseWithResolvers; exports.requestImage = requestImage; exports.setValueByPath = setValueByPath; exports.throttleRAF = throttleRAF; exports.uniqueId = uniqueId;