util.js 8.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // 工具:数字格式化(中文万进制单位,超额用科学计数法)、DOM 辅助、SVG 图标。署名:FmodeCode
  2. const UNITS = ['', '万', '亿', '兆', '京', '垓', '秭', '穰', '沟', '涧', '正', '载', '极'];
  3. function trim(v, d) { const s = Number(v.toPrecision(12)).toFixed(d); return s.includes('.') ? s.replace(/\.?0+$/, '') : s; }
  4. export function fmt(n) {
  5. if (n === undefined || n === null || Number.isNaN(n)) return '0';
  6. if (!Number.isFinite(n)) return '∞';
  7. const neg = n < 0; n = Math.abs(n);
  8. let s;
  9. if (n === 0) s = '0';
  10. else if (n < 1) s = n < 0.01 ? fmtSci(n) : trim(n, 2);
  11. else if (n < 1e4) s = n < 10 ? trim(n, 1) : String(Math.round(n));
  12. else if (n < 1e4 ** UNITS.length) {
  13. const k = Math.floor(Math.log10(n) / 4);
  14. const m = n / Math.pow(10, k * 4);
  15. s = trim(m, m >= 100 ? 0 : 2) + UNITS[k];
  16. } else s = fmtSci(n);
  17. return (neg ? '-' : '') + s;
  18. }
  19. export function fmtSci(n) {
  20. const e = Math.floor(Math.log10(n));
  21. const m = n / Math.pow(10, e);
  22. return trim(m, 2) + 'e' + e;
  23. }
  24. export function fmtPct(x, d = 0) { return trim(x, d) + '%'; }
  25. export function fmtTime(sec) {
  26. if (!Number.isFinite(sec)) return '—';
  27. sec = Math.max(0, Math.round(sec));
  28. if (sec < 60) return sec + '秒';
  29. const m = Math.floor(sec / 60), s = sec % 60;
  30. if (m < 60) return m + '分' + (s ? s + '秒' : '');
  31. const h = Math.floor(m / 60), mm = m % 60;
  32. if (h < 24) return h + '时' + (mm ? mm + '分' : '');
  33. return Math.floor(h / 24) + '天';
  34. }
  35. export const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
  36. export const $ = (sel, root = document) => root.querySelector(sel);
  37. export const $$ = (sel, root = document) => Array.from(root.querySelectorAll(sel));
  38. export function el(tag, cls, html) { const e = document.createElement(tag); if (cls) e.className = cls; if (html !== undefined) e.innerHTML = html; return e; }
  39. /** 确定性伪随机(星图噪声、极光位置等) */
  40. export function hash01(n) { const x = Math.sin(n * 127.1 + 311.7) * 43758.5453; return x - Math.floor(x); }
  41. // ── 图标(内联 SVG,统一 24 视口;stroke=currentColor)──────────
  42. const P = {
  43. moss: '<path d="M4 19c1-4 2-6 4-7M9 19c0-5 1-8 3-10M14 19c0-4 1-6 3-7M19 19c0-3 .5-5 1-6" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" fill="none"/><path d="M4 19h16" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>',
  44. bug: '<ellipse cx="12" cy="14" rx="4" ry="5" fill="currentColor" opacity=".9"/><path d="M12 9V5m-2 1 2-1.5L14 6M8 12 4 10m4 4H3m13-2 4-2m-4 4h5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/><circle cx="12" cy="4" r="2" fill="currentColor"/>',
  45. fern: '<path d="M12 21V6" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/><path d="M12 9c-3-1-5-3-6-6 3 .5 5 2 6 6Zm0 0c3-1 5-3 6-6-3 .5-5 2-6 6ZM12 14c-3-1-5-2-6-5 3 .5 5 2 6 5Zm0 0c3-1 5-2 6-5-3 .5-5 2-6 5Z" fill="currentColor"/>',
  46. deer: '<path d="M8 21v-6l-2-3 3-1M16 21v-6l2-3-3-1M10 11c0-2 1-3 2-3s2 1 2 3M9 7 7 3m2 4 1-4M15 7l2-4m-2 4-1-4" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" fill="none"/><path d="M10 11h4l-2 4-2-4Z" fill="currentColor"/>',
  47. whale: '<path d="M3 13c4-4 9-5 13-3 2 1 3 3 5 3-1 3-4 5-8 5-5 0-8-2-10-5Z" fill="currentColor" opacity=".9"/><path d="M19 12c1-2 2-3 2-5-2 0-3 1-3 3" stroke="currentColor" stroke-width="1.5" fill="none"/><circle cx="7" cy="12" r=".9" fill="#0d1424"/>',
  48. human: '<circle cx="12" cy="6" r="3" fill="currentColor"/><path d="M6 21c0-5 3-8 6-8s6 3 6 8" stroke="currentColor" stroke-width="1.8" fill="none" stroke-linecap="round"/><path d="M12 13v4" stroke="currentColor" stroke-width="1.6"/>',
  49. web: '<path d="M12 3v18M3 12h18M5.6 5.6l12.8 12.8M18.4 5.6 5.6 18.4" stroke="currentColor" stroke-width="1.2" opacity=".7"/><circle cx="12" cy="12" r="8.6" stroke="currentColor" stroke-width="1.4" fill="none"/><circle cx="12" cy="12" r="2.4" fill="currentColor"/>',
  50. mantis: '<path d="M12 21v-8m0 0-3-6 3-3 3 3-3 6Zm-3-5-5-3m11 3 5-3M12 13l-4 3m4-3 4 3" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" fill="none"/><circle cx="12" cy="3.5" r="1.6" fill="currentColor"/>',
  51. dove: '<path d="M3 13c4 0 7-2 9-6 1 3 3 5 7 5-2 4-6 7-11 7-3 0-5-2-5-6Z" fill="currentColor" opacity=".9"/><path d="M12 7 9 3l4 1" stroke="currentColor" stroke-width="1.5" fill="none"/><circle cx="15" cy="10" r=".9" fill="#0d1424"/>',
  52. starplant: '<path d="M12 22V10" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/><path d="M12 4l1.8 4.2L18 10l-4.2 1.8L12 16l-1.8-4.2L6 10l4.2-1.8L12 4Z" fill="currentColor"/>',
  53. star: '<path d="M12 2l2.6 6.5L21 10l-5 4.4L17.4 21 12 17.4 6.6 21 8 14.4 3 10l6.4-1.5L12 2Z" fill="currentColor"/>',
  54. root: '<path d="M12 21V5m0 10-5-4m5 2 5-4M12 18l-4 3m4-3 4 3" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" fill="none"/><circle cx="12" cy="3.5" r="2" fill="currentColor"/>',
  55. sprout: '<path d="M12 21v-8" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/><path d="M12 13c0-4-3-6-7-6 0 4 3 6 7 6Zm0 0c0-4 3-6 7-6 0 4-3 6-7 6Z" fill="currentColor"/>',
  56. earth: '<circle cx="12" cy="12" r="9" stroke="currentColor" stroke-width="1.6" fill="none"/><path d="M6 9c2 0 3 2 5 1s3 1 4 3-1 3-3 4-2 2-4 1M14 4c0 2-2 2-1 4" stroke="currentColor" stroke-width="1.5" fill="none"/>',
  57. moon: '<path d="M15 3a9 9 0 1 0 6 14A7.5 7.5 0 0 1 15 3Z" fill="currentColor"/>',
  58. mars: '<circle cx="12" cy="12" r="8" stroke="currentColor" stroke-width="1.8" fill="none"/><path d="M18 6l3-3M17 3h4v4" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" fill="none"/><circle cx="10" cy="11" r="1.6" fill="currentColor"/>',
  59. planet: '<circle cx="12" cy="12" r="8" stroke="currentColor" stroke-width="1.8" fill="none"/><path d="M3 9c4 1 9 1 13-1M8 20c1-4 1-9-1-13" stroke="currentColor" stroke-width="1.2" opacity=".7" fill="none"/>',
  60. ring: '<circle cx="12" cy="11" r="5" fill="currentColor" opacity=".8"/><ellipse cx="12" cy="13" rx="10" ry="3" stroke="currentColor" stroke-width="1.6" fill="none"/>',
  61. comet: '<circle cx="16" cy="8" r="3" fill="currentColor"/><path d="M14 10 3 21M12 7 2 14M17 12 9 21" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/>',
  62. sun: '<circle cx="12" cy="12" r="4.5" fill="currentColor"/><path d="M12 2v3m0 14v3M2 12h3m14 0h3M4.9 4.9 7 7m10 10 2.1 2.1M19.1 4.9 17 7M7 17l-2.1 2.1" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>',
  63. book: '<path d="M4 5c3-1 5-1 8 1v14c-3-2-5-2-8-1V5Zm16 0c-3-1-5-1-8 1v14c3-2 5-2 8-1V5Z" stroke="currentColor" stroke-width="1.6" fill="none" stroke-linejoin="round"/>',
  64. hourglass: '<path d="M6 3h12M6 21h12M7 3c0 5 5 6 5 9s-5 4-5 9m10-18c0 5-5 6-5 9s5 4 5 9" stroke="currentColor" stroke-width="1.6" fill="none" stroke-linecap="round"/>',
  65. seed: '<path d="M12 21c-4-3-6-7-6-10 0-4 3-7 6-7s6 3 6 7c0 3-2 7-6 10Z" fill="currentColor" opacity=".9"/><path d="M12 8v8" stroke="#0d1424" stroke-width="1.3"/>',
  66. hand: '<path d="M8 12V6a1.5 1.5 0 0 1 3 0v5m0-6a1.5 1.5 0 0 1 3 0v6m0-5a1.5 1.5 0 0 1 3 0v7c0 4-2 7-6 7-3 0-5-1-6-4l-1.5-3a1.5 1.5 0 0 1 2.5-1.6L8 14" stroke="currentColor" stroke-width="1.6" fill="none" stroke-linecap="round" stroke-linejoin="round"/>',
  67. compass: '<circle cx="12" cy="12" r="9" stroke="currentColor" stroke-width="1.6" fill="none"/><path d="m15 9-2 4-4 2 2-4 4-2Z" fill="currentColor"/>',
  68. spark: '<path d="M12 2v6m0 8v6M2 12h6m8 0h6M5 5l4 4m6 6 4 4M19 5l-4 4M9 15l-4 4" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>',
  69. leaf: '<path d="M5 19c0-8 6-14 14-14 0 8-6 14-14 14Z" fill="currentColor" opacity=".9"/><path d="M5 19 15 9" stroke="#0d1424" stroke-width="1.3"/>',
  70. drop: '<path d="M12 3c3 4 6 8 6 11a6 6 0 0 1-12 0c0-3 3-7 6-11Z" fill="currentColor"/>',
  71. wind: '<path d="M3 8h11a3 3 0 1 0-3-3M3 12h15a3 3 0 1 1-3 3M3 16h8" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" fill="none"/>',
  72. lock: '<rect x="5" y="10" width="14" height="10" rx="2" stroke="currentColor" stroke-width="1.7" fill="none"/><path d="M8 10V7a4 4 0 0 1 8 0v3" stroke="currentColor" stroke-width="1.7" fill="none"/>',
  73. check: '<path d="m5 12 4.5 4.5L19 7" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" fill="none"/>',
  74. pulse: '<path d="M2 12h4l2-6 4 12 2-6h8" stroke="currentColor" stroke-width="1.8" fill="none" stroke-linecap="round" stroke-linejoin="round"/>',
  75. plus: '<path d="M12 5v14M5 12h14" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>',
  76. arrow: '<path d="m9 5 7 7-7 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" fill="none"/>',
  77. };
  78. export function icon(name, cls = '') {
  79. const body = P[name] || P.star;
  80. return `<svg class="ico ${cls}" viewBox="0 0 24 24" aria-hidden="true">${body}</svg>`;
  81. }