forked from circle-Fi/circleFi-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.js
More file actions
59 lines (54 loc) · 2.42 KB
/
Copy pathformat.js
File metadata and controls
59 lines (54 loc) · 2.42 KB
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
57
58
59
export const short = (a) => (a ? `${a.slice(0, 5)}...${a.slice(-5)}` : '');
/** Token units are integers; this is the only place that knows about decimals. */
export function amount(units, decimals, symbol) {
const v = BigInt(units ?? 0);
const neg = v < 0n;
const abs = neg ? -v : v;
const scale = 10n ** BigInt(decimals);
const frac = (abs % scale).toString().padStart(decimals, '0').replace(/0+$/, '');
return `${neg ? '-' : ''}${abs / scale}${frac ? '.' + frac : ''} ${symbol}`;
}
/** Parse what a person typed into integer token units, without float drift. */
export function toUnits(text, decimals) {
const t = String(text).trim();
if (!/^\d*\.?\d*$/.test(t) || t === '' || t === '.') return null;
const [whole = '0', frac = ''] = t.split('.');
if (frac.length > decimals) return null;
return BigInt(whole || '0') * 10n ** BigInt(decimals) + BigInt((frac || '0').padEnd(decimals, '0') || '0');
}
export function duration(seconds, t) {
const s = Number(seconds);
if (!t) {
if (s % 86400 === 0 && s >= 86400) { const d = s / 86400; return d === 1 ? 'a day' : `${d} days`; }
if (s % 3600 === 0 && s >= 3600) { const h = s / 3600; return h === 1 ? 'an hour' : `${h} hours`; }
if (s % 60 === 0 && s >= 60) return `${s / 60} min`;
return `${s}s`;
}
if (s % 86400 === 0 && s >= 86400) {
const d = s / 86400;
return d === 1 ? t('duration.a_day') : t('duration.days', { count: d });
}
if (s % 3600 === 0 && s >= 3600) {
const h = s / 3600;
return h === 1 ? t('duration.an_hour') : t('duration.hours', { count: h });
}
if (s % 60 === 0 && s >= 60) {
return t('duration.min', { count: s / 60 });
}
return t('duration.sec', { count: s });
}
export function remaining(endsAt, t) {
const left = Number(endsAt) - Math.floor(Date.now() / 1000);
if (!t) {
if (left <= 0) return 'the window has closed';
if (left < 3600) return `${Math.ceil(left / 60)} min left`;
if (left < 86400) return `${Math.floor(left / 3600)} h left`;
return `${Math.floor(left / 86400)} d left`;
}
if (left <= 0) return t('remaining.closed');
if (left < 3600) return t('remaining.min_left', { count: Math.ceil(left / 60) });
if (left < 86400) return t('remaining.h_left', { count: Math.floor(left / 3600) });
return t('remaining.d_left', { count: Math.floor(left / 86400) });
}
export const STATUS = ['forming', 'active', 'complete'];
export const statusOf = (raw) => STATUS[Number(raw)] ?? 'forming';