aboutsummaryrefslogtreecommitdiffstats
path: root/src/format.js
blob: 6191033bcfc5f8c5d6632c9ca262da7e31c21c7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Small formatting helpers. Internally amounts are integer satoshis (Number;
// safe because max BTC supply ~2.1e15 sats < Number.MAX_SAFE_INTEGER).

export const SATS = 100_000_000;

export function btc(sats) {
  return (Number(sats) / SATS).toFixed(8);
}

// "0.01234500" with the trailing-zero portion dimmed is done in CSS; here we
// just produce a clean fixed-8 string.
export function fmtBtc(sats) {
  return btc(sats);
}

export function fmtSats(sats) {
  return Number(sats).toLocaleString('en-US');
}

export function fmtUsd(sats, price) {
  if (!price) return '';
  const usd = (Number(sats) / SATS) * price;
  return usd.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
}

// Parse a user-entered amount. Accepts BTC (default) or sats.
export function parseAmount(value, unit) {
  const n = Number(String(value).trim().replace(/,/g, ''));
  if (!isFinite(n) || n < 0) return null;
  return unit === 'sats' ? Math.round(n) : Math.round(n * SATS);
}

export function shortAddr(a, head = 10, tail = 8) {
  if (!a || a.length <= head + tail + 1) return a;
  return `${a.slice(0, head)}…${a.slice(-tail)}`;
}

export function shortTxid(t) {
  return shortAddr(t, 8, 8);
}

export function timeAgo(unixSeconds) {
  if (!unixSeconds) return 'pending';
  const s = Math.max(0, Math.floor(Date.now() / 1000 - unixSeconds));
  const units = [
    [31536000, 'y'],
    [2592000, 'mo'],
    [86400, 'd'],
    [3600, 'h'],
    [60, 'm'],
  ];
  for (const [secs, label] of units) {
    if (s >= secs) return `${Math.floor(s / secs)}${label} ago`;
  }
  return `${s}s ago`;
}
BP: 0.7633973179069156, GEL: 2.8950320527351363, GGP: 0.7634417474407681, GHS: 5.506778677450651, GIP: 0.7634417474407681, GMD: 51.09502026530779, GNF: 9521.397871009276, GTQ: 7.660548387389266, GYD: 208.25970150607054, HKD: 7.769651001477962, HNL: 24.612961636457605, HRK: 6.748502543364132, HTG: 99.227949803694, HUF: 305.64013800357253, IDR: 13667.802722895718, ILS: 3.451430358972499, IMP: 0.7634417474407681, INR: 71.46499950130115, IQD: 1193.147438048002, IRR: 42105.008235784815, ISK: 123.38744004279744, JEP: 0.7634417474407681, JMD: 139.41456744675256, JOD: 0.708966605311596, JPY: 109.06551724137931, KES: 100.60072265996898, KGS: 69.85026612384053, KHR: 4089.819789277068, KMF: 446.5001795315858, KPW: 900.0134594285818, KRW: 1186.498037846709, KWD: 0.30397598991721597, KYD: 0.8328842021271773, KZT: 379.16800529527507, LAK: 8877.657789222665, LBP: 1511.1774996146419, LKR: 181.3996862730875, LRD: 195.25017907822317, LSL: 14.779750106540206, LTL: 2.95274057685856, LVL: 0.6048899688993263, LYD: 1.4029060542040312, MAD: 9.626910696636957, MDL: 17.477868651790327, MGA: 3684.7089348699296, MKD: 55.9018714807729, MMK: 1463.177394434521, MNT: 2748.7517513396865, MOP: 7.997262596679572, MRO: 356.9999410628633, MUR: 36.903554362708206, MVR: 15.4603733894294, MWK: 736.1130459618994, MXN: 18.75570103457343, MYR: 4.086504302410982, MZN: 63.850077525002945, NAD: 14.780262406267283, NGN: 361.5008196795633, NIO: 33.71733567872913, NOK: 9.173537225602292, NPR: 114.14688222546629, NZD: 1.5415452410528891, OMR: 0.3850118327636077, PAB: 0.999442364013891, PEN: 3.344138475069591, PGK: 3.4082131166864635, PHP: 50.85751085803403, PKR: 154.4431709993018, PLN: 3.892450606145783, PYG: 6525.286300289245, QAR: 3.6409975790437676, RON: 4.3302030157679505, RSD: 106.58501999329023, RUB: 63.17741166229928, RWF: 949.3656895191637, SAR: 3.75192180402042, SBD: 8.247527813794916, SCR: 13.70055763598611, SDG: 45.2856737421455, SEK: 9.638936592708117, SGD: 1.3617198763226854, SHP: 0.7634417474407681, SLL: 9715.00196215329, SOS: 585.0003091932866, SRD: 7.458006836707863, STD: 21560.79429579189, SVC: 8.74500893124303, SYP: 514.9989228104854, SZL: 14.688762048110842, THB: 31.129969080671337, TJS: 9.689288855440804, TMT: 3.5100002720175545, TND: 2.822251035933519, TOP: 2.3116505118463646, TRY: 5.979699329930091, TTD: 6.752923735345054, TWD: 30.212996092014468, TZS: 2310.202055545985, UAH: 24.83074886432671, UGX: 3677.8646377179543, USD: 1, UYU: 37.3734148177029, UZS: 9534.72592599309, VEF: 7330111621.478939, VND: 23171.26161650965, VUV: 117.62686717382827, WST: 2.6770380915248397, XAF: 594.7539682827532, XAG: 0.0560981802025624, XAU: 0.0006365210768268246, XCD: 2.7025506179332104, XDR: 0.7268916554081624, XOF: 594.7431882270803, XPF: 108.13165196260665, YER: 250.35016729079578, ZAR: 14.75420131112461, ZMK: 9001.213295311323, ZMW: 14.841628659769507, ZWL: 322.000065284213, VES: 73301.11621478939, KVES: 73.30111621478939, }