Skip to content
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

Closed
wants to merge 31 commits into from
Closed

feat(lorem): add character length parameter to lorem.text #2227

wants to merge 31 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Jun 27, 2023

Adds a character length parameter to faker.lorem.text as described in #1546.

Changes made:

  • Added a new parameter length?: number | { min: number, max: number } such that lorem.text returns text that has a particular length if the parameter is passed
  • Added a new optional options object {length: number | { min: number, max: number }} such that lorem.text returns text that has a particular length if the parameter is passed
  • Added a new optional quick primitive argument such that lorem.text returns text that the length passed as argument
  • Added random seeded tests for the new parameter to lorem.spec.ts and updated its snapshot

Closes #1546

@ghost ghost self-requested a review as a code owner June 27, 2023 19:23
@codecov
Copy link

codecov bot commented Jun 27, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.60%. Comparing base (604d52d) to head (742e794).
Report is 324 commits behind head on next.

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           
Files Coverage Δ
src/modules/lorem/index.ts 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

@ghost ghost self-requested a review June 29, 2023 19:11
Alexander added 2 commits June 30, 2023 18:50
@matthewmayer matthewmayer dismissed their stale review July 17, 2023 00:48

outdated

@xDivisionByZerox xDivisionByZerox added c: feature Request for new feature needs rebase There is a merge conflict m: lorem Something is referring to the lorem module labels Jul 17, 2023
@xDivisionByZerox xDivisionByZerox added p: 1-normal Nothing urgent c: locale Permutes locale definitions labels Jul 17, 2023
Copy link
Contributor

@matthewmayer matthewmayer left a 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

@matthewmayer
Copy link
Contributor

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:

* faker.lorem.characters(14) // 'Doloribus aut.' (14 characters)
* faker.lorem.characters({length: 14}) // 'Doloribus aut.' (14 characters)
* faker.lorem.characters({length: {min: 10, max: 15}}) // 'autem non quis.' (15 characters)
* faker.lorem.characters({length: {min: 10, max: 12}}) // 'autem non.' (10 characters)

@ghost ghost mentioned this pull request Jul 19, 2023
Alexander added 2 commits July 19, 2023 20:46
…_lorem.text' of github.com:inkedtree/faker into feat/lorem/issue-1546/add_character_length_parameter_to_lorem.text
Copy link
Member

@ST-DDT ST-DDT left a 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.)

@ghost
Copy link
Author

ghost commented Aug 20, 2023

Yes, it would make sense to put dots in between as well.

@ST-DDT
Copy link
Member

ST-DDT commented Aug 24, 2023

@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.
This method generates vastly different types of text (some with newlines, some without).
So do we need the length parameter for the specific versions as well? Such as sentences, lines?
Or should we limit the text method to sentences?
Would it get confusing if we have lines( { length: number}) aka does the length refer to each line or to the entire string?

@ghost
Copy link
Author

ghost commented Aug 31, 2023

@ST-DDT

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 lorem module. Originally, I wanted to have a method that generates sentences with a particular length. So, basically a paragraph, I guess. But now I am not sure, if it wouldn't be better to add the parameter to paragraph(s) for example in this case.

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 length parameter does not refer to the length of the entire string, e.g. of lines.

| number
| {
/**
* The minimum length of text to generate.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The minimum length of text to generate.
* The minimum amount of characters to generate.

*/
min: number;
/**
* The maximum length of text to generate.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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.

@ST-DDT
Copy link
Member

ST-DDT commented Mar 28, 2024

Team Decision

  • We accept this feature request. Please rebase it and work on the suggestions we point out.

@ST-DDT ST-DDT added s: accepted Accepted feature / Confirmed bug and removed s: needs decision Needs team/maintainer decision c: locale Permutes locale definitions labels Mar 28, 2024
Comment on lines +365 to +369
if (options.length == null) {
return `${this[method]()}`;
}

const length = this.faker.helpers.rangeToNumber(options.length);
Copy link
Member

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:

Suggested change
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]()} `;
Copy link
Member

@ST-DDT ST-DDT Mar 28, 2024

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(/.$/, '.')}`;
Copy link
Member

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.

Suggested change
return `${text.substring(0, length).replace(/.$/, '.')}`;
return text.substring(0, length);

Comment on lines +369 to +374
it('should return a dot for length one', () => {
const actual = faker.lorem.text({ length: 1 });

expect(actual).toBeTypeOf('string');
expect(actual).toBe('.');
});
Copy link
Member

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,

@ST-DDT ST-DDT marked this pull request as draft March 28, 2024 18:04
@ST-DDT
Copy link
Member

ST-DDT commented Apr 25, 2024

Closed as stale. We can reopen this when you want to continue with it.

@ST-DDT ST-DDT closed this Apr 25, 2024
@bmulholland
Copy link

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 ?

@xDivisionByZerox
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: feature Request for new feature m: lorem Something is referring to the lorem module p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add character length parameter to lorem.text
6 participants