All files / common/utils/src assert.util.ts

56.25% Statements 9/16
30% Branches 3/10
66.66% Functions 2/3
56.25% Lines 9/16

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 38 39 40 4110x   10x   10x 117x     10x                       10x       27544x   27147x   27147x                        
import { isFunction } from './utils';
 
let __debug = false;
 
export function setDebug(debug: boolean) {
  __debug = debug;
}
 
export function isDebug() {
  return __debug;
}
 
export function assert(condition: boolean, errorProvider?: () => Error): void;
export function assert(
  condition: () => boolean,
  errorProvider?: () => Error,
): void;
export function assert(condition: boolean, msg?: string): void;
export function assert(condition: true, msg?: string): void;
export function assert(condition: false, msg?: string): never;
export function assert(
  condition: boolean | (() => boolean),
  msg?: string | (() => Error),
) {
  if (__debug) {
    const conditionValue =
      typeof condition === 'function' ? condition() : condition;
 
    Iif (!conditionValue) {
      /* eslint-disable no-debugger */
      debugger;
      const e = isFunction(msg) ? msg() : new Error(msg ?? 'assertion error');
      const stack = e.stack?.split('\n') ?? [];
      stack.splice(1, 1);
      e.stack = stack.join('\n');
 
      throw e;
    }
  }
}