All files / src/auxiliary Checks.ts

100% Statements 37/37
100% Branches 0/0
94.11% Functions 16/17
100% Lines 34/34

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 16229x 29x   66x                 29x 850x                   29x 70x                   29x 484x                   29x 670x                   29x 3x                   29x 7x 5x 4x 3x                   29x 24x                   29x 55x                   29x 3x                   29x 3x                   29x 1x                   29x 3x                   29x 136x                   29x   122x  
import { Duration, MAX_TIMEOUT, OptionalType, RequiredType } from "@jonloucks/concurrency-ts/api/Types";
import { illegalCheck, presentCheck, configCheck } from "@jonloucks/contracts-ts/auxiliary/Checks";
 
export { presentCheck, illegalCheck, configCheck};
 
/**
 * Check that a state is present
 *
 * @param state the state to check
 * @return the state if present
 * @throws IllegalArgumentException if the state is not present
 */
export function stateCheck<T>(state: T): T {
  return presentCheck(state, "State must be present.");
}
 
/**
 * Check that an event is present
 *
 * @param event the event to check
 * @return the event if present
 * @throws IllegalArgumentException if the event is not present
 */
export function eventCheck(event: string): string {
  return presentCheck(event, "Event must be present.");
}
 
/**
 * Check that a rule is present
 *
 * @param rule the rule to check
 * @return the rule if present
 * @throws IllegalArgumentException if the rule is not present
 */
export function ruleCheck<T>(rule: T): T {
  return presentCheck(rule, "Rule must be present.");
}
 
/**
 * Check that rules are present
 *
 * @param rules the rules to check
 * @return the rules if present
 * @throws IllegalArgumentException if the rules are not present
 */
export function rulesCheck<T>(rules: readonly T[]): readonly T[] {
  return presentCheck(rules, "Rules must be present.");
} 
 
/**
 * Check that a listener is present
 *
 * @param consumer the listener to check
 * @return the listener if present
 * @throws IllegalArgumentException if the listener is not present
 */
export function listenerCheck<T>(consumer: T): T {
  return presentCheck(consumer, "Listener must be present.");
}
 
/**
 * Check that a timeout is valid
 *
 * @param timeout the timeout to check
 * @return the timeout if valid
 * @throws IllegalArgumentException if the timeout is not valid
 */
export function timeoutCheck(timeout: Duration): Duration {
  const presentTimeout = presentCheck(timeout, "Timeout must be present.");
  illegalCheck(timeout, timeout.milliSeconds < 0, "Timeout must not be negative.");
  illegalCheck(timeout, timeout.milliSeconds > MAX_TIMEOUT.milliSeconds, "Timeout must be less than or equal to maximum time.");
  return presentTimeout;
}
 
/**
 * Check that a completion value is present
 *
 * @param completion the completion value to check
 * @return the completion value if present
 * @throws IllegalArgumentException if the completion value is not present
 */
export function completionCheck<T>(completion: OptionalType<T>): RequiredType<T> {
  return presentCheck(completion, "Completion must be present.");
}
 
/**
 * Check that an onCompletion consumer is present
 *
 * @param onCompletion the onCompletion consumer to check
 * @return the onCompletion consumer if present
 * @throws IllegalArgumentException if the onCompletion consumer is not present
 */
export function onCompletionCheck<T>(onCompletion: OptionalType<T>): RequiredType<T> {
  return presentCheck(onCompletion, "OnCompletion consumer must be present.");
} 
 
/**
 * Check that a finally block is present
 *
 * @param block the finally block to check
 * @return the finally block if present
 * @throws IllegalArgumentException if the finally block is not present
 */
export function finallyBlockCheck<T>(block: OptionalType<T>): RequiredType<T> {
  return presentCheck(block, "OnFinally consumer must be present.");
}
 
/**
 * Check that a success block is present
 *
 * @param onSuccess the success block to check
 * @return the success block if present
 * @throws IllegalArgumentException if the success block is not present
 */
export function successBlockCheck<T>(onSuccess: OptionalType<T>): RequiredType<T> {
  return presentCheck(onSuccess, "OnSuccess consumer must be present.");
}
 
/**
 * Check that a failure block is present
 *
 * @param onFailure the failure block to check
 * @return the failure block if present
 * @throws IllegalArgumentException if the failure block is not present
 */
export function failureBlockCheck<T>(onFailure: OptionalType<T>): RequiredType<T> {
  return presentCheck(onFailure, "OnFailure consumer must be present.");
}
 
/**
 * Check that a predicate is present
 *
 * @param predicate the predicate to check
 * @return the predicate if present
 * @throws IllegalArgumentException if the predicate is not present
 */
export function predicateCheck<T>(predicate: OptionalType<T>): RequiredType<T> {
  return presentCheck(predicate, "Predicate must be present.");
}
 
/**
 * Check that an initial value is present
 *
 * @param initialValue the initial value to check
 * @return the initial value if present
 * @throws IllegalArgumentException if the initial value is not present
 */
export function initialValueCheck<T>(initialValue: OptionalType<T>): RequiredType<T> {
  return presentCheck(initialValue, "Initial value must be present.");
}
 
/**
 * explicitly 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; }