diff --git a/.verb.md b/.verb.md index 1585a41..08369d9 100644 --- a/.verb.md +++ b/.verb.md @@ -8,12 +8,14 @@ var randomize = require('{%= name %}'); ```js randomize(pattern, length, options); +randomize.isCrypto; ``` - `pattern` **{String}**: (required) The pattern to use for randomizing - `length` **{Number}**: (optional) The length of the string to generate - `options` **{Object}**: (optional) See available [options](#options) +- `randomize.isCrypto` will be `true` when a cryptographically secure function is being used to generate random numbers. The value will be `false` when the function in use is `Math.random`. ### pattern diff --git a/index.js b/index.js index bb06289..b6df991 100644 --- a/index.js +++ b/index.js @@ -9,12 +9,14 @@ var isNumber = require('is-number'); var typeOf = require('kind-of'); +var mathRandom = require('math-random'); /** * Expose `randomatic` */ module.exports = randomatic; +module.exports.isCrypto = !!mathRandom.cryptographic; /** * Available mask characters @@ -78,7 +80,7 @@ function randomatic(pattern, length, options) { if (custom) mask += pattern; while (length--) { - res += mask.charAt(parseInt(Math.random() * mask.length, 10)); + res += mask.charAt(parseInt(mathRandom() * mask.length, 10)); } return res; }; diff --git a/package.json b/package.json index 01961ee..360f70c 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ }, "dependencies": { "is-number": "^4.0.0", - "kind-of": "^6.0.0" + "kind-of": "^6.0.0", + "math-random": "^1.0.1" }, "devDependencies": { "ansi-bold": "^0.1.1", diff --git a/test.js b/test.js index 289bd52..ba78906 100644 --- a/test.js +++ b/test.js @@ -16,6 +16,10 @@ function test(re, str) { } describe('randomatic', function() { + it('should export an isCrypto boolean property', function() { + assert.equal(typeof randomize.isCrypto, 'boolean'); + }); + it('should throw an error when no arguments are passed:', function() { assert.throws(function() { randomize();