utils.js

/**
 * @module utils
 * @description A set of utilities
 */

/**
 * Sum the values of the array (must be numbers).
 *
 * @static
 * @method sum
 *
 * @param array {Array} array of number values
 * @return {Number} the sum of the values in the array
 */
export function sum(array) {
  return (array || []).reduce(function (memo, num) {
    if (typeof num === "number") {
      memo += num;
    }
    return memo;
  }, 0);
}

export function bounded(n, min, max) {
  max = max || Number.MAX_VALUE; // have to have a min
  return n < min ? min : n > max ? max : n;
}

export function is(obj, type) {
  return Object.prototype.toString.call(obj) === "[object " + type + "]";
}

// All create* methods should log with this for consistent, useful data.
class Logger {
  constructor() {
    this.enabled = true;
  }
  disable() {
    this.enabled = false;
  }
  start(name, params = {}) {
    if (this.enabled) {
      console.group(name, params);
    }
  }
  end(obj) {
    if (this.enabled) {
      console.groupEnd();
    }
    return obj;
  }
}
export var logger = new Logger();