UNPKG

@gitborlando/utils

Version:

JavaScript/TypeScript 实用工具集合

660 lines (647 loc) 18.6 kB
var __defProp = Object.defineProperty; var __typeError = (msg) => { throw TypeError(msg); }; 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); var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); // src/array.ts function firstOne(input) { if (Array.isArray(input)) return input[0]; return input.values().next().value; } function lastOne(input) { if (Array.isArray(input)) return input[input.length - 1]; const arr = [...input]; return arr[arr.length - 1]; } function flushFuncs(input) { input.forEach((callback) => callback()); Array.isArray(input) ? input.length = 0 : input.clear(); } function stableIndex(arr, index) { if (index === void 0) return arr.length; if (index < 0) return 0; if (index > arr.length) return arr.length; return index; } function loopFor(arr, callback) { for (let index = 0; index < arr.length; index++) { const left = index === 0 ? arr.length - 1 : index - 1; const right = index === arr.length - 1 ? 0 : index + 1; const res = callback(arr[index], arr[right], arr[left], index); if (res === "break") break; if (res === "continue") continue; } } function reverseFor(items, callback) { for (let i = items.length - 1; i >= 0; i--) callback(items[i], i); } function reverse(arr) { return arr.slice().reverse(); } function range(count) { return new Array(count).fill(0).map((_, i) => i); } // node_modules/.pnpm/@gitborlando+geo@1.0.1/node_modules/@gitborlando/geo/src/math.ts var { sqrt, abs, min, max, round, floor, ceil, random } = Math; // node_modules/.pnpm/@gitborlando+geo@1.0.1/node_modules/@gitborlando/geo/src/angle.ts var { PI, cos, sin, tan, acos, asin, atan, atan2 } = Math; // node_modules/.pnpm/@gitborlando+geo@1.0.1/node_modules/@gitborlando/geo/src/xy.ts function xy_(x = 0, y = 0) { return { x, y }; } function xy_plus(self, another) { return { x: self.x + another.x, y: self.y + another.y }; } function xy_minus(self, another) { return { x: self.x - another.x, y: self.y - another.y }; } // src/types.ts function noopFunc() { } function anyFunc(...args) { } var AnyObject = {}; // src/browser/drag.ts var DragUtil = class { constructor() { __publicField(this, "canMove", false); __publicField(this, "started", false); __publicField(this, "current", { x: 0, y: 0 }); __publicField(this, "start", { x: 0, y: 0 }); __publicField(this, "shift", { x: 0, y: 0 }); __publicField(this, "marquee", { x: 0, y: 0, width: 0, height: 0 }); __publicField(this, "downHandler"); __publicField(this, "startHandler"); __publicField(this, "moveHandler"); __publicField(this, "endHandler"); __publicField(this, "isInfinity", false); __publicField(this, "needInfinity", () => { this.isInfinity = true; return this; }); __publicField(this, "onDown", (callback) => { if (this.downHandler) return this; this.downHandler = ({ clientX, clientY }) => { if (this.isInfinity) { document.body.requestPointerLock(); } this.canMove = true; this.current = { x: clientX, y: clientY }; this.start = { x: clientX, y: clientY }; this.marquee = this.calculateMarquee(); callback?.({ current: this.current, start: this.start, shift: this.shift, marquee: this.marquee }); }; window.addEventListener("mousedown", this.downHandler); return this; }); __publicField(this, "onStart", (callback) => { if (this.startHandler) return this; this.startHandler = ({ clientX, clientY }) => { this.current = { x: clientX, y: clientY }; this.start = { x: clientX, y: clientY }; this.marquee = this.calculateMarquee(); callback?.({ current: this.current, start: this.start, shift: this.shift, marquee: this.marquee }); }; if (!this.downHandler) { window.addEventListener("mousedown", () => { this.canMove = true; if (this.isInfinity) document.body.requestPointerLock(); }); } return this; }); __publicField(this, "onMove", (callback) => { if (this.moveHandler) return this; this.moveHandler = (event) => { if (!this.canMove) return; this.canMove = true; if (!this.started) { this.startHandler?.(event); this.started = true; } const { movementX, movementY } = event; const delta = xy_(movementX, movementY); this.current = xy_plus(this.current, delta); this.shift = xy_minus(this.current, this.start); this.marquee = this.calculateMarquee(); callback({ current: this.current, start: this.start, shift: this.shift, delta, marquee: this.marquee }); }; window.addEventListener("mousemove", this.moveHandler); return this; }); __publicField(this, "onDestroy", (callback) => { if (this.endHandler) return this; this.endHandler = () => { if (!this.canMove) return; this.marquee = this.calculateMarquee(); callback?.({ current: this.current, start: this.start, shift: this.shift, marquee: this.marquee }); this.destroy(); }; window.addEventListener("mouseup", this.endHandler); return this; }); __publicField(this, "onSlide", (callback) => { this.onStart().onMove(callback).onDestroy(); return this; }); __publicField(this, "destroy", () => { window.removeEventListener("mousedown", this.downHandler || noopFunc); window.removeEventListener("mousedown", this.startHandler || noopFunc); window.removeEventListener("mousemove", this.moveHandler || noopFunc); window.removeEventListener("mouseup", this.endHandler || noopFunc); this.downHandler = void 0; this.startHandler = void 0; this.moveHandler = void 0; this.endHandler = void 0; if (this.isInfinity) { document.exitPointerLock(); } this.setDataToDefault(); }); __publicField(this, "calculateMarquee", () => { this.marquee = { x: this.start.x, y: this.start.y, width: 0, height: 0 }; if (this.shift.x >= 0 && this.shift.y >= 0) { this.marquee.width = this.shift.x; this.marquee.height = this.shift.y; } if (this.shift.x >= 0 && this.shift.y < 0) { this.marquee.y = this.start.y + this.shift.y; this.marquee.width = this.shift.x; this.marquee.height = -this.shift.y; } if (this.shift.x < 0 && this.shift.y >= 0) { this.marquee.x = this.start.x + this.shift.x; this.marquee.width = -this.shift.x; this.marquee.height = this.shift.y; } if (this.shift.x < 0 && this.shift.y < 0) { this.marquee.x = this.start.x + this.shift.x; this.marquee.y = this.start.y + this.shift.y; this.marquee.width = -this.shift.x; this.marquee.height = -this.shift.y; } return this.marquee; }); __publicField(this, "setDataToDefault", () => { this.started = false; this.canMove = false; this.isInfinity = false; this.current = { x: 0, y: 0 }; this.start = { x: 0, y: 0 }; this.shift = { x: 0, y: 0 }; this.marquee = { x: 0, y: 0, width: 0, height: 0 }; }); } }; // src/is.ts var Is = class _Is { static number(value) { return typeof value === "number"; } static string(value) { return typeof value === "string"; } static boolean(value) { return typeof value === "boolean"; } static array(value) { return Array.isArray(value); } static object(value) { return typeof value === "object" && value !== null && !_Is.array(value); } static function(value) { return typeof value === "function"; } static null(value) { return value === null; } static undefined(value) { return value === void 0; } static symbol(value) { return typeof value === "symbol"; } static nullable(value) { return value === null || value === void 0; } static notNullable(value) { return value !== null && value !== void 0; } static falsy(value) { return _Is.nullable(value) || value === "null" || value === "undefined" || value === false || value === "false" || value === 0 || value === "" || isNaN(value); } static notFalsy(value) { return !_Is.falsy(value); } static empty(value) { if (_Is.array(value)) return value.length === 0; if (_Is.object(value)) return Object.keys(value).length === 0; return _Is.falsy(value); } static notEmpty(value) { return !_Is.empty(value); } }; // src/cache.ts var Cache = class { constructor() { __publicField(this, "cache", /* @__PURE__ */ new Map()); __publicField(this, "compareCache", /* @__PURE__ */ new Map()); } get(key) { return this.cache.get(key); } set(key, value) { this.cache.set(key, value); return value; } delete(key) { this.cache.delete(key); } getSet(key, fn, compare) { const value = this.cache.get(key); if (value === void 0) { return this.set(key, fn()); } if (compare) { const lastCompare = this.compareCache.get(key); const expired = compare?.some((i, index) => i !== lastCompare?.[index]); if (expired) { this.compareCache.set(key, compare); return this.set(key, fn()); } } return value; } clear() { this.cache.clear(); } forEach(callback) { this.cache.forEach((v, k, m) => callback(k, v, m)); } keys() { return this.cache.keys(); } values() { return this.cache.values(); } entries() { return this.cache.entries(); } fromObject(obj) { this.cache = new Map(Object.entries(obj)); } toObject() { return Object.fromEntries( [...this.cache.entries()].map(([key, value]) => [key, value]) ); } }; function createCache() { return new Cache(); } var ObjCache = class { constructor() { __publicField(this, "cache", {}); __publicField(this, "compareCache", /* @__PURE__ */ new Map()); } get(key) { return this.cache[key]; } set(key, value) { this.cache[key] = value; return value; } delete(key) { delete this.cache[key]; } clear() { this.cache = {}; } keys() { return Object.keys(this.cache); } values() { return Object.values(this.cache); } entries() { return Object.entries(this.cache); } getSet(key, fn, compare) { const value = this.cache[key]; if (value === void 0) return this.set(key, fn()); if (!compare) return value; const lastCompare = this.compareCache.get(key); if (compare?.some((i, index) => i !== lastCompare?.[index])) { this.compareCache.set(key, compare); return this.set(key, fn()); } return value; } }; function createObjCache() { return new ObjCache(); } // src/common.ts var This = globalThis; function Delete(target, filter) { if (Array.isArray(target)) { const index = typeof filter === "function" ? target.findIndex(filter) : target.findIndex((i) => i === filter); index >= 0 && target.splice(index, 1); } else { delete target[filter]; } } function iife(callback) { return callback(); } function matchCase(...args) { if (args.length === 2) { const [Case, obj] = args; return obj[Case]; } else { const [Case, Default, obj] = args; return obj[Case] || Default; } } function Log(someThing, label = "") { console.log(label, someThing); return someThing; } var macroMatchCache = createObjCache(); function macroMatch(_input) { const input = _input[0]; const test = macroMatchCache.getSet(input, () => { const right = input.trimStart().trimEnd().split("|"); return new Function( "left", right.reduce((all, i) => { return `if(left === ${i})return true;` + all; }, "return false;") ); }); return (left) => test(left); } function clone(object) { if (typeof object !== "object" || object === null) return object; const newObj = Array.isArray(object) ? [] : {}; for (const key in object) newObj[key] = clone(object[key]); return newObj; } function jsonFy(obj) { try { return JSON.stringify(obj, null, 2); } catch (e) { console.log("jsonFy error", e); } } function jsonParse(obj) { try { return JSON.parse(obj); } catch (e) { console.log("jsonParse error", e); } } function memorize(func) { const cache = createObjCache(); return (...args) => { const key = args.join("-"); return cache.getSet(key, () => func(...args)); }; } function debounce(wait, func) { let timeout; return (...args) => { clearTimeout(timeout); timeout = setTimeout(() => func(...args), wait); }; } function objKeys(obj) { return Object.keys(obj); } var Raf = class { constructor() { __publicField(this, "ids", []); } request(callback) { const id = requestAnimationFrame(() => callback(() => this.request(callback))); this.ids.push(id); return this; } cancelAll() { this.ids.forEach(cancelAnimationFrame); return this; } }; var objectKeyMap = /* @__PURE__ */ new WeakMap(); function objectKey(obj) { if (!objectKeyMap.has(obj)) { objectKeyMap.set(obj, nanoid()); } return objectKeyMap.get(obj); } function suffixOf(input, lowerCase = false) { if (!input) return ""; const index = input.lastIndexOf("."); if (index === -1) return ""; const suffix = input.slice(index + 1); return lowerCase ? suffix.toLowerCase() : suffix; } function createFuncAOP(before, after) { return (func) => { return (...args) => { before?.(...args); const result = func(...args); after?.(...args); return result; }; }; } function SetTimeout(func, time = 0) { const id = setTimeout(() => { func(); clearTimeout(id); }, time); } function optionalSet(target, key, value) { if (Is.nullable(target)) return; target[key] = value; } var alphabet = "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ_abcdefghijklmnpqrstuvwxyz"; function nanoid(size = 8) { let id = ""; let i = size | 0; while (i--) { id += alphabet[Math.random() * 61 | 0]; } return id; } // src/browser/event.ts var isLeftMouse = (e) => e.button === 0; var isRightMouse = (e) => e.button === 2; function listen(...args) { const [type, options, listener] = iife(() => { let type2 = args[0]; let options2 = {}; let listener2 = args[1]; if (args.length === 3) { listener2 = args[2]; options2 = args[1]; } return [type2, options2, listener2]; }); const target = options.target || window; target.addEventListener(type, listener, options); return () => target.removeEventListener(type, listener, options); } function stopPropagation(callback) { return (e) => { callback?.(e); e.stopPropagation(); }; } function preventDefault(callback) { return (e) => { callback?.(e); e.preventDefault(); }; } // src/browser/storage.ts var _store, _get, _mapToObject; var StorageUtil = class { constructor() { __publicField(this, "set", (key, value) => { const item = iife(() => { if (value instanceof Set) return { type: "set", value: [...value] }; if (value instanceof Map) return { type: "map", value: __privateGet(this, _mapToObject).call(this, value) }; return { type: "normal", value }; }); __privateGet(this, _store).call(this, key, item); }); __publicField(this, "get", (key) => { const item = __privateGet(this, _get).call(this, key); if (!item) return; if (item.type === "set") return new Set(item.value); if (item.type === "map") return new Map(Object.entries(item.value)); return item.value; }); __privateAdd(this, _store, (key, value) => { localStorage.setItem(key, jsonFy(value)); }); __privateAdd(this, _get, (key) => { return jsonParse(localStorage.getItem(key)); }); __privateAdd(this, _mapToObject, (map) => { return Object.fromEntries(map.entries()); }); } }; _store = new WeakMap(); _get = new WeakMap(); _mapToObject = new WeakMap(); // src/browser/wheel.ts var WheelUtil = class { constructor() { __publicField(this, "beforeWheelHandler"); __publicField(this, "duringWheelHandler"); __publicField(this, "afterWheelHandler"); __publicField(this, "wheelTimeOut"); __publicField(this, "curFrameTriggered", false); __publicField(this, "onBeforeWheel", (handler) => { this.beforeWheelHandler = handler; }); __publicField(this, "onDuringWheel", (handler) => { this.duringWheelHandler = handler; }); __publicField(this, "onAfterWheel", (handler) => { this.afterWheelHandler = handler; }); __publicField(this, "onWheel", (e) => { const direction = e.deltaY > 0 ? 1 : -1; if (this.wheelTimeOut) { clearTimeout(this.wheelTimeOut); } else { this.beforeWheelHandler?.({ e, direction }); } if (!this.curFrameTriggered) { this.curFrameTriggered = true; this.duringWheelHandler?.({ e, direction }); requestAnimationFrame(() => this.curFrameTriggered = false); } this.wheelTimeOut = setTimeout(() => { this.wheelTimeOut = void 0; this.afterWheelHandler?.({ e, direction }); }, 250); }); } }; export { AnyObject, Delete, DragUtil, Is, Log, Raf, SetTimeout, StorageUtil, This, WheelUtil, anyFunc, clone, createCache, createFuncAOP, createObjCache, debounce, firstOne, flushFuncs, iife, isLeftMouse, isRightMouse, jsonFy, jsonParse, lastOne, listen, loopFor, macroMatch, matchCase, memorize, nanoid, noopFunc, objKeys, objectKey, optionalSet, preventDefault, range, reverse, reverseFor, stableIndex, stopPropagation, suffixOf };