Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ede66a0
commit 04416ea
Showing
6 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ node_js: | |
- "5" | ||
- "6" | ||
- "7" | ||
- "8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Pandemonium Random String | ||
* ========================== | ||
* | ||
* Function generating random strings. | ||
*/ | ||
var createRandom = require('./random.js').createRandom; | ||
|
||
/** | ||
* Constants. | ||
*/ | ||
var DEFAULT_ALPHABET = ( | ||
'abcdefghijklmnopqrstuvwxyz' + | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + | ||
'0123456789' | ||
); | ||
|
||
/** | ||
* Creating a function returning a random string composed from characters of | ||
* the given alphabet. | ||
* | ||
* @param {function} rng - RNG function returning uniform random. | ||
* @param {string|array} alphabet - Target alphabet. | ||
* @return {function} - The created function. | ||
*/ | ||
function createRandomString(rng, alphabet) { | ||
if (!alphabet) | ||
alphabet = DEFAULT_ALPHABET; | ||
|
||
var customRandom = createRandom(rng), | ||
randomCharacterIndex = customRandom.bind(null, 0, alphabet.length - 1); | ||
|
||
/** | ||
* Random string function. | ||
* | ||
* @param {number} length - Desired string length. | ||
* @return {number} | ||
*/ | ||
return function(length) { | ||
if (arguments.length > 1) { | ||
|
||
// We want to generate a string of variable length | ||
length = customRandom(arguments[0], arguments[1]); | ||
} | ||
|
||
var characters = new Array(length); | ||
|
||
for (var i = 0; i < length; i++) | ||
characters[i] = alphabet[randomCharacterIndex()]; | ||
|
||
return characters.join(''); | ||
}; | ||
} | ||
|
||
/** | ||
* Default random string using `Math.random`. | ||
*/ | ||
var randomString = createRandomString(Math.random); | ||
|
||
/** | ||
* Exporting. | ||
*/ | ||
randomString.createRandomString = createRandomString; | ||
module.exports = randomString; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters