lorem: function()

in external_lib/dummy-json/lib/helpers.js [426:461]


  lorem: function (totalWords, options) {
    var ret = '';
    var i, word;
    var isNewSentence = true;
    var lastPunctuationIndex = 0;

    // Juggle the arguments if totalWords wasn't provided
    if (!options) {
      options = totalWords;
      totalWords = 25;
    }

    for (i = 0; i < totalWords; i++) {
      word = utils.randomArrayItem(options.data.root.lorem);

      // If the last iteration triggered a new sentence then capitalize the first letter
      if (isNewSentence) {
        word = word.charAt(0).toUpperCase() + word.slice(1);
        isNewSentence = false;
      }

      // Only introduce new punctuation if we're more then 3 words away from the end,
      // and more than 3 words since the last punctuation, and a 1 in 3 chance.
      if (i < totalWords - 3 && i - lastPunctuationIndex > 3 && utils.random() < 0.3) {
        isNewSentence = utils.random() < 0.6;
        word = word + (isNewSentence ? '.' : ',');
        lastPunctuationIndex = i;
      }

      ret = ret + word + ' ';
    }

    // Add a period/full-stop at the very end
    ret = ret.trimRight() + '.';
    return ret;
  },