All files / tao/src utils.js

100% Statements 10/10
100% Branches 14/14
100% Functions 3/3
100% Lines 10/10

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                        38x 2x   36x 1x   35x                   250x                         189x 6x   183x 6x      
/** @typedef {import('./AppCtxRoot').Trigram} Trigram */
/** @typedef {import('./AppCtxHandlers').Handler} Handler */
 
// taken from http://stackoverflow.com/questions/18884249/checking-whether-something-is-iterable
/**
 * Whether a value is iterable — strings excluded on purpose (a string arg
 * is a trigram part, never a collection).
 * @param {*} obj
 * @returns {obj is Iterable<any>}
 */
export function isIterable(obj) {
  // checks for null and undefined
  if (obj == null) {
    return false;
  }
  if (typeof obj === 'string') {
    return false;
  }
  return typeof obj[Symbol.iterator] === 'function';
}
 
/**
 * Normalize a trigram to long keys only; long keys win when both styles
 * are present. Missing parts stay undefined (wild).
 * @param {Trigram} ac
 * @returns {{term: (string|undefined), action: (string|undefined), orient: (string|undefined)}}
 */
export function _cleanAC({ t, term, a, action, o, orient }) {
  return {
    term: term || t,
    action: action || a,
    orient: orient || o,
  };
}
 
/**
 * Throw unless the handler is a function.
 * @param {Handler} handler
 * @throws {Error} when `handler` is missing or not a function
 */
export function _validateHandler(handler) {
  if (!handler) {
    throw new Error('cannot do anything with missing handler');
  }
  if (typeof handler !== 'function') {
    throw new Error('handler must be a function');
  }
}