Skip to content

Commit

Permalink
Merge pull request #19 from ywpark1/issue-18
Browse files Browse the repository at this point in the history
Fix Issue #18 : Exclude not working
  • Loading branch information
doowb authored Oct 23, 2018
2 parents 4d76cd1 + a04548e commit 8e78da8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ function randomatic(pattern, length, options) {
// Characters to exclude
if (opts.exclude) {
var exclude = typeOf(opts.exclude) === 'string' ? opts.exclude : opts.exclude.join('');
exclude = exclude.replace(new RegExp('[\\]]+', 'g'), '');
mask = mask.replace(new RegExp('[' + exclude + ']+', 'g'), '');

if(opts.exclude.indexOf(']') !== -1) mask = mask.replace(new RegExp('[\\]]+', 'g'), '');
}

while (length--) {
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ describe('randomatic', function() {
assert.equal(actual.length, 16);
});

it('should generate a random string excluding right square bracket on the `exclude` option', function() {
var actual = randomize('*', 16, {exclude: ']'});
assert(test(/[^\]]{16}/, actual));
assert.equal(actual.length, 16);
});

it('should generate a random string excluding array with one element(right square bracket) on the `exclude` option', function() {
var actual = randomize('*', 16, {exclude: [']']});
assert(test(/[^\]]{16}/, actual));
assert.equal(actual.length, 16);
});

it('should generate a radomized 16-character string', function() {
assert.equal(randomize('*', 16).length, 16);
});
Expand Down

0 comments on commit 8e78da8

Please sign in to comment.