fix(helpers): ignore wrapper characters in fromRegExp#3795
Conversation
✅ Deploy Preview for fakerjs ready!
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 #3795 +/- ##
==========================================
- Coverage 98.90% 98.90% -0.01%
==========================================
Files 897 897
Lines 3103 3102 -1
Branches 556 571 +15
==========================================
- Hits 3069 3068 -1
Misses 30 30
Partials 4 4
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR updates faker.helpers.fromRegExp to ignore start/end anchor characters (^ and $) when a RegExp object is provided, so anchored patterns don’t leak wrapper characters into the generated output (fixes #3663).
Changes:
- Strip
^/$anchors from the extracted pattern whenfromRegExpreceives aRegExp. - Add a unit test asserting anchored
/^foo$/iproducesfoo.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/modules/helpers/index.ts |
Adjusts the RegExp-to-pattern extraction to drop leading/trailing anchors. |
test/modules/helpers.spec.ts |
Adds coverage for anchored input being unwrapped before generation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
this is arguably breaking, although seems doubtful anyone was relying on this? |
|
The method basically only implements a third of all regex features. So we could declare this as Once the standalone module functions PR is merged, then I'll consider adding a |
|
The regex One edge case to consider: if someone passes pattern = /\/\^*(.+?)\$*\//.exec(pattern)?.[1] ?? '';Given that |
|
@faker-js/maintainers Thoughts? |
LGTM, this is a bug fix, not a breaking change - nobody should be relying on literal suggestion: consider using if (pattern instanceof RegExp) {
isCaseInsensitive = pattern.flags.includes('i');
pattern = pattern.source.replace(/^\^/, '').replace(/\$$/, '');
}a bit more readable and avoids the slash-parsing regex entirely. |
Fixes #3663
Ignore the anchor characters at the start and end of regex patterns.