UNPKG

@opensig/opendesign

Version:

<p align="center"><img src="../docs/public/opendesign-logo-light.png"/></p> <h1 align="center">opendesign</h1> <p align="center">一个 Vue 3 组件库</p> <p align="center"><b>皮肤可定制,使用 TypeScript</b></p>

233 lines (232 loc) 5.16 kB
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); import { isObject, isUndefined, isNull, isPlainObject } from "./is.mjs"; function debounce(fn, wait = 0, runFirst = true) { let handler = 0; return (...args) => { if (runFirst) { if (handler === 0) { fn(...args); } } clearTimeout(handler); handler = window.setTimeout(() => { if (!runFirst) { fn(...args); } handler = 0; }, wait); }; } function debounceRAF(fn) { let handle = 0; const rlt = (...args) => { 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; } 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 (error) { 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 (!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 (!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 (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 (isUndefined(val) || isNull(val) || typeof val === "number" && isNaN(val) || 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); } export { ColorPool, asyncSome, chunk, debounce, debounceRAF, formateToString, getValueByPath, idlePerformTask, moveToFirst, performTask, pick, requestImage, setValueByPath, throttleRAF, uniqueId };