All files / src/auxiliary Checks.ts

100% Statements 36/36
100% Branches 4/4
100% Functions 10/10
100% Lines 35/35

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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 13558x 58x                   58x   58x 2763x               58x   58x 219x                     58x   58x 17x                     58x   58x 469x                 58x   58x 6x                 58x   58x 6x                 58x   58x 61x                   58x   58x 8007x                     58x   58x 8014x 1x   8013x 12x   8001x                   58x   1393x
import { isNotPresent, OptionalType, RequiredType } from "@jonloucks/contracts-ts/api/Types";
import { IllegalArgumentException } from "@jonloucks/contracts-ts/auxiliary/IllegalArgumentException";
 
/**
 * Check if given Contract is not null or invalid
 *
 * @param contract the Contract to check
 * @param <T>      the deliverable type
 * @return a valid contract
 * @throws IllegalArgumentException when invalid
 */
export const contractCheck: <T>(contract: OptionalType<T>)
  => RequiredType<T>
  = <T>(contract: OptionalType<T>) => {
    return presentCheck(contract, "Contract must be present.");
  }
 
/**
 * Check if given Contracts is not null or invalid
 * @param contracts the Contracts to check
 * @return a valid Contracts
 */
export const contractsCheck: <T>(contracts: OptionalType<T>)
  => RequiredType<T>
  = <T>(contracts: OptionalType<T>) => {
    return presentCheck(contracts, "Contracts must be present.");
  }
 
/**
 * Check if given Promisor is not null or invalid
 *
 * @param promisor the Promisor to check
 * @param <T>      the deliverable type
 * @return a valid promisor
 * @throws IllegalArgumentException when invalid
 */
export const promisorCheck: <T>(promisor: OptionalType<T>)
  => RequiredType<T>
  = <T>(promisor: OptionalType<T>) => {
    return presentCheck(promisor, "Promisor must be present.");
  }
 
/**
 * Check if given config is not null or invalid
 *
 * @param config the config to check
 * @param <T>      the type of config
 * @return a valid config
 * @throws IllegalArgumentException when invalid
 */
export const configCheck: <T>(config: OptionalType<T>)
  => RequiredType<T>
  = <T>(config: OptionalType<T>) => {
    return presentCheck(config, "Config must be present.");
  }
 
/**
 * Check if given type is not null or invalid
 *
 * @param {*} type the type to check
 * @returns a valid type
 */
export const typeCheck: <T>(name: OptionalType<T>)
  => RequiredType<T>
  = <T>(name: OptionalType<T>) => {
    return presentCheck(name, "Type must be present.");
  }
 
/**
 * Check if given name is not null or invalid
 *
 * @param {*} name the name to check
 * @returns a valid name
 */
export const nameCheck: <T>(name: OptionalType<T>)
  => RequiredType<T>
  = <T>(name: T) => {
    return presentCheck(name, "Name must be present.");
  }
 
/**
 * Check if given message is not null or invalid
 * 
 * @param {*} value the message to check
 * @returns a valid message
 */
export const messageCheck: <T>(value: OptionalType<T>)
  => RequiredType<T>
  = <T>(value: OptionalType<T>) => {
    return presentCheck(value, "Message must be present.");
  }
 
/**
 * Check if given instance is not null
 * 
 * @param {*} value the instance to check
 * @param {*} message the message used if an exception is thrown
 * @returns the value passed
 */
export const presentCheck: <T>(value: OptionalType<T>, message: string)
  => RequiredType<T>
  = <T>(value: OptionalType<T>, message: string) => {
    return illegalCheck(value, isNotPresent(value), message) as RequiredType<T>;
  };
 
/**
* Check if given instance is not null
* 
* @param {*} value the instance to check
* @param {*} failed if true an IllegalArgumentException is thrown
* @param {*} message the message used if an exception is thrown
* @returns the value passed in
*/
export const illegalCheck: <T>(value: T, failed: boolean, message: string)
  => T
  = <T>(value: T, failed: boolean, message: string) => {
    if (isNotPresent(message)) {
      throw new IllegalArgumentException("Message for illegal check must be present.");
    }
    if (failed) {
      throw new IllegalArgumentException(message);
    }
    return value;
  };
 
/**
 * explicity mark a value as used to avoid compiler warnings
 * useful for "using" variable which are used for disposal or other side-effects
 * but not directly referenced in code.
 * 
 * @param value the value which you wish to declare as used
 */
export const used: (value: unknown) 
  => void 
  = (value: unknown) => { void value; }