All files / src/impl Internal.impl.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 4/4
100% Lines 15/15

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 4726x 26x         26x                 285x 284x 284x 985x   284x 985x 985x                       3x 2x     3x     1x   1x      
import { ContractException } from "@jonloucks/contracts-ts/api/ContractException";
import { presentCheck } from "@jonloucks/contracts-ts/auxiliary/Checks";
 
/**
 * Helper functions for internal implementations.
 */
export const Internal = {
 
  /**
   * Iterate over a Map in reverse order, invoking the callback for each entry.
   * 
   * @param map the Map to iterate over
   * @param callback the callback to invoke for each entry
   */
  mapForEachReversed<K, V>(map: Map<K, V>, callback: (key: K, value: V) => void): void {
    const validMap = presentCheck(map, "Map must be present.");
    const entries: [K, V][] = [];
    validMap.forEach((value, key) => {
      entries.push([key, value]);
    });
    while (entries.length > 0) {
      const entry = entries.pop()!;
      callback(entry[0], entry[1]);
    }
  },
 
  /**
   * Throws an AggregateError with the provided message and list of errors.
   * If only one error is provided, it throws that error directly.
   * 
   * @param message the message for the AggregateError
   * @param errorList the list of errors to include
   */
  throwAggregateError(message: string, ...errorList: unknown[]): never {
    if (errorList.length === 1) {
      throw errorList[0];
    } else {
      // Map each error object to its message property
      const messages = errorList.map(error => `- ${error instanceof Error ? error.message : String(error)}`);
 
      // Join the messages with a newline separator
      const messagesJoined = messages.join('\n');
 
      throw new ContractException(message + "\n" + messagesJoined);
    }
  }
}