Code coverage report for lib/functions.js

Statements: 100% (59 / 59)      Branches: 100% (35 / 35)      Functions: 100% (11 / 11)      Lines: 100% (59 / 59)      Ignored: none     

All files » lib/ » functions.js
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 1761                       1 3   3 2     1 1   3                   1 3   3 2     1 1   3                     1   1 8     4   4 1   3 1 1 1   2 1 1     1 1 1   4                 1   3 3                 1 2   2 1     1 1   2               1 1               1 1               1 1                 1   2     1 1     1 2 2 23   2   2     1                      
var dateFormat = require('dateformat'),
  faker = require('Faker'),
  Nonsense = require('Nonsense'),
  ns = new Nonsense();
 
/**
 * Template function to generate a random integer.
 *
 * @param {Number} min: minimum integer
 * @param {Number} max: maximum integer
 * @param {Function} cb: jazz cb(data) callback
 */
function integer(min, max, cb) {
  var value;
  // template: {integer(min, max)}
  if (min && max && cb) {
    value = ns.integerInRange(min, max);
  // template: {integer()}
  } else {
    cb = min;
    value = ns.integer();
  }
  cb(value);
}
 
/**
 * Template function to generate a random float.
 *
 * @param {Number} min: minimum float
 * @param {Number} max: maximum float
 * @param {Function} cb: jazz cb(data) callback
 */
function _float(min, max, cb) {
  var value;
  // template: {float(min, max)}
  if (min && max && cb) {
    value = ns.realInRange(min, max);
  // template: {float()}
  } else {
    cb = min;
    value = ns.real();
  }
  cb(value);
}
 
/**
 * Template function to generate a random date.
 *
 * @param {String} format: date format (felixge/node-dateformat)
 * @param {Number} min: minimum year
 * @param {Number} max: maximum year
 * @param {Function} cb: jazz cb(data) callback
 */
function date(format, min, max, cb) {
 
  function _timestamp(year) {
    return new Date(year, 0, 1).getTime();
  }
 
  var value;
  // template: {date(format, min, max)}
  if (format && min && max && cb) {
    value = ns.timestamp(_timestamp(min), _timestamp(max));
  // template: {date(min, max)}
  } else if (format && min && max) {
    cb = max;
    value = ns.timestamp(_timestamp(format), _timestamp(min));
    format = 'isoDateTime';
  // template: {date(format)}
  } else if (format && min) {
    cb = min;
    value = ns.timestamp(_timestamp(1970), _timestamp(2020));
  // template: {date()}
  } else {
    cb = format;
    format = 'isoDateTime';
    value = ns.timestamp(_timestamp(1970), _timestamp(2020));
  }
  cb(dateFormat(new Date(value), format));
}
 
/**
 * Template function to select an item out of the arguments.
 * Last argument must be the callback.
 *
 * @param {Function} cb: jazz cb(data) callback
 */
function select() {
  // template: {select(arg1, arg2, ..., argN)}
  var args = Array.prototype.slice.call(arguments);
  args[args.length - 1](ns.pick(args.slice(0, args.length - 1)));
}
 
/**
 * Template function to generate random word(s) from Lorem Ipsum.
 *
 * @param {Number} num: how many random words to generate
 * @param {Function} cb: jazz cb(data) callback
 */
function word(num, cb) {
  var value;
  // template: {word(num)}
  if (num && cb) {
    value = ns.words(num);
  // template: {word()}
  } else {
    cb = num;
    value = ns.word();
  }
  cb(value);
}
 
/**
 * Template function to generate a random first name.
 *
 * @param {Function} cb: jazz cb(data) callback
 */
function firstName(cb) {
  cb(ns.firstName());
}
 
/**
 * Template function to generate a random last name.
 *
 * @param {Function} cb: jazz cb(data) callback
 */
function lastName(cb) {
  cb(ns.lastName());
}
 
/**
 * Template function to generate a random email.
 *
 * @param {Function} cb: jazz cb(data) callback
 */
function email(cb) {
  cb(faker.Internet.email());
}
 
/**
 * Template function to generate a random phone.
 *
 * @param {String} format: phone number format, e.g. #### #### will generate 8 digit numbers separated with a space
 * @param {Function} cb: jazz cb(data) callback
 */
function phone(format, cb) {
  // template: {phone(format)}
  if (format && cb) {
  // template: {phone()}
  } else {
    cb = format;
    format = '#### ####';
  }
  // replace with faker.PhoneNumber.phoneNumberFormat(format) if Faker.js > 0.1.3 has been published
  function _phone() {
    var value = '';
    for (var i = 0, ln = format.length; i < ln; i += 1) {
      value += (format.charAt(i) === '#') ? Math.floor(Math.random() * 9) : format.charAt(i);
    }
    return value;
  }
  cb(_phone());
}
 
module.exports = {
  integer: integer,
  float: _float,
  date: date,
  select: select,
  word: word,
  first_name: firstName,
  last_name: lastName,
  email: email,
  phone: phone
};