| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- // 通用工具:大数格式化、数学、随机数、DOM 辅助
- export const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
- export const lerp = (a, b, t) => a + (b - a) * t;
- export const rand = (a, b) => a + Math.random() * (b - a);
- export const randInt = (a, b) => Math.floor(rand(a, b + 1));
- // 确定性随机(里世界选秀可复现)
- export function mulberry32(seed) {
- let a = seed >>> 0;
- return function () {
- a |= 0; a = (a + 0x6D2B79F5) | 0;
- let t = Math.imul(a ^ (a >>> 15), 1 | a);
- t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
- return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
- };
- }
- export function hashStr(s) {
- let h = 2166136261 >>> 0;
- for (let i = 0; i < s.length; i++) { h ^= s.charCodeAt(i); h = Math.imul(h, 16777619); }
- return h >>> 0;
- }
- // 中文大数单位(万进制),超出「极」后使用科学计数法
- export const UNITS = ['', '万', '亿', '兆', '京', '垓', '秭', '穰', '沟', '涧', '正', '载', '极'];
- function trimFixed(v, d) {
- if (d <= 0) return String(Math.round(v));
- const s = v.toFixed(d);
- return s.includes('.') ? s.replace(/\.?0+$/, '') : s;
- }
- export function sci(n) {
- const e = Math.floor(Math.log10(n));
- const m = n / Math.pow(10, e);
- return m.toFixed(2).replace(/\.?0+$/, '') + 'e' + e;
- }
- /** 数字格式化:<1万 合理精度;万~极 用中文单位;更大用科学计数法 */
- export function fmt(n, opt = {}) {
- if (n === undefined || n === null || Number.isNaN(n)) return '0';
- if (!Number.isFinite(n)) return '∞';
- const neg = n < 0;
- n = Math.abs(n);
- let s;
- if (n < 1 && n > 0) s = n < 0.001 ? n.toPrecision(2) : trimFixed(n, 2);
- else if (n < 1e4) {
- if (n < 10) s = trimFixed(n, opt.int ? 0 : 2);
- else if (n < 100) s = trimFixed(n, opt.int ? 0 : 1);
- else s = opt.int ? String(Math.round(n)) : trimFixed(n, 1);
- } else {
- const tier = Math.floor(Math.log10(n) / 4);
- if (tier < UNITS.length) {
- const v = n / Math.pow(10, tier * 4);
- s = trimFixed(v, v >= 100 ? 0 : v >= 10 ? 1 : 2) + UNITS[tier];
- } else s = sci(n);
- }
- return (neg ? '-' : '') + s;
- }
- /** 百分比(输入 0.25 → 25%) */
- export function fmtPct(x, d = 1) {
- if (!Number.isFinite(x)) return '100%';
- return trimFixed(x * 100, d) + '%';
- }
- export function fmtX(x) { return '×' + fmt(x); }
- /** 时长:95 → 1分35秒 */
- export function fmtTime(sec) {
- if (!Number.isFinite(sec)) return '∞';
- sec = Math.max(0, Math.round(sec));
- if (sec < 60) return sec + '秒';
- const m = Math.floor(sec / 60), s = sec % 60;
- if (m < 60) return m + '分' + (s ? s + '秒' : '');
- const h = Math.floor(m / 60), mm = m % 60;
- if (h < 24) return h + '时' + (mm ? mm + '分' : '');
- return Math.floor(h / 24) + '天' + (h % 24 ? (h % 24) + '时' : '');
- }
- export const safeNum = (v, def = 0) => { const n = Number(v); return Number.isFinite(n) ? n : def; };
- export const deepClone = (o) => (o === undefined ? o : JSON.parse(JSON.stringify(o)));
- export function el(tag, cls, text) {
- const e = document.createElement(tag);
- if (cls) e.className = cls;
- if (text !== undefined) e.textContent = text;
- return e;
- }
- export function esc(s) {
- return String(s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
- }
|