-
-
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## next #2227 +/- ##
=======================================
Coverage 99.60% 99.60%
=======================================
Files 2784 2784
Lines 252492 252537 +45
Branches 1082 1085 +3
=======================================
+ Hits 251496 251543 +47
+ Misses 969 967 -2
Partials 27 27
|
…and max instead of being an object with all three properties
…r for negative min
…arameter_to_lorem.text
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.
I think the description of text()
in the overview could also be modified to reflect this can be used to produce text of a certain length
The generic [`text()`](https://fakerjs.dev/api/lorem.html#text) method can be used to generate some text between one sentence and multiple paragraphs
The behavior of the function when the length parameter is supplied is quite different to the version without. I'm wondering if therefore it would make sense to just make it a separate method, like:
|
…e js-doc for lorem.text
…_lorem.text' of github.com:inkedtree/faker into feat/lorem/issue-1546/add_character_length_parameter_to_lorem.text
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.
Also, should we stick with sentence()
(if length is provided) to produce a more consistent text?
(We end with a dot, so it would make sense to have dots in between as well.)
Yes, it would make sense to put dots in between as well. |
@inkedtree We kind of are unsure what requirements we have for the method/the parameter. And would like to hear your requirements for the method. |
Sorry, for the late reply. I am not sure, if I understand all your questions correctly. To be honest, I am not entirely sure what my requirements are after reading about all available methods in the Sometimes when I am using faker I would like to be able to generate sentences, lines and so on with a specific length (range). I think, it would get confusing if the |
…arameter_to_lorem.text
…arameter_to_lorem.text
…arameter_to_lorem.text
| number | ||
| { | ||
/** | ||
* The minimum length of text to generate. |
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.
* The minimum length of text to generate. | |
* The minimum amount of characters to generate. |
*/ | ||
min: number; | ||
/** | ||
* The maximum length of text to generate. |
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.
* The maximum length of text to generate. | |
* The maximum amount of characters to generate. |
@@ -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. |
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.
* @param options.length The length of text to generate as number or range. | |
* @param options.length The length of text (in characters) to generate as number or range. |
Team Decision
|
if (options.length == null) { | ||
return `${this[method]()}`; | ||
} | ||
|
||
const length = this.faker.helpers.rangeToNumber(options.length); |
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.
Please use our standard way of decstructuring options:
if (options.length == null) { | |
return `${this[method]()}`; | |
} | |
const length = this.faker.helpers.rangeToNumber(options.length); | |
const { length: lengthOrRange } = options; | |
if (lengthOrRange == null) { | |
return `${this[method]()}`; | |
} | |
const length = this.faker.helpers.rangeToNumber(lengthOrRange); |
|
||
let text = ''; | ||
do { | ||
text = `${text}${this[method]()} `; |
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.
Please only use this.sentence()
here and maybe randomly (e.g. faker.datatype.boolean()) add a newline to simulate paragraphs/lines.
text = `${text}${this[method]()} `; | ||
} while (text.length < length); | ||
|
||
return `${text.substring(0, length).replace(/.$/, '.')}`; |
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.
Please just return the value without dot at the end.
return `${text.substring(0, length).replace(/.$/, '.')}`; | |
return text.substring(0, length); |
it('should return a dot for length one', () => { | ||
const actual = faker.lorem.text({ length: 1 }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('.'); | ||
}); |
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.
This test is no longer needed,
Closed as stale. We can reopen this when you want to continue with it. |
I just wrote a custom function for this and would love to have it baked into faker. It looks like the original submitter deleted their Github account. I'd be open to addressing the comments and submitting a fresh PR. Would that be accepted, @ST-DDT ? |
@bmulholland feel free to open another PR. We always love when the community gets involved. Make sure to read our contribution guide before hand and like the original issue. |
Adds a character length parameter to
faker.lorem.text
as described in #1546.Changes made:
Added a new parameterlength?: number | { min: number, max: number }
such thatlorem.text
returns text that has a particular length if the parameter is passed{length: number | { min: number, max: number }}
such thatlorem.text
returns text that has a particular length if the parameter is passedlorem.text
returns text that the length passed as argumentlorem.spec.ts
and updated its snapshotCloses #1546