-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
index.ts
381 lines (368 loc) · 13.1 KB
/
index.ts
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import type { Faker } from '../..';
import { filterWordListByLength } from '../word/filterWordListByLength';
/**
* Module to generate random texts and words.
*/
export class LoremModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
LoremModule.prototype
) as Array<keyof LoremModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}
/**
* Generates a word of a specified length.
*
* @param options The expected length of the word or the options to use.
* @param options.length The expected length of the word.
* @param options.strategy The strategy to apply when no words with a matching length are found.
*
* Available error handling strategies:
*
* - `fail`: Throws an error if no words with the given length are found.
* - `shortest`: Returns any of the shortest words.
* - `closest`: Returns any of the words closest to the given length.
* - `longest`: Returns any of the longest words.
* - `any-length`: Returns a word with any length.
*
* Defaults to `'any-length'`.
*
* @example
* faker.lorem.word() // 'temporibus'
* faker.lorem.word(5) // 'velit'
* faker.lorem.word({ strategy: 'shortest' }) // 'a'
* faker.lorem.word({ length: { min: 5, max: 7 }, strategy: 'fail' }) // 'quaerat'
*
* @since 3.1.0
*/
word(
options:
| number
| {
/**
* The expected length of the word.
*
* @default 1
*/
length?:
| number
| {
/**
* The minimum length of the word.
*/
min: number;
/**
* The maximum length of the word.
*/
max: number;
};
/**
* The strategy to apply when no words with a matching length are found.
*
* Available error handling strategies:
*
* - `fail`: Throws an error if no words with the given length are found.
* - `shortest`: Returns any of the shortest words.
* - `closest`: Returns any of the words closest to the given length.
* - `longest`: Returns any of the longest words.
* - `any-length`: Returns a word with any length.
*
* @default 'any-length'
*/
strategy?: 'fail' | 'closest' | 'shortest' | 'longest' | 'any-length';
} = {}
): string {
const opts = typeof options === 'number' ? { length: options } : options;
return this.faker.helpers.arrayElement(
filterWordListByLength({
...opts,
wordList: this.faker.definitions.lorem.words,
})
);
}
/**
* Generates a space separated list of words.
*
* @param wordCount The number of words to generate. Defaults to `3`.
* @param wordCount.min The minimum number of words to generate.
* @param wordCount.max The maximum number of words to generate.
*
* @example
* faker.lorem.words() // 'qui praesentium pariatur'
* faker.lorem.words(10) // 'debitis consectetur voluptatem non doloremque ipsum autem totam eum ratione'
* faker.lorem.words({ min: 1, max: 3 }) // 'tenetur error cum'
*
* @since 2.0.1
*/
words(
wordCount:
| number
| {
/**
* The minimum number of words to generate.
*/
min: number;
/**
* The maximum number of words to generate.
*/
max: number;
} = 3
): string {
return this.faker.helpers
.multiple(() => this.word(), { count: wordCount })
.join(' ');
}
/**
* Generates a space separated list of words beginning with a capital letter and ending with a period.
*
* @param wordCount The number of words, that should be in the sentence. Defaults to a random number between `3` and `10`.
* @param wordCount.min The minimum number of words to generate. Defaults to `3`.
* @param wordCount.max The maximum number of words to generate. Defaults to `10`.
*
* @example
* faker.lorem.sentence() // 'Voluptatum cupiditate suscipit autem eveniet aut dolorem aut officiis distinctio.'
* faker.lorem.sentence(5) // 'Laborum voluptatem officiis est et.'
* faker.lorem.sentence({ min: 3, max: 5 }) // 'Fugiat repellendus nisi.'
*
* @since 2.0.1
*/
sentence(
wordCount:
| number
| {
/**
* The minimum number of words to generate.
*/
min: number;
/**
* The maximum number of words to generate.
*/
max: number;
} = { min: 3, max: 10 }
): string {
const sentence = this.words(wordCount);
return `${sentence.charAt(0).toUpperCase() + sentence.substring(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`.
* @param wordCount.min The minimum number of words to generate.
* @param wordCount.max The maximum number of words to generate.
*
* @example
* faker.lorem.slug() // 'dolores-illo-est'
* faker.lorem.slug(5) // 'delectus-totam-iusto-itaque-placeat'
* faker.lorem.slug({ min: 1, max: 3 }) // 'illo-ratione'
*
* @since 4.0.0
*/
slug(
wordCount:
| number
| {
/**
* The minimum number of words to generate.
*/
min: number;
/**
* The maximum number of words to generate.
*/
max: number;
} = 3
): string {
const words = this.words(wordCount);
return this.faker.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 sentenceCount.min The minimum number of sentences to generate. Defaults to `2`.
* @param sentenceCount.max The maximum number of sentences to generate. Defaults to `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.'
* faker.lorem.sentences({ min: 1, max: 3 }) // 'Placeat ex natus tenetur repellendus repellendus iste. Optio nostrum veritatis.'
*
* @since 2.0.1
*/
sentences(
sentenceCount:
| number
| {
/**
* The minimum number of sentences to generate.
*/
min: number;
/**
* The maximum number of sentences to generate.
*/
max: number;
} = { min: 2, max: 6 },
separator: string = ' '
): string {
return this.faker.helpers
.multiple(() => this.sentence(), { count: sentenceCount })
.join(separator);
}
/**
* Generates a paragraph with the given number of sentences.
*
* @param sentenceCount The number of sentences to generate. Defaults to `3`.
* @param sentenceCount.min The minimum number of sentences to generate.
* @param sentenceCount.max The maximum number of sentences to generate.
*
* @example
* faker.lorem.paragraph() // 'Non architecto nam unde sint. Ex tenetur dolor facere optio aut consequatur. Ea laudantium reiciendis repellendus.'
* faker.lorem.paragraph(2) // 'Animi possimus nemo consequuntur ut ea et tempore unde qui. Quis corporis esse occaecati.'
* faker.lorem.paragraph({ min: 1, max: 3 }) // 'Quis doloribus necessitatibus sint. Rerum accusamus impedit corporis porro.'
*
* @since 2.0.1
*/
paragraph(
sentenceCount:
| number
| {
/**
* The minimum number of sentences to generate.
*/
min: number;
/**
* The maximum number of sentences to generate.
*/
max: number;
} = 3
): string {
return this.sentences(sentenceCount);
}
/**
* Generates the given number of paragraphs.
*
* @param paragraphCount The number of paragraphs to generate. Defaults to `3`.
* @param paragraphCount.min The minimum number of paragraphs to generate.
* @param paragraphCount.max The maximum number of paragraphs to generate.
* @param separator The separator to use. Defaults to `'\n'`.
*
* @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.'
*
* faker.lorem.paragraphs({ min: 1, max: 3 })
* // 'Eum nam fugiat laudantium.
* // Dignissimos tempore porro necessitatibus commodi nam.
* // Veniam at commodi iste perferendis totam dolorum corporis ipsam.'
*
* @since 2.0.1
*/
paragraphs(
paragraphCount:
| number
| {
/**
* The minimum number of paragraphs to generate.
*/
min: number;
/**
* The maximum number of paragraphs to generate.
*/
max: number;
} = 3,
separator: string = '\n'
): string {
return this.faker.helpers
.multiple(() => this.paragraph(), { count: paragraphCount })
.join(separator);
}
/**
* Generates a random text based on a random lorem method.
*
* @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.'
*
* @since 3.1.0
*/
text(): string {
const methods: Array<keyof LoremModule> = [
'sentence',
'sentences',
'paragraph',
'paragraphs',
'lines',
];
const method = this.faker.helpers.arrayElement(methods);
return `${this[method]()}`;
}
/**
* 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`.
* @param lineCount.min The minimum number of lines to generate. Defaults to `1`.
* @param lineCount.max The maximum number of lines to generate. Defaults to `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.'
*
* faker.lorem.lines(2)
* // 'Quod quas nam quis impedit aut consequuntur.
* // Animi dolores aspernatur.'
*
* faker.lorem.lines({ min: 1, max: 3 })
* // 'Error dolorem natus quos eum consequatur necessitatibus.'
*
* @since 3.1.0
*/
lines(
lineCount:
| number
| {
/**
* The minimum number of lines to generate.
*/
min: number;
/**
* The maximum number of lines to generate.
*/
max: number;
} = { min: 1, max: 5 }
): string {
return this.sentences(lineCount, '\n');
}
}