all files / src/ utils.js

78.43% Statements 40/51
70.97% Branches 22/31
80% Functions 12/15
75% Lines 27/36
2 statements, 2 functions, 7 branches Ignored     
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            4190×                 490×                                               216× 1029× 1029×                       84× 84× 924× 757×     84×                 1051× 1051×                               84×                   1236×                     684×                 552×      
/**
 * Determine if the thing is not undefined and not null.
 *
 * @param {*} thing The thing to test
 * @returns {boolean} True if the thing is not undefined and not null.
 */
export function existy(thing) {
  return thing !== void 0 && thing !== null;
}
 
/**
 * Is this a function?
 *
 * @param {*} x The variable to test
 * @returns {boolean} True if the variable is a function
 */
export function isFunction(x) {
  return 'function' === typeof x;
}
 
/**
 * Loop over each item in an array-like value.
 *
 * @param {Array<*>} arr The array to loop over
 * @param {Function} fn The function to call
 * @param {?Object} target The object to bind to the function
 */
export function each(arr, fn, target) {
  let i;
  const len = (arr && arr.length) || 0;
  for (i = 0; i < len; i++) {
    fn.call(target, arr[i], i);
  }
}
 
/**
 * Loop over each key/value pair in a hash.
 *
 * @param {Object} obj The object
 * @param {Function} fn The function to call
 * @param {?Object} target The object to bind to the function
 */
export function eachKey(obj, fn, target) {
  for (let key in obj) {
    Eif (obj.hasOwnProperty(key)) {
      fn.call(target, key, obj[key]);
    }
  }
}
 
/**
 * Set default options where some option was not specified.
 *
 * @param {Object} options The destination
 * @param {Object} _defaults The defaults
 * @returns {Object}
 */
export function defaults(options, _defaults) {
  options = options || {};
  eachKey(_defaults, function(key, val) {
    if (!existy(options[key])) {
      options[key] = val;
    }
  });
  return options;
}
 
/**
 * Convert value (e.g., a NodeList) to an array.
 *
 * @param {*} obj The object
 * @returns {Array<*>}
 */
export function toArray(obj) {
  try {
    return Array.prototype.slice.call(obj);
  } catch (e) {
    const ret = [];
    each(obj, function(val) {
      ret.push(val);
    });
    return ret;
  }
}
 
/**
 * Get the last item in an array
 *
 * @param {Array<*>} array The array
 * @returns {*} The last item in the array
 */
export function last(array) {
  return array[array.length - 1];
}
 
/**
 * Test if token is a script tag.
 *
 * @param {Object} tok The token
 * @param {String} tag The tag name
 * @returns {boolean} True if the token is a script tag
 */
export function isTag(tok, tag) {
  return !tok ||
    !(tok.type === 'startTag' || tok.type === 'atomicTag') ||
    !('tagName' in tok) ? !1 : !!~tok.tagName.toLowerCase().indexOf(tag);
}
 
/**
 * Test if token is a script tag.
 *
 * @param {Object} tok The token
 * @returns {boolean} True if the token is a script tag
 */
export function isScript(tok) {
  return isTag(tok, 'script');
}
 
/**
 * Test if token is a style tag.
 *
 * @param {Object} tok The token
 * @returns {boolean} True if the token is a style tag
 */
export function isStyle(tok) {
  return isTag(tok, 'style');
}