-
-
Notifications
You must be signed in to change notification settings - Fork 953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(lorem): add character length parameter to lorem.text #2227
Changes from all commits
8026316
8d1ddd9
30e73fb
65f7d5d
8f5ba98
ac91a62
425ad90
7cb71ed
4fad2bf
a2f6bbd
fa121ee
33d0f37
2abd70b
fa79dec
956101b
bfa702f
9280569
9fcacde
1bd5f3c
5cab96d
15a8f93
402802d
153dcfa
056886a
1fbb6da
05fc62b
1774fc5
0046096
c3f19ad
9cac43d
742e794
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -310,6 +310,9 @@ export class LoremModule { | |||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||
* Generates a random text based on a random lorem method. | ||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||
* @param options The options for the text to generate or options.length as quick primitive argument. | ||||||||||||||||||||||||||
* @param options.length The length of text to generate as number or range. | ||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||
* @example | ||||||||||||||||||||||||||
* faker.lorem.text() // 'Doloribus autem non quis vero quia.' | ||||||||||||||||||||||||||
* faker.lorem.text() | ||||||||||||||||||||||||||
|
@@ -318,10 +321,33 @@ export class LoremModule { | |||||||||||||||||||||||||
* // Quis ut dolor dolores facilis possimus tempore voluptates. | ||||||||||||||||||||||||||
* // Iure nam officia optio cumque. | ||||||||||||||||||||||||||
* // Dolor tempora iusto.' | ||||||||||||||||||||||||||
* faker.lorem.text(14) // 'Doloribus aut.' | ||||||||||||||||||||||||||
* faker.lorem.text({length: 14}) // 'Doloribus aut.' | ||||||||||||||||||||||||||
* faker.lorem.text({length: {min: 10, max: 15}}) // 'autem non quis.' | ||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||
* @since 3.1.0 | ||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||
text(): string { | ||||||||||||||||||||||||||
text( | ||||||||||||||||||||||||||
options: | ||||||||||||||||||||||||||
| number | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||
* The length (range) of text to generate. | ||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||
length?: | ||||||||||||||||||||||||||
| number | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||
* The minimum length of text to generate. | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||
min: number; | ||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||
* The maximum length of text to generate. | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||
max: number; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
} = {} | ||||||||||||||||||||||||||
): string { | ||||||||||||||||||||||||||
const methods: Array<keyof LoremModule> = [ | ||||||||||||||||||||||||||
'sentence', | ||||||||||||||||||||||||||
'sentences', | ||||||||||||||||||||||||||
|
@@ -332,7 +358,26 @@ export class LoremModule { | |||||||||||||||||||||||||
|
||||||||||||||||||||||||||
const method = this.faker.helpers.arrayElement(methods); | ||||||||||||||||||||||||||
ST-DDT marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return `${this[method]()}`; | ||||||||||||||||||||||||||
if (typeof options === 'number') { | ||||||||||||||||||||||||||
options = { length: options }; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
ST-DDT marked this conversation as resolved.
Show resolved
Hide resolved
ST-DDT marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
if (options.length == null) { | ||||||||||||||||||||||||||
return `${this[method]()}`; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
const length = this.faker.helpers.rangeToNumber(options.length); | ||||||||||||||||||||||||||
Comment on lines
+365
to
+369
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use our standard way of decstructuring options:
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (length <= 0) { | ||||||||||||||||||||||||||
return ''; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let text = ''; | ||||||||||||||||||||||||||
do { | ||||||||||||||||||||||||||
text = `${text}${this[method]()} `; | ||||||||||||||||||||||||||
ST-DDT marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please only use |
||||||||||||||||||||||||||
} while (text.length < length); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return `${text.substring(0, length).replace(/.$/, '.')}`; | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please just return the value without dot at the end.
Suggested change
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,6 +325,53 @@ describe('lorem', () => { | |
expect(actual).toBeTruthy(); | ||
expect(actual).toBeTypeOf('string'); | ||
}); | ||
|
||
it('should return text of length 15 for the options object', () => { | ||
const actual = faker.lorem.text({ length: 15 }); | ||
|
||
expect(actual).toBeTruthy(); | ||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toHaveLength(15); | ||
expect(actual.trim()).toHaveLength(15); | ||
}); | ||
|
||
it('should return text of length 15 for the quick primitive argument', () => { | ||
const actual = faker.lorem.text(15); | ||
|
||
expect(actual).toBeTruthy(); | ||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toHaveLength(15); | ||
expect(actual.trim()).toHaveLength(15); | ||
}); | ||
|
||
it('should return text with a length in range [25, 40]', () => { | ||
const actual = faker.lorem.text({ length: { min: 25, max: 40 } }); | ||
|
||
expect(actual).toBeTruthy(); | ||
expect(actual).toBeTypeOf('string'); | ||
expect(actual.length).toBeGreaterThanOrEqual(25); | ||
expect(actual.length).toBeLessThanOrEqual(40); | ||
}); | ||
|
||
it('should throw an error if min is greater than max', () => { | ||
expect(() => | ||
faker.lorem.text({ length: { min: 3, max: 1 } }) | ||
).toThrowError(/^Max 1 should be greater than min 3.$/); | ||
}); | ||
|
||
it('should return an empty string for length zero', () => { | ||
const actual = faker.lorem.text({ length: 0 }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe(''); | ||
}); | ||
|
||
it('should return a dot for length one', () => { | ||
const actual = faker.lorem.text({ length: 1 }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('.'); | ||
}); | ||
Comment on lines
+369
to
+374
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is no longer needed, |
||
}); | ||
|
||
describe('lines()', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.