All files / src/lib DotEnvCaster.ts

100% Statements 59/59
100% Branches 26/26
100% Functions 6/6
100% Lines 59/59

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x 1x 4x 3x 4x 1x 1x 4x 1x 8x 7x 7x 3x 3x 4x 8x 1x 1x 8x 1x 9x 8x 8x 8x 3x 3x 9x 1x 1x 9x 1x 4x 3x 4x 1x 1x 4x 1x 8x 7x 3x 3x 4x 8x 1x 1x 8x 1x 7x 6x 3x 3x 3x 3x 7x 1x 1x 7x 1x  
export class DotEnvCaster {
  castString(constant: string | undefined): string {
    if (typeof constant === 'string') {
      return constant;
    } else {
      throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
    }
  }
  castNumber(constant: string | undefined): number {
    if (typeof constant === 'string') {
      const result = Number(constant);
      if (isNaN(result) || !isFinite(result)) {
        throw new Error('This constant can not convert to number.');
      }
      return result;
    } else {
      throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
    }
  }
  castBigInt(constant: string | undefined): bigint {
    if (typeof constant === 'string') {
      try {
        return BigInt(constant);
      } catch (_e) {
        throw new Error('This constant can not convert to bigint.');
      }
    } else {
      throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
    }
  }
  castSymbol(constant: string | undefined): symbol {
    if (typeof constant === 'string') {
      return Symbol(constant);
    } else {
      throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
    }
  }
  castBoolean(constant: string | undefined): boolean {
    if (typeof constant === 'string') {
      if (!(constant === 'true' || constant === 'false')) {
        throw new Error('This constant can not convert to boolean.');
      }
      return Boolean(constant);
    } else {
      throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
    }
  }
  castNull(constant: string | undefined): null {
    if (typeof constant === 'string') {
      if (constant === 'null') {
        return null;
      } else {
        throw new Error('This constant can not convert to null.');
      }
    } else {
      throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
    }
  }
}