All files / packages/tao/src utils.js

100% Statements 19/19
95% Branches 19/20
100% Functions 4/4
100% Lines 19/19
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      150x 8x   142x 6x   136x         38x 37x 57x 78x 78x 98x   58x 309x       229x       256x               414x 12x   204x 6x      
// taken from http://stackoverflow.com/questions/18884249/checking-whether-something-is-iterable
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';
}
 
// needed a convenience function for this
export function concatIterables(...iterables) {
  let rv = [];
  if (iterables.length) {
    for (let list of iterables) {
      let iterator = list[Symbol.iterator]();
      Eif (list.values && typeof list.values === 'function') {
        iterator = list.values();
      }
      for (let item of iterator) {
        rv = rv.concat(item);
      }
    }
  }
  return rv;
}
 
export function _cleanAC({ t, term, a, action, o, orient }) {
  return {
    term: term || t,
    action: action || a,
    orient: orient || o
  };
}
 
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');
  }
}