All files / src/modules random.js

100% Statements 250/250
100% Branches 67/67
100% Functions 16/16
100% Lines 250/250

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 2511x 1x 1x 1x 1x 1x 48x 48x 48x 48x 48x 48x 48x 1x 1x 48x 48x 48x 1x 1x 3991x 3991x 3991x 4x 4x 3991x 3991x 3774x 3774x 3991x 3991x 3933x 3933x 3991x 3991x 1x 3991x 1x 1x 3991x 3991x 3991x 3991x 3991x 9x 3991x 50x 50x 50x 50x 50x 50x 3x 50x 1x 1x 3982x 3932x 3932x 3991x 3991x 3991x 1x 1x 7x 7x 7x 1x 1x 1x 1x 7x 7x 7x 7x 7x 1x 1x 1547x 1547x 1547x 1547x 1x 1x 5x 5x 5x 2x 5x 1x 3x 1x 1x 5x 5x 5x 5x 5x 8x 8x 8x 5x 5x 5x 1x 1x 31x 31x 31x 31x 31x 1x 1x 2x 2x 2x 4x 4x 2x 2x 2x 1x 1x 4x 4x 4x 4x 1x 4x 2x 2x 2x 2x 2x 2x 2x 3x 1x 1x 4x 4x 4x 1x 1x 14x 14x 1x 1x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 1x 1x 5x 5x 5x 5x 11x 11x 5x 5x 5x 1x 1x 1x 1x 1x 1x 33x 33x 33x 26x 26x 26x 31x 1x 1x 1x 6x 1x 1x 33x 33x 27x 27x 33x 33x 33x 33x 43x 43x 33x 33x 33x 1x 1x 4x 4x 4x 17x 17x 4x 4x 4x 1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 1x  
import * as uuid from 'uuid';
import lfib from '../vendor/lfib.js';
import * as constants from '../constants.js';
 
export class Random {
  constructor(pure, seed) {
    this.pure = pure;
    this.seed = seed;
    this.num = this.seed || new Date().getTime() % 1000000000;
 
    // Preserve past behavior and fall back to the default time-based seed if an
    // empty array is passed.
    if (Array.isArray(this.num) && !this.num.length) {
      this.num = new Date().getTime() % 1000000000;
    }
 
    this.lfibGen = lfib(this.num);
  }
 
  number(options = {}) {
    const def = typeof options === 'number' ? { max: options } : options;
 
    if (typeof def.max === 'undefined') {
      def.max = 99999;
    }
 
    if (typeof def.min === 'undefined') {
      def.min = def.max < 0 ? -99999 : 0;
    }
 
    if (typeof def.precision === 'undefined') {
      def.precision = 0;
    }
 
    if (def.precision > 10) {
      def.precision = 10;
    } else if (def.precision < 0) {
      def.precision = 1;
    }
 
    let result = '';
    const randomNumber = Math.round(this.lfibGen() * (def.max - def.min) + def.min);
 
    if (def.min === def.max) {
      result = def.max;
    } else if (def.precision >= 1) {
      const template = this.pure.helpers.repeatString({ string: '#', num: def.precision });
      result = parseFloat(
        `${randomNumber}.${this.pure.helpers.replaceSymbolWithNumber({ string: template })}`
      );
 
      if (result > def.max) {
        result = def.max;
      } else if (result < def.min) {
        result = def.min;
      }
    } else {
      result = randomNumber;
    }
 
    return result;
  }
 
  float(options = {}) {
    let def = options;
 
    if (typeof def === 'number') {
      def = {
        precision: def
      };
    }
 
    def.precision = def.precision || 1;
 
    return this.pure.random.number(def);
  }
 
  arrayElement(array = ['a', 'b', 'c']) {
    const number = this.pure.random.number({ max: array.length });
 
    return array[number] ? array[number] : array[0];
  }
 
  arrayElements(array = ['a', 'b', 'c'], count) {
    let value = count;
 
    if (typeof value !== 'number') {
      value = this.pure.random.number({ min: 1, max: array.length });
    } else if (value > array.length) {
      value = array.length;
    } else if (value < 0) {
      value = 0;
    }
 
    const arrayCopy = array.slice();
    const countToRemove = arrayCopy.length - value;
 
    for (let i = 0; i < countToRemove; i += 1) {
      const indexToRemove = this.number({ max: arrayCopy.length - 1 });
      arrayCopy.splice(indexToRemove, 1);
    }
 
    return arrayCopy;
  }
 
  objectElement(object = { foo: 'bar', too: 'car' }, field) {
    const array = Object.keys(object);
    const key = this.pure.random.arrayElement(array);
 
    return field === 'key' ? key : object[key];
  }
 
  generateObj(length = 2) {
    const obj = {};
 
    while (Object.keys(obj).length < length) {
      obj[this.word()] = this.pure.random.word();
    }
 
    return obj;
  }
 
  uuid(version = 'v1', opts = {}) {
    let generated = '';
    let def = opts;
 
    if (version === 'v4') {
      generated = uuid.v4();
    } else if (version === 'v5') {
      if (typeof opts.name === 'undefined' || typeof opts.namespace === 'undefined') {
        def = {
          name: 'uuid',
          namespace: uuid.v1()
        };
      }
      generated = uuid.v5(def.name, def.namespace);
    } else {
      generated = uuid.v1();
    }
 
    return generated;
  }
 
  boolean() {
    return !!this.pure.random.number(1);
  }
 
  word() {
    // regex statement used to remove unwanted characters from beginning/end of words
    const wordSanitizerRegex = /^[\s()[\]{}.,\-'"]+|[\s()[\]{}.,\-'"]+$/g;
    const wordMethods = [
      'commerce.department',
      'commerce.productName',
      'commerce.productAdjective',
      'commerce.productMaterial',
      'commerce.product',
      'commerce.color',
      'company.catchPhraseAdjective',
      'company.catchPhraseDescriptor',
      'company.catchPhraseNoun',
      'company.bsAdjective',
      'company.bsBuzz',
      'company.bsNoun',
      'address.streetSuffix',
      'address.county',
      'address.country',
      'address.state',
      'finance.accountName',
      'finance.transactionType',
      'finance.currencyName',
      'hacker.noun',
      'hacker.verb',
      'hacker.adjective',
      'hacker.ingverb',
      'hacker.abbreviation',
      'name.jobDescriptor',
      'name.jobArea',
      'name.jobType'
    ];
    // randomly pick from the many pure methods that can generate words
    const randomWordMethod = this.pure.random.arrayElement(wordMethods);
    let result = this.pure.fake.parse(`{{${randomWordMethod}}}`);
    result = this.pure.random.arrayElement(result.split(' '));
 
    return result.replace(wordSanitizerRegex, '');
  }
 
  words(count) {
    const def = count || this.pure.random.number({ min: 1, max: 3 });
    const words = [];
 
    for (let i = 0; i < def; i += 1) {
      words.push(this.pure.random.word());
    }
 
    return words.join(' ');
  }
 
  locale() {
    return this.pure.random.arrayElement(this.pure.possibleLocales);
  }
 
  alpha(options) {
    let def = options;
 
    if (typeof def === 'undefined') {
      def = {
        count: 1
      };
    } else if (typeof def === 'number') {
      def = {
        count: def
      };
    } else if (typeof def.count === 'undefined') {
      def.count = 1;
    }
 
    if (typeof def.upcase === 'undefined') {
      def.upcase = false;
    }
 
    let wholeString = '';
 
    for (let i = 0; i < def.count; i += 1) {
      wholeString += this.pure.random.arrayElement(constants.lowerAlpha);
    }
 
    return def.upcase ? wholeString.toUpperCase() : wholeString;
  }
 
  alphaNumeric(count = 1) {
    let wholeString = '';
 
    for (let i = 0; i < count; i += 1) {
      wholeString += this.pure.random.arrayElement(constants.alphaNum);
    }
 
    return wholeString;
  }
 
  hexaDecimal(count = 1) {
    const template = this.pure.helpers.repeatString({ string: '#', num: count });
 
    return this.pure.helpers.replaceSymbolWithHex({ string: template });
  }
 
  returnSeed() {
    return this.num;
  }
}