Skip to content

Commit 7e6273b

Browse files
Shinigami92damienwebdev
authored andcommitted
feat: migrate lorem (#86)
1 parent 86580d8 commit 7e6273b

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Hacker } from './hacker';
99
import { Helpers } from './helpers';
1010
import { Image } from './image';
1111
import { Internet } from './internet';
12+
import { Lorem } from './lorem';
1213
import { Mersenne } from './mersenne';
1314
import { Music } from './music';
1415
import { Name } from './name';
@@ -191,7 +192,7 @@ export class Faker {
191192
// readonly iban = new (require('./iban'))(this);
192193
readonly image: Image = new Image(this);
193194
readonly internet: Internet = new Internet(this);
194-
readonly lorem = new (require('./lorem'))(this);
195+
readonly lorem: Lorem = new Lorem(this);
195196
readonly music: Music = new Music(this);
196197
readonly name: Name = new Name(this);
197198
readonly phone: Phone = new Phone(this);

src/lorem.ts

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import type { Faker } from '.';
2+
import type { Helpers } from './helpers';
3+
4+
export class Lorem {
5+
private readonly Helpers: Helpers;
6+
7+
constructor(private readonly faker: Faker) {
8+
this.Helpers = faker.helpers;
9+
10+
// Bind `this` so namespaced is working correctly
11+
for (const name of Object.getOwnPropertyNames(Lorem.prototype)) {
12+
if (name === 'constructor' || typeof this[name] !== 'function') {
13+
continue;
14+
}
15+
this[name] = this[name].bind(this);
16+
}
17+
}
18+
19+
/**
20+
* Generates a word of a specified length.
21+
*
22+
* @method faker.lorem.word
23+
* @param length length of the word that should be returned. Defaults to a random length
24+
*/
25+
word(length?: number): string {
26+
const hasRightLength = (word: string) => word.length === length;
27+
let properLengthWords: string[];
28+
if (typeof length === 'undefined') {
29+
properLengthWords = this.faker.definitions.lorem.words;
30+
} else {
31+
properLengthWords =
32+
this.faker.definitions.lorem.words.filter(hasRightLength);
33+
}
34+
return this.faker.random.arrayElement(properLengthWords);
35+
}
36+
37+
/**
38+
* Generates a space separated list of words.
39+
*
40+
* @method faker.lorem.words
41+
* @param num number of words, defaults to 3
42+
*/
43+
words(num?: number): string {
44+
if (typeof num == 'undefined') {
45+
num = 3;
46+
}
47+
const words: string[] = [];
48+
for (let i = 0; i < num; i++) {
49+
words.push(this.faker.lorem.word());
50+
}
51+
return words.join(' ');
52+
}
53+
54+
/**
55+
* sentence
56+
*
57+
* @method faker.lorem.sentence
58+
* @param wordCount defaults to a random number between 3 and 10
59+
* @param range
60+
*/
61+
// TODO @Shinigami92 2022-01-11: `range` is not in use
62+
sentence(wordCount?: number, range?: number): string {
63+
if (typeof wordCount == 'undefined') {
64+
wordCount = this.faker.datatype.number({ min: 3, max: 10 });
65+
}
66+
// if (typeof range == 'undefined') { range = 7; }
67+
68+
// strange issue with the node_min_test failing for capitalize, please fix and add faker.lorem.back
69+
//return faker.lorem.words(wordCount + Helpers.randomNumber(range)).join(' ').capitalize();
70+
71+
const sentence = this.faker.lorem.words(wordCount);
72+
return sentence.charAt(0).toUpperCase() + sentence.slice(1) + '.';
73+
}
74+
75+
/**
76+
* slug
77+
*
78+
* @method faker.lorem.slug
79+
* @param wordCount number of words, defaults to 3
80+
*/
81+
slug(wordCount?: number) {
82+
const words = this.faker.lorem.words(wordCount);
83+
return this.Helpers.slugify(words);
84+
}
85+
86+
/**
87+
* sentences
88+
*
89+
* @method faker.lorem.sentences
90+
* @param sentenceCount defaults to a random number between 2 and 6
91+
* @param separator defaults to `' '`
92+
*/
93+
sentences(sentenceCount?: number, separator?: string) {
94+
if (typeof sentenceCount === 'undefined') {
95+
sentenceCount = this.faker.datatype.number({ min: 2, max: 6 });
96+
}
97+
if (typeof separator == 'undefined') {
98+
separator = ' ';
99+
}
100+
const sentences: string[] = [];
101+
for (sentenceCount; sentenceCount > 0; sentenceCount--) {
102+
sentences.push(this.faker.lorem.sentence());
103+
}
104+
return sentences.join(separator);
105+
}
106+
107+
/**
108+
* paragraph
109+
*
110+
* @method faker.lorem.paragraph
111+
* @param sentenceCount defaults to 3
112+
*/
113+
paragraph(sentenceCount: number = 3): string {
114+
return this.faker.lorem.sentences(
115+
sentenceCount + this.faker.datatype.number(3)
116+
);
117+
}
118+
119+
/**
120+
* paragraphs
121+
*
122+
* @method faker.lorem.paragraphs
123+
* @param paragraphCount defaults to 3
124+
* @param separator defaults to `'\n \r'`
125+
*/
126+
paragraphs(paragraphCount: number = 3, separator: string = '\n \r'): string {
127+
const paragraphs: string[] = [];
128+
for (paragraphCount; paragraphCount > 0; paragraphCount--) {
129+
paragraphs.push(this.faker.lorem.paragraph());
130+
}
131+
return paragraphs.join(separator);
132+
}
133+
134+
/**
135+
* Returns random text based on a random lorem method
136+
*
137+
* @method faker.lorem.text
138+
* @param times
139+
*/
140+
// TODO @Shinigami92 2022-01-11: Is this a function-name alias?
141+
// Or can we just remove the `loremText`?
142+
// TODO @Shinigami92 2022-01-11: `times` is not in use
143+
text = function loremText(times?: number) {
144+
const loremMethods = [
145+
'lorem.word',
146+
'lorem.words',
147+
'lorem.sentence',
148+
'lorem.sentences',
149+
'lorem.paragraph',
150+
'lorem.paragraphs',
151+
'lorem.lines',
152+
];
153+
const randomLoremMethod = this.faker.random.arrayElement(loremMethods);
154+
return this.faker.fake('{{' + randomLoremMethod + '}}');
155+
};
156+
157+
/**
158+
* Returns lines of lorem separated by `'\n'`
159+
*
160+
* @method faker.lorem.lines
161+
* @param lineCount defaults to a random number between 1 and 5
162+
*/
163+
lines(lineCount?: number): string {
164+
if (typeof lineCount === 'undefined') {
165+
lineCount = this.faker.datatype.number({ min: 1, max: 5 });
166+
}
167+
return this.faker.lorem.sentences(lineCount, '\n');
168+
}
169+
}

0 commit comments

Comments
 (0)