Skip to content

Commit

Permalink
Merge pr 24
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaskg committed May 10, 2021
2 parents 0475716 + e719fe7 commit a0840ed
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ randomstring.generate({
- `alphabetic` - [a-z A-Z]
- `numeric` - [0-9]
- `hex` - [0-9 a-f]
- `binary` - [01]
- `octal` - [0-7]
- `custom` - any given characters
- `capitalization` - define whether the output should be lowercase / uppercase only. (default: null) [OPTIONAL]
- `lowercase`
Expand All @@ -69,7 +71,7 @@ randomstring.generate({

$ randomstring 7
> CpMg433

$ randomstring length=24 charset=github readable
> hthbtgiguihgbuttuutubugg

Expand Down
24 changes: 16 additions & 8 deletions lib/charset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ function Charset() {

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

var numbers = '0123456789';
var charsLower = 'abcdefghijklmnopqrstuvwxyz';
var charsUpper = charsLower.toUpperCase();
var hexChars = 'abcdef';


var numbers = '0123456789';
var charsLower = 'abcdefghijklmnopqrstuvwxyz';
var charsUpper = charsLower.toUpperCase();
var hexChars = 'abcdef';
var binaryChars = '01';
var octalChars = '01234567';

if (type === 'alphanumeric') {
chars = numbers + charsLower + charsUpper;
}
Expand All @@ -24,10 +26,16 @@ Charset.prototype.setType = function(type) {
else if (type === 'hex') {
chars = numbers + hexChars;
}
else if (type === 'binary') {
chars = binaryChars;
}
else if (type === 'octal') {
chars = octalChars;
}
else {
chars = type;
}

this.chars = chars;
}

Expand All @@ -51,4 +59,4 @@ Charset.prototype.removeDuplicates = function() {
this.chars = charMap.join('');
}

module.exports = exports = Charset;
module.exports = exports = Charset;
28 changes: 14 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ describe("randomstring.generate(options)", function() {
});
});

it("accepts 'numeric' as charset option", function() {
var testData = random({ length: testLength, charset: 'numeric' });
var search = testData.search(/\D/ig);
assert.equal(search, -1);
});

it("accepts 'numeric' as charset option async", function(done) {
random({ length: testLength, charset: 'numeric' }, function(err, testData) {
assert.equal(testData.length, testLength);
Expand All @@ -51,13 +45,7 @@ describe("randomstring.generate(options)", function() {
done();
});
});

it("accepts 'alphabetic' as charset option", function() {
var testData = random({ length: testLength, charset: 'alphabetic' });
var search = testData.search(/\d/ig);
assert.equal(search, -1);
});


it("accepts 'alphabetic' as charset option async", function(done) {
var testData = random({ length: testLength, charset: 'alphabetic' }, function(err, testData) {
var search = testData.search(/\d/ig);
Expand All @@ -71,6 +59,18 @@ describe("randomstring.generate(options)", function() {
var search = testData.search(/[^0-9a-f]/ig);
assert.equal(search, -1);
});

it("accepts 'binary' as charset option", function() {
var testData = random({ length: testLength, charset: 'binary' });
var search = testData.search(/[^01]/ig);
assert.equal(search, -1);
});

it("accepts 'octal' as charset option", function() {
var testData = random({ length: testLength, charset: 'octal' });
var search = testData.search(/[^0-7]/ig);
assert.equal(search, -1);
});

it("accepts custom charset", function() {
var charset = "abc";
Expand All @@ -87,7 +87,7 @@ describe("randomstring.generate(options)", function() {
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 a0840ed

Please sign in to comment.