Skip to content

Commit

Permalink
special chars are off by default
Browse files Browse the repository at this point in the history
  • Loading branch information
hereandnow committed May 16, 2013
1 parent 564f4a6 commit 136d1bc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.

## Release History

- 0.1.1 do not contain special cars per default

- 0.1.0 Initial Release


Expand Down
7 changes: 4 additions & 3 deletions lib/random-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function _defaults (opts) {
length: opts.length || 8,
numeric: typeof opts.numeric === 'boolean' ? opts.numeric : true,
letters: typeof opts.letters === 'boolean' ? opts.letters : true,
special: typeof opts.special === 'boolean' ? opts.special : true
special: typeof opts.special === 'boolean' ? opts.special : false
};
}

Expand All @@ -33,10 +33,11 @@ function _buildChars (opts) {

module.exports = function randomString(opts) {
opts = _defaults(opts);
var x, _i, rn, rnd = '',
var i, rn,
rnd = '',
len = opts.length,
randomChars = _buildChars(opts);
for (x = _i = 1; 1 <= len ? _i <= len : _i >= len; x = 1 <= len ? ++_i : --_i) {
for (i = 1; i <= len; i++) {
rnd += randomChars.substring(rn = Math.floor(Math.random() * randomChars.length), rn + 1);
}
return rnd;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "random-string",
"description": "Simple Module for generating Random Strings",
"version": "0.1.0",
"version": "0.1.1",
"homepage": "https://github.com/valiton/node-random-string",
"author": {
"name": "Valiton GmbH, Bastian 'hereandnow' Behrens",
Expand Down
12 changes: 9 additions & 3 deletions test/random-string_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,40 @@ exports['randomString'] = {
},

'include_only_numbers': function(test) {
test.expect(1);
test.expect(3);
var result = randomString({
numeric: true,
letters: false,
special: false
});
test.ok(/^\d+$/.test(result), 'the random string should include only numbers');
test.equal(/^[a-zA-Z]+$/.test(result), false, 'the random string should not include letters');
test.equal(/^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$/.test(result), false, 'the random string should not include specials');
test.done();
},

'include_only_letters': function(test) {
test.expect(1);
test.expect(3);
var result = randomString({
numeric: false,
letters: true,
special: false
});
test.equal(/^\d+$/.test(result), false, 'the random string should not include numbers');
test.ok(/^[a-zA-Z]+$/.test(result), 'the random string should include only letters');
test.equal(/^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$/.test(result), false, 'the random string should not include specials');
test.done();
},

'include_only_specials': function(test) {
test.expect(1);
test.expect(3);
var result = randomString({
numeric: false,
letters: false,
special: true
});
test.equal(/^\d+$/.test(result), false, 'the random string should not include numbers');
test.equal(/^[a-zA-Z]+$/.test(result), false, 'the random string should not include letters');
test.ok(/^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$/.test(result), 'the random string should include only specials');
test.done();
}
Expand Down

0 comments on commit 136d1bc

Please sign in to comment.