UNPKG

@baiwusanyu/utils-com

Version:
217 lines (212 loc) 6.33 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // ../node_modules/.pnpm/hash-sum@2.0.0/node_modules/hash-sum/hash-sum.js var require_hash_sum = __commonJS({ "../node_modules/.pnpm/hash-sum@2.0.0/node_modules/hash-sum/hash-sum.js"(exports2, module2) { "use strict"; function pad(hash2, len) { while (hash2.length < len) { hash2 = "0" + hash2; } return hash2; } function fold(hash2, text) { var i; var chr; var len; if (text.length === 0) { return hash2; } for (i = 0, len = text.length; i < len; i++) { chr = text.charCodeAt(i); hash2 = (hash2 << 5) - hash2 + chr; hash2 |= 0; } return hash2 < 0 ? hash2 * -2 : hash2; } function foldObject(hash2, o, seen) { return Object.keys(o).sort().reduce(foldKey, hash2); function foldKey(hash3, key) { return foldValue(hash3, o[key], key, seen); } } function foldValue(input, value, key, seen) { var hash2 = fold(fold(fold(input, key), toString(value)), typeof value); if (value === null) { return fold(hash2, "null"); } if (value === void 0) { return fold(hash2, "undefined"); } if (typeof value === "object" || typeof value === "function") { if (seen.indexOf(value) !== -1) { return fold(hash2, "[Circular]" + key); } seen.push(value); var objHash = foldObject(hash2, value, seen); if (!("valueOf" in value) || typeof value.valueOf !== "function") { return objHash; } try { return fold(objHash, String(value.valueOf())); } catch (err) { return fold(objHash, "[valueOf exception]" + (err.stack || err.message)); } } return fold(hash2, value.toString()); } function toString(o) { return Object.prototype.toString.call(o); } function sum(o) { return pad(foldValue(0, o, "", []).toString(16), 8); } module2.exports = sum; } }); // ../packages/com/index.ts var com_exports = {}; __export(com_exports, { copyText: () => copyText, createHash: () => createHash, debounce: () => debounce, downloadFile: () => downloadFile, localCache: () => localCache, sessionCache: () => sessionCache, throttle: () => throttle }); module.exports = __toCommonJS(com_exports); // ../packages/com/src/com.ts var import_hash_sum = __toESM(require_hash_sum(), 1); var downloadFile = (url) => { const aLink = document.createElement("a"); aLink.href = url; aLink.click(); }; function debounce(func, wait) { let timeoutId; return function debounced(...args) { if (timeoutId !== void 0) clearTimeout(timeoutId); timeoutId = window.setTimeout(() => { func(...args); timeoutId = void 0; }, wait); }; } function throttle(func, wait) { let isThrottled = false; return function throttled(...args) { if (!isThrottled) { func(...args); isThrottled = true; setTimeout(() => { isThrottled = false; }, wait); } }; } var createHash = (id = (/* @__PURE__ */ new Date()).getTime().toString(), prefix = "", suffix = "") => { return `${prefix}${(0, import_hash_sum.default)(id)}${suffix}`; }; var copyText = (content, resolve, err) => { navigator.clipboard.writeText(content).then(() => { resolve && resolve(); }).catch((error) => { err && err(error); }); }; // ../packages/com/src/cache.ts var sessionCache = { set(key, value) { if (!sessionStorage) return; if (key != null && value != null) sessionStorage.setItem(key, value); }, get(key) { if (!sessionStorage) return null; if (key == null) return null; return sessionStorage.getItem(key); }, setJSON(key, jsonValue) { if (jsonValue != null) this.set(key, JSON.stringify(jsonValue)); }, getJSON(key) { const value = this.get(key); if (value != null) return JSON.parse(value); }, remove(key) { sessionStorage.removeItem(key); } }; var localCache = { set(key, value) { if (!localStorage) return; if (key != null && value != null) localStorage.setItem(key, value); }, get(key) { if (!localStorage) return null; if (key == null) return null; return localStorage.getItem(key); }, setJSON(key, jsonValue) { if (jsonValue != null) this.set(key, JSON.stringify(jsonValue)); }, getJSON(key) { const value = this.get(key); if (value != null) return JSON.parse(value); }, remove(key) { localStorage.removeItem(key); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { copyText, createHash, debounce, downloadFile, localCache, sessionCache, throttle });