Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

lorem.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * @fileOverview Generates "Lorem ipsum" style text.
  3. * @author rviscomi@gmail.com Rick Viscomi,
  4. * tinsley@tinsology.net Mathew Tinsley
  5. * @version 1.0
  6. */
  7. /**
  8. * Copyright (c) 2009, Mathew Tinsley (tinsley@tinsology.net)
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the organization nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY MATHEW TINSLEY ''AS IS'' AND ANY
  23. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /**
  34. * @class Jibborish generator.
  35. */
  36. var LoremIpsum = function () {};
  37. /**
  38. * Average number of words per sentence.
  39. * @constant {number}
  40. */
  41. LoremIpsum.WORDS_PER_SENTENCE_AVG = 24.460;
  42. /**
  43. * Standard deviation of the number of words per sentence.
  44. * @constant {number}
  45. */
  46. LoremIpsum.WORDS_PER_SENTENCE_STD = 5.080;
  47. /**
  48. * List of possible words.
  49. * @constant {Array.string}
  50. */
  51. LoremIpsum.WORDS = [
  52. 'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur',
  53. 'adipiscing', 'elit', 'curabitur', 'vel', 'hendrerit', 'libero',
  54. 'eleifend', 'blandit', 'nunc', 'ornare', 'odio', 'ut',
  55. 'orci', 'gravida', 'imperdiet', 'nullam', 'purus', 'lacinia',
  56. 'a', 'pretium', 'quis', 'congue', 'praesent', 'sagittis',
  57. 'laoreet', 'auctor', 'mauris', 'non', 'velit', 'eros',
  58. 'dictum', 'proin', 'accumsan', 'sapien', 'nec', 'massa',
  59. 'volutpat', 'venenatis', 'sed', 'eu', 'molestie', 'lacus',
  60. 'quisque', 'porttitor', 'ligula', 'dui', 'mollis', 'tempus',
  61. 'at', 'magna', 'vestibulum', 'turpis', 'ac', 'diam',
  62. 'tincidunt', 'id', 'condimentum', 'enim', 'sodales', 'in',
  63. 'hac', 'habitasse', 'platea', 'dictumst', 'aenean', 'neque',
  64. 'fusce', 'augue', 'leo', 'eget', 'semper', 'mattis',
  65. 'tortor', 'scelerisque', 'nulla', 'interdum', 'tellus', 'malesuada',
  66. 'rhoncus', 'porta', 'sem', 'aliquet', 'et', 'nam',
  67. 'suspendisse', 'potenti', 'vivamus', 'luctus', 'fringilla', 'erat',
  68. 'donec', 'justo', 'vehicula', 'ultricies', 'varius', 'ante',
  69. 'primis', 'faucibus', 'ultrices', 'posuere', 'cubilia', 'curae',
  70. 'etiam', 'cursus', 'aliquam', 'quam', 'dapibus', 'nisl',
  71. 'feugiat', 'egestas', 'class', 'aptent', 'taciti', 'sociosqu',
  72. 'ad', 'litora', 'torquent', 'per', 'conubia', 'nostra',
  73. 'inceptos', 'himenaeos', 'phasellus', 'nibh', 'pulvinar', 'vitae',
  74. 'urna', 'iaculis', 'lobortis', 'nisi', 'viverra', 'arcu',
  75. 'morbi', 'pellentesque', 'metus', 'commodo', 'ut', 'facilisis',
  76. 'felis', 'tristique', 'ullamcorper', 'placerat', 'aenean', 'convallis',
  77. 'sollicitudin', 'integer', 'rutrum', 'duis', 'est', 'etiam',
  78. 'bibendum', 'donec', 'pharetra', 'vulputate', 'maecenas', 'mi',
  79. 'fermentum', 'consequat', 'suscipit', 'aliquam', 'habitant', 'senectus',
  80. 'netus', 'fames', 'quisque', 'euismod', 'curabitur', 'lectus',
  81. 'elementum', 'tempor', 'risus', 'cras'
  82. ];
  83. /**
  84. * Generate "Lorem ipsum" style words.
  85. * @param num_words {number} Number of words to generate.
  86. * @return {string} "Lorem ipsum..."
  87. */
  88. LoremIpsum.prototype.generate = function (num_words) {
  89. var words, ii, position, word, current, sentences, sentence_length, sentence;
  90. /**
  91. * @default 100
  92. */
  93. num_words = num_words || 100;
  94. words = [LoremIpsum.WORDS[0], LoremIpsum.WORDS[1]];
  95. num_words -= 2;
  96. for (ii = 0; ii < num_words; ii++) {
  97. position = Math.floor(Math.random() * LoremIpsum.WORDS.length);
  98. word = LoremIpsum.WORDS[position];
  99. if (ii > 0 && words[ii - 1] === word) {
  100. ii -= 1;
  101. } else {
  102. words[ii] = word;
  103. }
  104. }
  105. sentences = [];
  106. current = 0;
  107. while (num_words > 0) {
  108. sentence_length = this.getRandomSentenceLength();
  109. if (num_words - sentence_length < 4) {
  110. sentence_length = num_words;
  111. }
  112. num_words -= sentence_length;
  113. sentence = [];
  114. for (ii = current; ii < (current + sentence_length); ii++) {
  115. sentence.push(words[ii]);
  116. }
  117. sentence = this.punctuate(sentence);
  118. current += sentence_length;
  119. sentences.push(sentence.join(' '));
  120. }
  121. return sentences.join(' ');
  122. };
  123. /**
  124. * Insert commas and periods in the given sentence.
  125. * @param {Array.string} sentence List of words in the sentence.
  126. * @return {Array.string} Sentence with punctuation added.
  127. */
  128. LoremIpsum.prototype.punctuate = function (sentence) {
  129. var word_length, num_commas, ii, position;
  130. word_length = sentence.length;
  131. /* End the sentence with a period. */
  132. sentence[word_length - 1] += '.';
  133. if (word_length < 4) {
  134. return sentence;
  135. }
  136. num_commas = this.getRandomCommaCount(word_length);
  137. for (ii = 0; ii <= num_commas; ii++) {
  138. position = Math.round(ii * word_length / (num_commas + 1));
  139. if (position < (word_length - 1) && position > 0) {
  140. /* Add the comma. */
  141. sentence[position] += ',';
  142. }
  143. }
  144. /* Capitalize the first word in the sentence. */
  145. sentence[0] = sentence[0].charAt(0).toUpperCase() + sentence[0].slice(1);
  146. return sentence;
  147. };
  148. /**
  149. * Produces a random number of commas.
  150. * @param {number} word_length Number of words in the sentence.
  151. * @return {number} Random number of commas
  152. */
  153. LoremIpsum.prototype.getRandomCommaCount = function (word_length) {
  154. var base, average, standard_deviation;
  155. /* Arbitrary. */
  156. base = 6;
  157. average = Math.log(word_length) / Math.log(base);
  158. standard_deviation = average / base;
  159. return Math.round(this.gaussMS(average, standard_deviation));
  160. };
  161. /**
  162. * Produces a random sentence length based on the average word length
  163. * of an English sentence.
  164. * @return {number} Random sentence length
  165. */
  166. LoremIpsum.prototype.getRandomSentenceLength = function () {
  167. return Math.round(
  168. this.gaussMS(
  169. LoremIpsum.WORDS_PER_SENTENCE_AVG,
  170. LoremIpsum.WORDS_PER_SENTENCE_STD
  171. )
  172. );
  173. };
  174. /**
  175. * Produces a random number.
  176. * @return {number} Random number
  177. */
  178. LoremIpsum.prototype.gauss = function () {
  179. return (Math.random() * 2 - 1) +
  180. (Math.random() * 2 - 1) +
  181. (Math.random() * 2 - 1);
  182. };
  183. /**
  184. * Produces a random number with Gaussian distribution.
  185. * @param {number} mean
  186. * @param {number} standard_deviation
  187. * @return {number} Random number
  188. */
  189. LoremIpsum.prototype.gaussMS = function (mean, standard_deviation) {
  190. return Math.round(this.gauss() * standard_deviation + mean);
  191. };
  192. module.exports = {
  193. LoremIpsum
  194. }