Skip to content

Commit 15eb60a

Browse files
maximilian-kraussfent
authored andcommitted
fix: calling shorthand method with string (#50)
* randexp: Convert string to regexp * Removes additional local variable fixes #49
1 parent d76f4c0 commit 15eb60a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/randexp.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ module.exports = class RandExp {
236236
*/
237237
static randexp(regexp, m) {
238238
var randexp;
239+
if(typeof regexp === 'string') {
240+
regexp = new RegExp(regexp, m);
241+
}
242+
239243
if (regexp._randexp === undefined) {
240244
randexp = new RandExp(regexp, m);
241245
regexp._randexp = randexp;

test/main-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ describe('Call with a string', () => {
5353
});
5454
});
5555

56+
describe('Call shorthand randexp method with a string', () => {
57+
it('Returns a correctly generated string', () => {
58+
var r = randexp('\\d{4}');
59+
assert.equal(r.length, 4);
60+
});
61+
});
62+
5663
describe('Call without a string or regular expression', () => {
5764
it('Throws an error', () => {
5865
assert.throws(() => {

0 commit comments

Comments
 (0)