feat(helpers): add repeatedElements option to arrayElements - #4013
feat(helpers): add repeatedElements option to arrayElements#4013shreyanshu12345 wants to merge 4 commits into
Conversation
✅ Deploy Preview for fakerjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #4013 +/- ##
=======================================
Coverage 98.98% 98.98%
=======================================
Files 926 926
Lines 3246 3252 +6
Branches 582 584 +2
=======================================
+ Hits 3213 3219 +6
Misses 29 29
Partials 4 4
🚀 New features to boost your workflow:
|
|
i would prefer an options object to allow for future expansion i think allowRepeatedElements would be more consistent with other params like allowLeadingZeros and allowSpecialCharacters in other methods. faker.helpers.arrayElements([1, 2, 3], 5, {allowRepeatedElements: true}) // [1, 3, 1, 2, 1] |
| @@ -1020,22 +1020,31 @@ export class SimpleHelpersModule extends SimpleModuleBase { | |||
| * @param count Number or range of elements to pick. | |||
| * When not provided, random number of elements will be picked. | |||
| * When value exceeds array boundaries, it will be limited to stay inside. | |||
There was a problem hiding this comment.
should we note that this is only the case when allowRepeatedElements is false?
There was a problem hiding this comment.
Yes, when allowRepeatedElements is true, my implementation of the function allows it to exceed the length of the array. Well, Since repetition is allowed we can pick more elements than present in the array right ??
There was a problem hiding this comment.
should i update the function description to this:
- @PARAM count Number or range of elements to pick.
- When not provided, random number of elements will be picked.
- When value exceeds array boundaries and
allowRepeatedElementsis false, - it will be limited to stay inside.
| count?: NumberOrRange, | ||
| options?: { | ||
| /** | ||
| * If true, elements can be picked more than once. | ||
| * | ||
| * @default false | ||
| */ | ||
| allowRepeatedElements?: boolean; | ||
| } |
There was a problem hiding this comment.
question (blocking): shouldn't this collapse to a countOrOptions?: NumberOrRange | {count:NumberOrRange, allowRepeatedElements?: boolean}?
please check how other methods are already implemented
Closes #3222
Summary
Added
repeatedElementsboolean parameter tofaker.helpers.arrayElements().When
repeatedElements: true, elements can be picked more than once(sampling with replacement). Defaults to
falseto preserve existing behavior.Changes
repeatedElementsparameter toarrayElementsinsrc/modules/helpers/index.ts