seed.js

/**
 * Seed utilities for testing and randomness control. These set values and can be
 * overridden in tests to provide predictable results.
 * @module seed
 */

/**
 * Returns a random value (<code>Math.random()</code> by default).
 * @function random
 * @returns {Number} a random number between 0 and 1.0. This can
 * be overridden in tests to control for randomness.
 *
 * @example
 * seed.random()
 * => 0.42480169934972056
 */
function random() {
  return Math.random();
}

/**
 * Returns a long value (<code>new Date().getTime()</code> by default).
 * @function timestamp
 * @returns {Number} milliseconds since the epoch. This can be
 * overridden in tests to control for time.
 *
 * @example
 * seed.timestamp()
 * => 1571246424383
 */
function timestamp() {
  return new Date().getTime();
}

/**
 * Restores the seed functions to their default implementations.
 * @function restore
 *
 * @example
 * seed.restore()
 */
function restore() {
  seed.random = random;
  seed.timestamp = timestamp;
}

var seed = {
  random: random,
  timestamp: timestamp,
  restore: restore,
};

export { seed };