feat: add exclude option to faker.helpers.enumValue - #3959
Conversation
✅ Deploy Preview for fakerjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
ST-DDT
left a comment
There was a problem hiding this comment.
CI is failing. Please run pnpn run preflight before commiting.
88c9e9d to
4e5470c
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## next #3959 +/- ##
==========================================
- Coverage 98.91% 98.88% -0.03%
==========================================
Files 923 924 +1
Lines 3223 3228 +5
Branches 567 587 +20
==========================================
+ Hits 3188 3192 +4
- Misses 31 32 +1
Partials 4 4
🚀 New features to boost your workflow:
|
4e5470c to
7515bb3
Compare
Closes faker-js#2994. Add an optional `exclude` option (`enumValue(enumObject, { exclude: [...] })`): values in `exclude` are not eligible to be picked. If exclude removes every enum value, a FakerError is thrown. The default behavior (no exclude / empty exclude) is unchanged.
7515bb3 to
81487f2
Compare
There was a problem hiding this comment.
Pull request overview
This PR extends faker.helpers.enumValue to support excluding specific enum values from selection, aligning the helper with common seeding/use-case needs (e.g., avoiding a particular enum member during generation).
Changes:
- Added an optional
optionsparameter withexcludeto filter out ineligible enum values before selection. - Added a dedicated error path when no enum values remain after applying
exclude. - Updated JSDoc to document the new option and thrown error condition.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| .filter((key) => !exclude.includes(enumObject[key] as T[keyof T])); | ||
| if (keys.length === 0) { | ||
| throw new FakerError( | ||
| 'No enum value is available to pick from after applying the exclude option.' | ||
| ); |
| /** | ||
| * The values to exclude when picking. | ||
| */ | ||
| exclude?: Array<T[keyof T]>; |
| const exclude = options?.exclude ?? []; | ||
| // ignore numeric keys added by TypeScript | ||
| const keys: Array<keyof T> = Object.keys(enumObject).filter((key) => | ||
| Number.isNaN(Number(key)) | ||
| ); | ||
| const keys: Array<keyof T> = Object.keys(enumObject) | ||
| .filter((key) => Number.isNaN(Number(key))) | ||
| .filter((key) => !exclude.includes(enumObject[key] as T[keyof T])); | ||
| if (keys.length === 0) { | ||
| throw new FakerError( |
|
Could you please address the feedback provided by copilot? |
Closes #2994.
Add an optional
excludeoption (enumValue(enumObject, { exclude: [...] })): values inexcludeare not eligible to be picked. If exclude removes every enum value, a FakerError is thrown. The default behavior (no exclude / empty exclude) is unchanged.