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

fix(lorem): return word with maximum possible length when expected length is too large #1218

Closed
wants to merge 1 commit into from

Conversation

hankucz
Copy link
Contributor

@hankucz hankucz commented Aug 1, 2022

Fixes #1131

@codecov
Copy link

codecov bot commented Aug 1, 2022

Codecov Report

Merging #1218 (cef5c50) into main (4fa243e) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1218      +/-   ##
==========================================
- Coverage   99.62%   99.62%   -0.01%     
==========================================
  Files        2153     2153              
  Lines      236521   236537      +16     
  Branches      979      979              
==========================================
+ Hits       235643   235646       +3     
- Misses        857      870      +13     
  Partials       21       21              
Impacted Files Coverage Δ
src/modules/lorem/index.ts 100.00% <100.00%> (ø)
src/modules/internet/user-agent.ts 81.74% <0.00%> (-2.65%) ⬇️
src/modules/finance/index.ts 99.31% <0.00%> (-0.69%) ⬇️

@ejcheng ejcheng added c: bug Something isn't working p: 1-normal Nothing urgent s: needs decision Needs team/maintainer decision m: lorem Something is referring to the lorem module labels Aug 1, 2022
@ejcheng ejcheng added this to the v7 - Current Major milestone Aug 1, 2022
@ejcheng ejcheng requested a review from a team August 2, 2022 16:29
Comment on lines +36 to +42
properLengthWords = this.faker.definitions.lorem.words.filter((word) => {
if (word.length > maxLength) {
maxLength = word.length;
}

return word.length === length;
});
Copy link
Member

Choose a reason for hiding this comment

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

Beside that this is very costy, you already used reduce in test file, but why not here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Filtering was already part of the implementation before with word.length === length. So this is no change. Adding calculating max would not increase the cost of filtering much. So don't think this is a major problem.

Copy link
Member

Choose a reason for hiding this comment

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

Previously you ran only once through all words, now you do it twice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will only run a second time when the length is not satisfied. I don't see any other way of implementing this unless we want to throw, as @ST-DDT suggests, but I don't think it's a good idea.

@Shinigami92, any other suggestions welcome...

@@ -18,20 +18,36 @@ export class Lorem {
* Generates a word of a specified length.
*
* @param length length of the word that should be returned. Defaults to a random length.
* If a word with the specified length does not exist return a word with maximum possible length.
Copy link
Member

Choose a reason for hiding this comment

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

This does not fit my expectations regarding this parameter.

If the length of the word doesn't matter for me, then I wouldn't specify it.
The very fact, that I specify a specific length expresses intend that I actually want a word of that particular length.

If I just want a very long word, I dig into the data pick a suitable length and use that (Or maybe use a range parameter to narrow it down).

So this method should throw an error, if it cannot satisfy the requested data or should have an optional param/option that allows for a "closest fit". Not necessarily the longest. e.g. requesting a word with length 1 should not return Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz (which is the name of an actual German law until 2013) according to https://de.babbel.com/de/magazine/das-laengste-wort.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not for general words, this is for lorem, which is the same for all languages (although it does change the alphabet for some, like jp).

I can change implementation to throw an error, but when I had a brief look, we don't do that in other places. For example, faker.word.* would return a word of any length, when length can not be satisfied: https://github.com/faker-js/faker/blob/main/src/modules/word/index.ts#L11

So if we want to stay consistent, that's fine for me. I can change it in any other place. The question is: should faker be strict or be as close to the expectation as possible?

Copy link
Member

Choose a reason for hiding this comment

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

I now looked a bit deeper into the implementation and tried to think about what your proposed implementation does.
And I'm more and more on @ST-DDT side (comment above here) that this current proposed PR is not what I, @ST-DDT or a user would expect.
We might need to discuss this in a team meeting, but I would currently reject your proposed solution, sorry 🙁

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's discuss this at the meeting, as different functions behave differently. As @hankucz pointed out, faker.word.* would ignore length if it can not be satisfied. So if we want to throw an error, we would need to change the behavior of those other methods too, to stay consistent.

@hankucz hankucz requested review from ST-DDT and Shinigami92 August 3, 2022 18:47
@@ -18,20 +18,36 @@ export class Lorem {
* Generates a word of a specified length.
*
* @param length length of the word that should be returned. Defaults to a random length.
* If a word with the specified length does not exist return a word with maximum possible length.
Copy link
Member

Choose a reason for hiding this comment

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

I now looked a bit deeper into the implementation and tried to think about what your proposed implementation does.
And I'm more and more on @ST-DDT side (comment above here) that this current proposed PR is not what I, @ST-DDT or a user would expect.
We might need to discuss this in a team meeting, but I would currently reject your proposed solution, sorry 🙁

@ST-DDT
Copy link
Member

ST-DDT commented Aug 4, 2022

See here for our team decision: #1131 (comment)

@Shinigami92 Shinigami92 changed the title fix(lorem.word): return word with maximum possible length when expected length is too large fix(lorem): return word with maximum possible length when expected length is too large Aug 12, 2022
@ST-DDT
Copy link
Member

ST-DDT commented Sep 22, 2022

This PR is stale and does not match our team decision.
Closing for now so someone else can tackle this.

@ST-DDT ST-DDT closed this Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: bug Something isn't working m: lorem Something is referring to the lorem module p: 1-normal Nothing urgent s: needs decision Needs team/maintainer decision
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

faker.lorem.word can return undefined despite being typed as string
5 participants