UNPKG

751 BJavaScriptView Raw
1import { bnToBn } from '../bn/toBn.js';
2/** @internal */
3function formatValue(elapsed) {
4 if (elapsed < 15) {
5 return `${elapsed.toFixed(1)}s`;
6 }
7 else if (elapsed < 60) {
8 return `${elapsed | 0}s`;
9 }
10 else if (elapsed < 3600) {
11 return `${elapsed / 60 | 0}m`;
12 }
13 return `${elapsed / 3600 | 0}h`;
14}
15/**
16 * @name formatElapsed
17 * @description Formats an elapsed value into s, m, h or day segments
18 */
19export function formatElapsed(now, value) {
20 const tsNow = now?.getTime() || 0;
21 const tsValue = value instanceof Date
22 ? value.getTime()
23 : bnToBn(value).toNumber();
24 return (tsNow && tsValue)
25 ? formatValue(Math.max(Math.abs(tsNow - tsValue), 0) / 1000)
26 : '0.0s';
27}