Skip to content

Commit 5010bc0

Browse files
fix: handled anchors and added tests for regex (#179)
1 parent e720300 commit 5010bc0

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/samplers/string-regex.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ export function regexSample(pattern) {
186186
pattern = pattern.toString();
187187
pattern = pattern.match(/\/(.+?)\//)?.[1] ?? ''; // Remove frontslash from front and back of RegExp
188188
}
189-
189+
190+
pattern = pattern.replace(/^(\^)?(.*?)(\$)?$/, '$2'); // Remove anchors if present
191+
190192
let min
191193
let max
192194
let repetitions

test/unit/string.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('sampleString', () => {
123123
});
124124

125125
it('should generate valid text for basic regexes', () => {
126-
[/#{3}test[1-5]/, /[500-15000]/, /#{2,9}/, /#{5}/, /0x[0-9a-f]{40}/]
126+
[/#{3}test[1-5]/, /[500-15000]/, /#{2,9}/, /#{5}/, /0x[0-9a-f]{40}/, /^[0-9]$/]
127127
.forEach((regexp) => {
128128
res = sampleString(
129129
{pattern: regexp.source},

0 commit comments

Comments
 (0)