Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

randexp: Convert string to regexp #50

Merged
merged 2 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/randexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ module.exports = class RandExp {
*/
static randexp(regexp, m) {
var randexp;
if(typeof regexp === 'string') {
regexp = new RegExp(regexp, m);
}

if (regexp._randexp === undefined) {
randexp = new RandExp(regexp, m);
regexp._randexp = randexp;
Expand Down
7 changes: 7 additions & 0 deletions test/main-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ describe('Call with a string', () => {
});
});

describe('Call shorthand randexp method with a string', () => {
it('Returns a correctly generated string', () => {
var r = randexp('\\d{4}');
assert.equal(r.length, 4);
});
});

describe('Call without a string or regular expression', () => {
it('Throws an error', () => {
assert.throws(() => {
Expand Down