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

Add character length parameter to lorem.text #1546

Closed
ST-DDT opened this issue Nov 9, 2022 · 12 comments
Closed

Add character length parameter to lorem.text #1546

ST-DDT opened this issue Nov 9, 2022 · 12 comments
Assignees
Labels
c: feature Request for new feature m: lorem Something is referring to the lorem module p: 1-normal Nothing urgent
Milestone

Comments

@ST-DDT
Copy link
Member

ST-DDT commented Nov 9, 2022

Clear and concise description of the problem

Sometimes we just want a text that has a particular length instead of conforming to arbitrary length units (lines/paragraphs).

Suggested solution

Add length parameter to the lorem.text method.

faker.lorem.text({ length: number | { min: number, max: number }})

faker.lorem.text({ length: 150 })

Nostrum nostrum aperiam vero eveniet quasi. Neque libero consequuntur molestias iste nihil corrupti ducimus suscipit. Ratione ducimus occaecati offic.

Alternative

faker.lorem.paragraphs(<sufficientlyLargeNumber>).substring(0, 150)

Additional context

Originally requested in Discord by kryp71c.

@ST-DDT ST-DDT added c: feature Request for new feature p: 1-normal Nothing urgent m: lorem Something is referring to the lorem module labels Nov 9, 2022
@ST-DDT
Copy link
Member Author

ST-DDT commented Nov 9, 2022

Currently blocked by #1486

@ST-DDT ST-DDT added the s: on hold Blocked by something or frozen to avoid conflicts label Nov 9, 2022
@ST-DDT ST-DDT moved this to Todo in Faker Roadmap Nov 9, 2022
@mrzmyr
Copy link

mrzmyr commented Nov 22, 2022

@ST-DDT still blocked? #1486 seems merged

@Shinigami92 Shinigami92 removed the s: on hold Blocked by something or frozen to avoid conflicts label Nov 22, 2022
@ST-DDT
Copy link
Member Author

ST-DDT commented Nov 22, 2022

@mrzmyr No, it is no longer blocked. Do you wish to work on this?

@mrzmyr
Copy link

mrzmyr commented Nov 23, 2022

@ST-DDT, nope, just wanted to ping.

@EarthyOrange
Copy link

@ST-DDT How should this be approached?

If I understood things correctly then once faker is initialized all the Module classes are initialized.

To implement the text method, I think we will need to know the lengths of all the words declared in the required locales module.

A few approaches that I can think of are:

  • Since, we don't have that information available at the time of initialization we will need to compute it at run time; Perhaps when the method is called. It will be slightly slow to return on the first call but should be able to perform well in the subsequent calls.
  • We change the Lorem's locales data to also hold the length of the words. I think there can be a script which calculates that and generates a new file for consumption. This would keep the first call performant but other methods will need to to be changed to accommodate this change.

@ST-DDT
Copy link
Member Author

ST-DDT commented Dec 27, 2022

If I understood things correctly then once faker is initialized all the Module classes are initialized.

Currently yes, this might change when we implement proper tree shaking.
Please note that the modules dont get reinitialized when the locale changes.

To implement the text method, I think we will need to know the lengths of all the words declared in the required locales module.

I'm not sure about that. Could you please explain your idea in more detail? I think having a avg or mean length is sufficient to determine what to generate (sentence, paragraph, ...). Also I think generating that on the fly is fast enough for now.

@EarthyOrange
Copy link

EarthyOrange commented Dec 27, 2022

Let's say we need a sentence that is exactly 150 characters long. We have a list of words of variable lengths per locale. So to generate a sentence for a locale:

  • Randomly choose a word (w1) from the list, such that w1.length < 150; Add to buffer.
  • Randomly choose a word (w2) from the list, such that w2.length < 150 - w1.length; Add to buffer.
  • And so on until we have a sentence that is exactly 150 characters long.

To be able to randomly choose a word from the list:

  • We need to group all the words in the list by their length
  • We randomly select a length followed by a random selection of word from the group of words corresponding to the length
  • We eliminate groups, from randomized selection, whose lengths don't satisfy the constraint wN.length < 150 - (w1.length + ... + wN-1.length)

Please note that the modules dont get reinitialized when the locale changes.

Do we know how the APIs are generally invoked? Eg: Will the text method be invoked for all locales per import?

import faker from '';

test("#1", () => { /* faker.text for en */ });
test("#2", () => { /* faker.text for de */ });
test("#3", () => { /* faker.text for es */ });

@ST-DDT
Copy link
Member Author

ST-DDT commented Dec 27, 2022

I think we could do that a tad more easily. We generate a paragraph/sentences until the char limit is exceeded, then excess chars are trimmed, finally the end of the sentence/paragraph is pseudo restored.

  1. Foo Bar. Foo Bar. Foo Bar.
  2. Foo Bar. Foo Bar. Foo
  3. Foo Bar. Foo Bar. Foo.

@ghost
Copy link

ghost commented Jun 20, 2023

I would like to work on this issue.

@ST-DDT
Copy link
Member Author

ST-DDT commented Oct 20, 2024

@bmulholland If you would like to work on this, please leave a comment so we can assign the issue to you.

@bmulholland
Copy link

I'd love to, it's just not high on my priority list.

@ST-DDT
Copy link
Member Author

ST-DDT commented Oct 21, 2024

Awesome. No hurry.

@ST-DDT ST-DDT closed this as not planned Won't fix, can't repro, duplicate, stale Nov 9, 2024
@ST-DDT ST-DDT pinned this issue Nov 9, 2024
@ST-DDT ST-DDT unpinned this issue Nov 9, 2024
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
Projects
No open projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

5 participants