LCOV - code coverage report
Current view: top level - dashql-app/src/utils - format.ts Coverage Total Hit
Test: DashQL 6e74948 Lines: 98.5 % 68 67
Test Date: 2026-11-06 17:46:50

            Line data    Source code
       1            1 : export enum ByteFormat {
       2            1 :     SI = 0,
       3            1 :     IEC = 1,
       4            1 : }
       5              : 
       6            1 : export function formatBytes(value: number, format: ByteFormat = ByteFormat.SI): string {
       7            1 :     const [multiple, k, suffix] = format === ByteFormat.SI ? [1000, 'k', 'B'] : [1024, 'K', 'iB'];
       8            1 :     const exp = (Math.log(value) / Math.log(multiple)) | 0;
       9            1 :     const size = Number((value / Math.pow(multiple, exp)).toFixed(2));
      10            1 :     return `${size} ${exp ? `${k}MGTPEZY`[exp - 1] + suffix : `byte${size !== 1 ? 's' : ''}`}`;
      11            1 : }
      12              : 
      13            1 : export function formatThousands(value: number): string {
      14            1 :     const [multiple, k] = [1000, 'k'];
      15            1 :     const exp = (Math.log(value) / Math.log(multiple)) | 0;
      16            1 :     const size = Number((value / Math.pow(multiple, exp)).toFixed(2));
      17            1 :     return size + (exp ? ` ${`${k}MGTPEZY`[exp - 1]}` : '');
      18            1 : }
      19              : 
      20            1 : export function formatNanoseconds(value: number): string {
      21            1 :     const suffix = 'ns';
      22            1 :     if (value >= 1000 * 1000 * 1000) {
      23            1 :         value /= 1000 * 1000 * 1000;
      24            1 :         return `${value.toFixed(1)} s`;
      25            1 :     }
      26            1 :     if (value >= 1000 * 1000) {
      27            1 :         value /= 1000 * 1000;
      28            1 :         return `${value.toFixed(2)} ms`;
      29            1 :     }
      30            1 :     if (value >= 1000) {
      31            1 :         value /= 1000;
      32            1 :         return `${value.toFixed(0)} us`;
      33            1 :     }
      34            1 :     return `${value.toFixed(1)} ${suffix}`;
      35            1 : }
      36              : 
      37            1 : export function formatMilliseconds(value: number): string {
      38            1 :     const suffix = 'ms';
      39            1 :     if (value >= 1000) {
      40            1 :         value /= 1000;
      41            1 :         return `${value.toFixed(2)} s`;
      42            1 :     }
      43            1 :     return `${value.toFixed(0)} ${suffix}`;
      44            1 : }
      45              : 
      46            1 : export function formatHHMMSS(secs: number) {
      47            1 :     const hours = Math.floor(secs / 3600);
      48            1 :     const minutes = Math.floor((secs - (hours * 3600)) / 60);
      49            1 :     const seconds = Math.floor(secs - (hours * 3600) - (minutes * 60));
      50            1 :     const hh = hours < 10 ? `0${hours}` : hours.toString();
      51            1 :     const mm = minutes < 10 ? `0${minutes}` : minutes.toString();
      52            1 :     const ss = seconds < 10 ? `0${seconds}` : seconds.toString();
      53            1 :     return `${hh}:${mm}:${ss}`;
      54            1 : }
      55              : 
      56            1 : const TIME_UNITS: [Intl.RelativeTimeFormatUnit, number][] = [
      57            1 :     ['year', 24 * 60 * 60 * 1000 * 365],
      58            1 :     ['month', (24 * 60 * 60 * 1000 * 365) / 12],
      59            1 :     ['day', 24 * 60 * 60 * 1000],
      60            1 :     ['hour', 60 * 60 * 1000],
      61            1 :     ['minute', 60 * 1000],
      62            1 :     ['second', 1000],
      63            1 : ];
      64              : 
      65            1 : const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
      66              : 
      67            1 : export function formatTimeDifference(to: Date, from: Date = new Date()): string {
      68            1 :     const elapsed = to.getTime() - from.getTime();
      69            1 :     for (const [unitName, unitInMs] of TIME_UNITS) {
      70            1 :         if (Math.abs(elapsed) > unitInMs || unitName == 'second') {
      71            1 :             return rtf.format(Math.round(elapsed / unitInMs), unitName);
      72              :         }
      73              :     }
      74            0 :     return '';
      75            1 : }
      76              : 
      77            1 : export function quoteIfAnyUpper(text: string) {
      78            1 :     return /[A-Z]/.test(text) ? `"${text}"` : text;
      79            1 : }
        

Generated by: LCOV version 2.0-1