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
|
import type { Faker } from '.';
import type { Helpers } from './helpers';
/**
* Module to generate random texts and words.
*/
export class Lorem {
private readonly Helpers: Helpers;
constructor(private readonly faker: Faker) {
this.Helpers = faker.helpers;
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(Lorem.prototype)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}
/**
* Generates a word of a specified length.
*
* @param length length of the word that should be returned. Defaults to a random length.
*
* @example
* faker.lorem.word() // 'temporibus'
* faker.lorem.word(5) // 'velit'
*/
word(length?: number): string {
const hasRightLength = (word: string) => word.length === length;
let properLengthWords: readonly string[];
if (typeof length === 'undefined') {
properLengthWords = this.faker.definitions.lorem.words;
} else {
properLengthWords =
this.faker.definitions.lorem.words.filter(hasRightLength);
}
return this.faker.random.arrayElement(properLengthWords);
}
/**
* Generates a space separated list of words.
*
* @param num The number of words to generate. Defaults to `3`.
*
* @example
* faker.lorem.words() // 'qui praesentium pariatur'
* faker.lorem.words(10) // 'debitis consectetur voluptatem non doloremque ipsum autem totam eum ratione'
*/
words(num?: number): string {
if (typeof num == 'undefined') {
num = 3;
}
const words: string[] = [];
for (let i = 0; i < num; i++) {
words.push(this.faker.lorem.word());
}
return words.join(' ');
}
/**
* Generates a space separated list of words beginning a capital letter and ending with a dot.
*
* @param wordCount The number of words, that should be in the sentence. Defaults to a random number between `3` and `10`.
* @param range Currently this parameter does nothing.
*
* @example
* faker.lorem.sentence() // 'Voluptatum cupiditate suscipit autem eveniet aut dolorem aut officiis distinctio.'
* faker.lorem.sentence(5) // 'Laborum voluptatem officiis est et.'
*/
// TODO @Shinigami92 2022-01-11: `range` is not in use
sentence(wordCount?: number, range?: number): string {
if (typeof wordCount == 'undefined') {
wordCount = this.faker.datatype.number({ min: 3, max: 10 });
}
// if (typeof range == 'undefined') { range = 7; }
// strange issue with the node_min_test failing for capitalize, please fix and add faker.lorem.back
//return faker.lorem.words(wordCount + Helpers.randomNumber(range)).join(' ').capitalize();
const sentence = this.faker.lorem.words(wordCount);
return sentence.charAt(0).toUpperCase() + sentence.slice(1) + '.';
}
/**
* Generates a slugified text consisting of the given number of hyphen separated words.
*
* @param wordCount The number of words to generate. Defaults to `3`.
*
* @example
* faker.lorem.slug() // 'dolores-illo-est'
*/
slug(wordCount?: number): string {
const words = this.faker.lorem.words(wordCount);
return this.Helpers.slugify(words);
}
/**
* Generates the given number of sentences.
*
* @param sentenceCount The number of sentences to generate. Defaults to a random number between `2` and `6`.
* @param separator The separator to add between sentences. Defaults to `' '`.
*
* @example
* faker.lorem.sentences() // 'Iste molestiae incidunt aliquam possimus reprehenderit eum corrupti. Deleniti modi voluptatem nostrum ut esse.'
* faker.lorem.sentences(2) // 'Maxime vel numquam quibusdam. Dignissimos ex molestias quos aut molestiae quam nihil occaecati maiores.'
* faker.lorem.sentences(2, '\n')
* // 'Et rerum a unde tempora magnam sit nisi.
* // Et perspiciatis ipsam omnis.'
*/
sentences(sentenceCount?: number, separator?: string): string {
if (typeof sentenceCount === 'undefined') {
sentenceCount = this.faker.datatype.number({ min: 2, max: 6 });
}
if (typeof separator === 'undefined') {
separator = ' ';
}
const sentences: string[] = [];
for (sentenceCount; sentenceCount > 0; sentenceCount--) {
sentences.push(this.faker.lorem.sentence());
}
return sentences.join(separator);
}
/**
* Generates a paragraph with at least the given number of sentences.
*
* @param sentenceCount The minim number of sentences to generate. Defaults to `3`.
*
* @example
* faker.lorem.paragraph() // 'Non architecto nam unde sint. Ex tenetur dolor facere optio aut consequatur. Ea laudantium reiciendis repellendus.'
* faker.lorem.paragraph() // 'Animi possimus nemo consequuntur ut ea et tempore unde qui. Quis corporis esse occaecati.'
*/
paragraph(sentenceCount: number = 3): string {
return this.faker.lorem.sentences(
sentenceCount + this.faker.datatype.number(3)
);
}
/**
* Generates the given number of paragraphs.
*
* @param paragraphCount The number of paragraphs to generate. Defaults to `3`.
* @param separator The separator to use. Defaults to `'\n \r'`.
*
* @example
* faker.lorem.paragraphs()
* // 'Beatae voluptatem dicta et assumenda fugit eaque quidem consequatur. Fuga unde provident. Id reprehenderit soluta facilis est laborum laborum. Illum aut non ut. Est nulla rem ipsa.
* // Voluptatibus quo pariatur est. Temporibus deleniti occaecati pariatur nemo est molestias voluptas. Doloribus commodi et et exercitationem vel et. Omnis inventore cum aut amet.
* // Sapiente deleniti et. Ducimus maiores eum. Rem dolorem itaque aliquam.'
*
* faker.lorem.paragraphs(5)
* // 'Quia hic sunt ducimus expedita quo impedit soluta. Quam impedit et ipsum optio. Unde dolores nulla nobis vero et aspernatur officiis.
* // Aliquam dolorem temporibus dolores voluptatem voluptatem qui nostrum quia. Sit hic facilis rerum eius. Beatae doloribus nesciunt iste ipsum.
* // Natus nam eum nulla voluptas molestiae fuga libero nihil voluptatibus. Sed quam numquam eum ipsam temporibus eaque ut et. Enim quas debitis quasi quis. Vitae et vitae.
* // Repellat voluptatem est laborum illo harum sed reprehenderit aut. Quo sit et. Exercitationem blanditiis totam velit ad dicta placeat.
* // Rerum non eum incidunt amet quo. Eaque laborum ut. Recusandae illo ab distinctio veritatis. Cum quis architecto ad maxime a.'
*
* faker.lorem.paragraphs(2, '<br/>\n')
* // 'Eos magnam aut qui accusamus. Sapiente quas culpa totam excepturi. Blanditiis totam distinctio occaecati dignissimos cumque atque qui officiis.<br/>
* // Nihil quis vel consequatur. Blanditiis commodi deserunt sunt animi dolorum. A optio porro hic dolorum fugit aut et sint voluptas. Minima ad sed ipsa est non dolores.'
*/
// TODO ST-DDT 2022-02-09: The separator looks odd.
paragraphs(paragraphCount: number = 3, separator: string = '\n \r'): string {
const paragraphs: string[] = [];
for (paragraphCount; paragraphCount > 0; paragraphCount--) {
paragraphs.push(this.faker.lorem.paragraph());
}
return paragraphs.join(separator);
}
/**
* Generates a random text based on a random lorem method.
*
* @param times This parameter does nothing.
*
* @example
* faker.lorem.text() // 'Doloribus autem non quis vero quia.'
* faker.lorem.text()
* // 'Rerum eum reiciendis id ipsa hic dolore aut laborum provident.
* // Quis beatae quis corporis veritatis corrupti ratione delectus sapiente ut.
* // Quis ut dolor dolores facilis possimus tempore voluptates.
* // Iure nam officia optio cumque.
* // Dolor tempora iusto.'
*/
// TODO @Shinigami92 2022-01-11: `times` is not in use
text(times?: number): string {
const loremMethods = [
'lorem.word',
'lorem.words',
'lorem.sentence',
'lorem.sentences',
'lorem.paragraph',
'lorem.paragraphs',
'lorem.lines',
];
const randomLoremMethod = this.faker.random.arrayElement(loremMethods);
return this.faker.fake(`{{${randomLoremMethod}}}`);
}
/**
* Generates the given number lines of lorem separated by `'\n'`.
*
* @param lineCount The number of lines to generate. Defaults to a random number between `1` and `5`.
*
* @example
* faker.lorem.lines()
* // 'Rerum quia aliquam pariatur explicabo sint minima eos.
* // Voluptatem repellat consequatur deleniti qui quibusdam harum cumque.
* // Enim eveniet a qui.
* // Consectetur velit eligendi animi nostrum veritatis.'
*
* faker.lorem.lines()
* // 'Soluta deserunt eos quam reiciendis libero autem enim nam ut.
* // Voluptate aut aut.'
*/
lines(lineCount?: number): string {
if (typeof lineCount === 'undefined') {
lineCount = this.faker.datatype.number({ min: 1, max: 5 });
}
return this.faker.lorem.sentences(lineCount, '\n');
}
}
|