Skip to content

Commit

Permalink
Added support for multiple character sets
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaskg committed Jun 2, 2023
1 parent a32502e commit d367de2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.3.0 / Jun 02, 2023
==================
* Added support for multiple character sets

1.2.3 / Oct 20, 2022
==================
* Fixed React support
Expand Down
13 changes: 12 additions & 1 deletion lib/charset.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ function Charset() {
}

Charset.prototype.setType = function(type) {
if (Array.isArray(type)) {
for (var i=0; i < type.length; i++) {
this.chars += this.getCharacters(type[i]);
}
}
else {
this.chars = this.getCharacters(type);
}
}

Charset.prototype.getCharacters = function(type) {
var chars;

var numbers = '0123456789';
Expand Down Expand Up @@ -36,7 +47,7 @@ Charset.prototype.setType = function(type) {
chars = type;
}

this.chars = chars;
return chars;
}

Charset.prototype.removeUnreadable = function() {
Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ describe("randomstring.generate(options)", function() {
});
});

it("accepts an array of charsets", function() {
var charset = ['alphabetic', '!'];
var testData = random({ length: testLength, charset: charset });
var search = testData.search(/[^A-Za-z!]/ig);
assert.equal(search, -1);
});

it("accepts an array of charsets async", function(done) {
var charset = ['alphabetic', '!'];
random({ length: testLength, charset: charset }, function(err, testData) {
var search = testData.search(/[^A-Za-z!]/ig);
assert.equal(search, -1);
done();
});
});

it("accepts readable option", function() {
var testData = random({ length: testLength, readable: true });
var search = testData.search(/[0OIl]/g);
Expand Down

0 comments on commit d367de2

Please sign in to comment.