Code coverage report for core/src/time/util.ts

Statements: 94.59% (35 / 37)      Branches: 76.47% (13 / 17)      Functions: 80% (8 / 10)      Lines: 94.44% (34 / 36)      Ignored: none     

All files » core/src/time/ » util.ts
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 60 61 62 63 64 65 66 67 68 69 70 711 1       1   125 4   121 112   9 9   125       1         1           1 35       1 54 54 54       1 3       1 42 7 7 7 49 49 49   7       1 10 10       1 7    
import {Milliseconds, NANOS_IN_MS, Nanoseconds, NanoTime, TimeAndRemainder} from './constants';
import {NanoTimeMeasurement} from './primitives';
 
 
 
export function rawNanos(nanos: NanoTime): bigint {
  let result;
  if (nanos instanceof NanoTimeMeasurement) {
    result = (nanos as NanoTimeMeasurement).asNanoseconds();
  }
  else if (typeof nanos === 'bigint' || result instanceof BigInt) {
    result = nanos;
  }
  else Eif (nanos && 'asNanoseconds' in (nanos as any)) {
    result = (nanos as any).asNanoseconds();
  }
  return result;
}
 
 
export function millisAsNanoString(milllis: Milliseconds, postfix: '' | 'n' = '') {
  return `${milllis}000000${postfix}`;
}
 
 
export function nanosAsString(nanos: NanoTime, postfix: '' | 'n' = ''): string {
  return `${rawNanos(nanos)}${postfix}`;
}
 
 
 
export function millisAsNanos(millis: Milliseconds): bigint {
  return NANOS_IN_MS * BigInt(millis);
}
 
 
export function timePart(nanos: NanoTime, unit: Nanoseconds): TimeAndRemainder {
  const count = rawNanos(nanos) / unit;
  const remainder = rawNanos(nanos) % unit;
  return [count, remainder];
}
 
 
export function timePartValue(nanos: NanoTime, unit: Nanoseconds): bigint {
  return timePart(nanos, unit)[0];
}
 
 
export function timeParts(nanos: NanoTime, parts: { name: string; unit: bigint }[]): Record<string, bigint> {
  parts.sort((left, right) => left.unit > right.unit ? -1 : 1);
  let remainder: bigint = rawNanos(nanos);
  const result = {};
  for (const part of parts) {
    const [val, remain] = timePart(remainder, part.unit);
    result[part.name] = val;
    remainder = remain;
  }
  return result;
}
 
 
export function nanosAsMillis(nanos: NanoTime, truncate = false): Milliseconds {
  const result = Number(rawNanos(nanos) / NANOS_IN_MS);
  return truncate ? Math.floor(result) : result;
}
 
 
export function nanosAsJsDate(nanos: NanoTime): Date {
  return new Date(Number(nanosAsMillis(nanos)));
}