# All files / lib util.ts

88.24% Statements 15/17
85.71% Branches 12/14
100% Functions 4/4
91.67% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    381x
    2x

    1x

    565x
    542x
    332x
    332x

    1x

    296x

    184x

    112x

     export function sleep(ms: number): Promise<void> {
      return new Promise((resolve) => setTimeout(resolve, ms));
    }

    /**
     * Safely converts various input types to BigInt
     * @param value Value to convert to BigInt
     * @returns BigInt representation of the value, or BigInt(0) for null/undefined
     */
    export function toBigInt(
      value: string | number | bigint | null | undefined,
    ): bigint {
      if (value === null || value === undefined) return BigInt(0);
      if (typeof value === 'bigint') return value;
      Iif (typeof value === 'string') return BigInt(value);
      Eif (typeof value === 'number') return BigInt(value);
      return BigInt(0);
    }

    /**
     * Safely converts BigInt to number, preserving precision for safe integers
     * @param value BigInt value to convert
     * @returns Number if within safe range, otherwise returns the BigInt as-is for JSON serialization
     */
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    export function bigIntToNumber(value: bigint): number | any {
      // For values within safe integer range, convert to number
      if (
        value <= BigInt(Number.MAX_SAFE_INTEGER) &&
        value >= BigInt(Number.MIN_SAFE_INTEGER)
      ) {
        return Number(value);
      }
      // For larger values, preserve as BigInt (json-bigint will handle serialization)
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      return value as any;
    }