Skip to content

feat(helpers): add repeatedElements option to arrayElements - #4013

Open
shreyanshu12345 wants to merge 4 commits into
faker-js:nextfrom
shreyanshu12345:feature/array-elements-repeated
Open

feat(helpers): add repeatedElements option to arrayElements#4013
shreyanshu12345 wants to merge 4 commits into
faker-js:nextfrom
shreyanshu12345:feature/array-elements-repeated

Conversation

@shreyanshu12345

Copy link
Copy Markdown

Closes #3222

Summary

Added repeatedElements boolean parameter to faker.helpers.arrayElements().

When repeatedElements: true, elements can be picked more than once
(sampling with replacement). Defaults to false to preserve existing behavior.

Changes

  • Added repeatedElements parameter to arrayElements in src/modules/helpers/index.ts
  • Updated JSDoc with new parameter and examples
  • Added seeded tests for new parameter
  • Added random seeded tests for new parameter

@shreyanshu12345
shreyanshu12345 requested a review from a team as a code owner August 27, 2026 18:23
@netlify

netlify Bot commented Aug 27, 2026

Copy link
Copy Markdown

Deploy Preview for fakerjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit e5cdaa2
🔍 Latest deploy log https://app.netlify.com/projects/fakerjs/deploys/6a91e2dea2fce600082b6031
😎 Deploy Preview https://deploy-preview-4013.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.98%. Comparing base (bbd1693) to head (c0f4dab).

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           
Files with missing lines Coverage Δ
src/modules/helpers/module.ts 94.94% <100.00%> (+0.09%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ST-DDT ST-DDT added c: feature Request for new feature p: 1-normal Nothing urgent m: helpers Something is referring to the helpers module s: pending triage Pending Triage labels Aug 28, 2026
ST-DDT
ST-DDT previously requested changes Aug 28, 2026

@ST-DDT ST-DDT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

CI appears to be failing. Please fix using pnpm run preflight.

@ST-DDT

ST-DDT commented Aug 28, 2026

Copy link
Copy Markdown
Member

@ST-DDT ST-DDT added the s: on hold Blocked by something or frozen to avoid conflicts label Aug 28, 2026
@matthewmayer

matthewmayer commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

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]

@ST-DDT
ST-DDT dismissed their stale review August 28, 2026 06:21

Fixed

@@ -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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we note that this is only the case when allowRepeatedElements is false?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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 ??

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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 allowRepeatedElements is false,
  • it will be limited to stay inside.

Comment thread src/modules/helpers/module.ts Outdated
Comment on lines +1037 to +1045
count?: NumberOrRange,
options?: {
/**
* If true, elements can be picked more than once.
*
* @default false
*/
allowRepeatedElements?: boolean;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

question (blocking): shouldn't this collapse to a countOrOptions?: NumberOrRange | {count:NumberOrRange, allowRepeatedElements?: boolean}?

please check how other methods are already implemented

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: helpers Something is referring to the helpers module p: 1-normal Nothing urgent s: on hold Blocked by something or frozen to avoid conflicts s: pending triage Pending Triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add method/option to pick elements from an array multiple times

4 participants