All files / ramekin/tests DocumentGenerator.js

0% Statements 0/16
100% Branches 0/0
0% Functions 0/4
0% Lines 0/15

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                                                                                       
const moment = require('moment')
 
/**
 * Generates a date in the last n days
 */
function * generateDate (n) {
  while (true) {
    // generate a random number in the last n days
    let daysAgo = Math.floor(Math.random() * n) + 1
    const docDate = moment().subtract(daysAgo, 'days')
    yield docDate.toDate()
  }
}
 
/**
 * Generates a date in the last n days
 */
function * generateTextSequence (options) {
  while (true) {
    // generate number of words:
    let length = Math.floor((Math.random() * options.maxSentenceLength) + options.minSentenceLength)
    // generate a random number in the last n days
    yield Array.from({length: length}, () => options.vocab[Math.floor(Math.random() * options.vocab.length)])
  }
}
 
var generateDocument = function * (options) {
  // setup generators for dates, subjects and text.
  let dateGen = generateDate(options.historyDays)
  let bodyGen = generateTextSequence(options)
 
  while (true) {
    let doc = {
      _id: Math.random().toString(),
      date: dateGen.next().value,
      subject: options.subjects[ Math.floor(Math.random() * options.subjects.length) ],
      body: bodyGen.next().value.join(' ')
    }
    yield doc
  }
}
 
module.exports = generateDocument