/**
 * @module tgam-patterns-utils
 */

/**
 * @method toArray
 * @description converts an array like (nodelist) to an array
 * @param {Array|NodeList} val
 * @return {Array}
 */
export function toArray(val) {
  if (!isDefined(val)) {
    return null;
  } else if (isDefined(Array.from)) {
    return Array.from(val);
  } else {
    return [].slice.call(val);
  }
}

/**
 * @method isDefined
 * @description whether variable is not undefined or null and not an empty string
 * @param {*} val
 * @return {boolean}
 */
export function isDefined(val) {
  return (typeof val !== "undefined" && val !== null) && (!isEmpty(val));
}

/**
 * @method isEmpty
 * @description whether string is empty or has no visible characters or array has a length of 0
 * @param {*} val
 * @return {boolean}
 */
export function isEmpty(val) {
  return typeof val === "undefined" || (typeof val === "string" && val.trim() === "") || (Array.isArray(val) && val.length === 0);
}
