1
0
mirror of https://github.com/S2-/securerandomstring.git synced 2025-08-04 11:10:06 +02:00

remove useless urlsafe: false option

This commit is contained in:
s2
2015-08-27 23:37:05 +02:00
parent 9a72a2cfe2
commit 7a9679a262
3 changed files with 6 additions and 25 deletions

View File

@@ -21,19 +21,16 @@ srs(function(err, sr) {
``` ```
### Options: length, urlsafe ### Options: length
Optionally, you can specify a 'length' option to specify a length. Optionally, you can specify a 'length' option to specify a length.
When 'urlsafe' is true (the default), potential `+` and `/` characters are replaced respectively with `-` and `_`.
This is a valid [base64url](https://en.wikipedia.org/wiki/Base64) string.
```javascript ```javascript
// sync // sync
var result = srs({length: 256, urlsafe:true}); var result = srs({length: 256});
// async // async
srs({length: 256, urlsafe:true}, function(err, sr) { srs({length: 256}, function(err, sr) {
console.log(sr); console.log(sr);
}); });
``` ```

View File

@@ -7,7 +7,6 @@ function srs(options, cb) {
} else { } else {
options = options || {}; options = options || {};
} }
options.urlsafe = typeof(options.urlsafe) === 'boolean' ? options.urlsafe : true;
var length = options['length'] || 32; var length = options['length'] || 32;
@@ -27,9 +26,7 @@ function srs(options, cb) {
function _finish (buf) { function _finish (buf) {
var string = buf.toString('base64'); var string = buf.toString('base64');
if (options.urlsafe) {
string = string.replace(/\//g,'_').replace(/\+/g,'-'); string = string.replace(/\//g,'_').replace(/\+/g,'-');
}
return string.substr(0, length); return string.substr(0, length);
} }

View File

@@ -50,28 +50,15 @@ srs({length: 256}, function(err, sr) {
); );
}); });
srs({length: 256}, function(err, sr) {
test('generate a urlsafe random string 256 chars long',
sr.length,
256
);
});
// sync tests // sync tests
test('generate a random string 32 chars long (sync)', srs().length, 32); test('generate a random string 32 chars long (sync)', srs().length, 32);
test('generate a random string 1 chars long (sync)', srs({length:1}).length, 1); test('generate a random string 1 chars long (sync)', srs({length:1}).length, 1);
test('generate a random string 256 chars long (sync)', srs({length:256}).length, 256); test('generate a random string 256 chars long (sync)', srs({length:256}).length, 256);
test('generate a urlsafe random string 256 chars long (sync)', srs({length:256}).length, 256);
//when generating, the default should be urlsafe
//in 2000 chars there should be at least one substitution //in 2000 chars there should be at least one substitution
test('check that the random string is urlsafe by default', (function() { test('check that the random string is urlsafe', (function() {
var s = srs({length: 2000}); var s = srs({length: 2000});
return s.indexOf('+') + s.indexOf('/') === -2; return s.indexOf('+') + s.indexOf('/') === -2;
})(), true); })(), true);
test('check that the useless urlsafe: false option works', (function() {
var s = srs({length: 2000, urlsafe: false});
return s.indexOf('_') + s.indexOf('-') === -2;
})(), true);