Skip to content

Commit

Permalink
respect list of symbols (#50)
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
dillonstreator authored and brendanashworth committed Jan 14, 2021
1 parent c1946ea commit f0db9f2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ console.log(passwords);
### Available options
Any of these can be passed into the options object for each function.

| Name | Description | Default Value |
|--------------------------|-----------------------------------------------------|---------------|
| length | Integer, length of password. | 10 |
| numbers* | Boolean, put numbers in password. | false |
| symbols* | Boolean, put symbols in password. | false |
| lowercase* | Boolean, put lowercase in password | true |
| uppercase* | Boolean, use uppercase letters in password. | true |
| excludeSimilarCharacters | Boolean, exclude similar chars, like 'i' and 'l'. | false |
| exclude | String, characters to be excluded from password. | '' |
| strict | Boolean, password must include at least one character from each pool. | false |
| Name | Description | Default Value |
|--------------------------|-----------------------------------------------------------------------|---------------|
| length | Integer, length of password. | 10 |
| numbers* | Boolean, put numbers in password. | false |
| symbols* | Boolean or String, put symbols in password. | false |
| lowercase* | Boolean, put lowercase in password | true |
| uppercase* | Boolean, use uppercase letters in password. | true |
| excludeSimilarCharacters | Boolean, exclude similar chars, like 'i' and 'l'. | false |
| exclude | String, characters to be excluded from password. | '' |
| strict | Boolean, password must include at least one character from each pool. | false |

*At least one should be true.
9 changes: 9 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ var password = generator.generate({
strict: true // defaults to false
});

// Generate one password with provided list of symbols.
var password = generator.generate({
length: 15, // defaults to 10
numbers: true, // defaults to false
symbols: "!@#$%&*", // defaults to false
uppercase: true, // defaults to true
strict: true // defaults to false
});

// Generate ten bulk.
var passwords = generator.generateMultiple(10, {
length: 15,
Expand Down
13 changes: 12 additions & 1 deletion src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ var generate = function(options, pool) {
// If the option is not checked, ignore it.
if (options[rule.name] == false) return true;

// Treat symbol differently if explicit string is provided
if (rule.name === 'symbols' && typeof options[rule.name] === 'string') {
// Create a regular expression from the provided symbols
var re = new RegExp("["+options[rule.name]+"]");
return re.test(password);
};

// Run the regex on the password and return whether
// or not it matches.
return rule.rule.test(password);
Expand Down Expand Up @@ -107,7 +114,11 @@ self.generate = function(options) {
}
// symbols
if (options.symbols) {
pool += symbols;
if (typeof options.symbols === "string") {
pool += options.symbols;
} else {
pool += symbols;
}
}

// Throw error if pool is empty.
Expand Down
10 changes: 10 additions & 0 deletions test/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ describe('generate-password', function() {
assert.equal(passwords.length, amountToGenerate);
});

it('should respect explicit list of symbols when provided', function() {
var passwords = generator.generateMultiple(amountToGenerate, { length: 10, strict: true, symbols: "!", lowercase: true });

passwords.forEach(function (password) {
assert.notMatch(password, /[@#$%^&*()+_\-=}{[\]|:;"/?.><,`~]/, 'password does not have default symbols');
assert.match(password, /[!]/, 'password has provided symbol');
});
assert.equal(passwords.length, amountToGenerate);
});

it('should throw an error if rules don\'t correlate with length', function() {
assert.throws(function() {
generator.generate({length: 2, strict: true, symbols: true, numbers: true});
Expand Down

0 comments on commit f0db9f2

Please sign in to comment.