Skip to content

Commit

Permalink
Accept generate() when called without the options parameter (#14)
Browse files Browse the repository at this point in the history
When options is not provided, defaults are used. This allows calling
generate() and generateMultiple() without having to provide an empty
options object when defaults are wanted.
  • Loading branch information
kaworu authored and brendanashworth committed Oct 2, 2016
1 parent 07c662c commit d16c953
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $ npm install generate-password --save

## Usage

#### `generate(options)`
#### `generate([options])`

Generate one password with the given options. Returns a string.

Expand All @@ -28,7 +28,7 @@ var password = generator.generate({
console.log(password);
```

#### `generateMultiple(amount, options)`
#### `generateMultiple(amount[, options])`

Bulk generate multiple passwords at once, with the same options for all. Returns an array.

Expand Down
1 change: 1 addition & 0 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var generate = function(options, pool) {
// Generate a random password.
self.generate = function(options) {
// Set defaults.
options = options || {};
if (!options.hasOwnProperty('length')) options.length = 10;
if (!options.hasOwnProperty('numbers')) options.numbers = false;
if (!options.hasOwnProperty('symbols')) options.symbols = false;
Expand Down
12 changes: 11 additions & 1 deletion test/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ var generator = process.env.JSCOV ? require('../src-cov/generate') : require('..

describe('generate-password', function() {
describe('generate()', function() {
it('should accept to be called without the options parameter', function() {
assert.doesNotThrow(function () {
generator.generate();
});
});
it('should give password of correct length', function() {
var length = 12;

Expand Down Expand Up @@ -87,11 +92,16 @@ describe('generate-password', function() {
});

describe('generateMultiple()', function() {
it('should accept to be called without the options parameter', function() {
assert.doesNotThrow(function () {
generator.generateMultiple(1);
});
});
// should give right amount
it('should give right amount of passwords', function() {
var amount = 34;

var passwords = generator.generateMultiple(amount, {});
var passwords = generator.generateMultiple(amount);

assert.equal(passwords.length, amount);
});
Expand Down

0 comments on commit d16c953

Please sign in to comment.