lorem.js

  1. /**
  2. *
  3. * @namespace faker.lorem
  4. */
  5. var Lorem = function (faker) {
  6. var self = this;
  7. var Helpers = faker.helpers;
  8. /**
  9. * generates a word of a specified length
  10. *
  11. * @method faker.lorem.word
  12. * @param {number} length length of the word that should be returned. Defaults to a random length
  13. */
  14. self.word = function (length) {
  15. var hasRightLength = function(word) { return word.length === length; };
  16. var properLengthWords;
  17. if(typeof length === 'undefined') {
  18. properLengthWords = faker.definitions.lorem.words;
  19. } else {
  20. properLengthWords = faker.definitions.lorem.words.filter(hasRightLength);
  21. }
  22. return faker.random.arrayElement(properLengthWords);
  23. };
  24. /**
  25. * generates a space separated list of words
  26. *
  27. * @method faker.lorem.words
  28. * @param {number} num number of words, defaults to 3
  29. */
  30. self.words = function (num) {
  31. if (typeof num == 'undefined') { num = 3; }
  32. var words = [];
  33. for (var i = 0; i < num; i++) {
  34. words.push(faker.lorem.word());
  35. }
  36. return words.join(' ');
  37. };
  38. /**
  39. * sentence
  40. *
  41. * @method faker.lorem.sentence
  42. * @param {number} wordCount defaults to a random number between 3 and 10
  43. * @param {number} range
  44. */
  45. self.sentence = function (wordCount, range) {
  46. if (typeof wordCount == 'undefined') { wordCount = faker.random.number({ min: 3, max: 10 }); }
  47. // if (typeof range == 'undefined') { range = 7; }
  48. // strange issue with the node_min_test failing for captialize, please fix and add faker.lorem.back
  49. //return faker.lorem.words(wordCount + Helpers.randomNumber(range)).join(' ').capitalize();
  50. var sentence = faker.lorem.words(wordCount);
  51. return sentence.charAt(0).toUpperCase() + sentence.slice(1) + '.';
  52. };
  53. /**
  54. * slug
  55. *
  56. * @method faker.lorem.slug
  57. * @param {number} wordCount number of words, defaults to 3
  58. */
  59. self.slug = function (wordCount) {
  60. var words = faker.lorem.words(wordCount);
  61. return Helpers.slugify(words);
  62. };
  63. /**
  64. * sentences
  65. *
  66. * @method faker.lorem.sentences
  67. * @param {number} sentenceCount defautls to a random number between 2 and 6
  68. * @param {string} separator defaults to `' '`
  69. */
  70. self.sentences = function (sentenceCount, separator) {
  71. if (typeof sentenceCount === 'undefined') { sentenceCount = faker.random.number({ min: 2, max: 6 });}
  72. if (typeof separator == 'undefined') { separator = " "; }
  73. var sentences = [];
  74. for (sentenceCount; sentenceCount > 0; sentenceCount--) {
  75. sentences.push(faker.lorem.sentence());
  76. }
  77. return sentences.join(separator);
  78. };
  79. /**
  80. * paragraph
  81. *
  82. * @method faker.lorem.paragraph
  83. * @param {number} sentenceCount defaults to 3
  84. */
  85. self.paragraph = function (sentenceCount) {
  86. if (typeof sentenceCount == 'undefined') { sentenceCount = 3; }
  87. return faker.lorem.sentences(sentenceCount + faker.random.number(3));
  88. };
  89. /**
  90. * paragraphs
  91. *
  92. * @method faker.lorem.paragraphs
  93. * @param {number} paragraphCount defaults to 3
  94. * @param {string} separator defaults to `'\n \r'`
  95. */
  96. self.paragraphs = function (paragraphCount, separator) {
  97. if (typeof separator === "undefined") {
  98. separator = "\n \r";
  99. }
  100. if (typeof paragraphCount == 'undefined') { paragraphCount = 3; }
  101. var paragraphs = [];
  102. for (paragraphCount; paragraphCount > 0; paragraphCount--) {
  103. paragraphs.push(faker.lorem.paragraph());
  104. }
  105. return paragraphs.join(separator);
  106. }
  107. /**
  108. * returns random text based on a random lorem method
  109. *
  110. * @method faker.lorem.text
  111. * @param {number} times
  112. */
  113. self.text = function loremText (times) {
  114. var loremMethods = ['lorem.word', 'lorem.words', 'lorem.sentence', 'lorem.sentences', 'lorem.paragraph', 'lorem.paragraphs', 'lorem.lines'];
  115. var randomLoremMethod = faker.random.arrayElement(loremMethods);
  116. return faker.fake('{{' + randomLoremMethod + '}}');
  117. };
  118. /**
  119. * returns lines of lorem separated by `'\n'`
  120. *
  121. * @method faker.lorem.lines
  122. * @param {number} lineCount defaults to a random number between 1 and 5
  123. */
  124. self.lines = function lines (lineCount) {
  125. if (typeof lineCount === 'undefined') { lineCount = faker.random.number({ min: 1, max: 5 });}
  126. return faker.lorem.sentences(lineCount, '\n')
  127. };
  128. return self;
  129. };
  130. module["exports"] = Lorem;