Skip to content

Commit

Permalink
feat: charset option for String.random
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Nov 29, 2019
1 parent ceebe8d commit 2a20eeb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/string/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ _Note: When not applying this setting, accidental generation of same string is s
### `length: 10`

Desired length of result string

### `charset: null`

Fixed list of possible characters

```javascript
random({ charset: "abc" }); // "bacbccbbac"
```
21 changes: 16 additions & 5 deletions string/random.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
"use strict";

var isObject = require("type/object/is")
, ensureNaturalNumber = require("type/natural-number/ensure");
, ensureNaturalNumber = require("type/natural-number/ensure")
, ensureString = require("type/string/ensure");

var generated = Object.create(null), random = Math.random, uniqTryLimit = 100;

var getChunk = function () { return random().toString(36).slice(2); };

var getString = function (length) {
var str = getChunk();
var getString = function (length, charset) {
var str;
if (charset) {
var charsetLength = charset.length;
str = "";
for (var i = 0; i < length; ++i) {
str += charset.charAt(Math.floor(Math.random() * charsetLength));
}
return str;
}
str = getChunk();
if (length === null) return str;
while (str.length < length) str += getChunk();
return str.slice(0, length);
Expand All @@ -18,9 +28,10 @@ module.exports = function (/* options */) {
var options = arguments[0];
if (!isObject(options)) options = {};
var length = ensureNaturalNumber(options.length, { "default": 10 })
, isUnique = options.isUnique;
, isUnique = options.isUnique
, charset = ensureString(options.charset, { isOptional: true });

var str = getString(length);
var str = getString(length, charset);
if (isUnique) {
var count = 0;
while (generated[str]) {
Expand Down
10 changes: 9 additions & 1 deletion test/string/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ var isValidFormat = RegExp.prototype.test.bind(/^[a-z0-9]+$/);
describe("string/random", function () {
it("Should return string", function () { assert.equal(typeof random(), "string"); });
it("Should return by default string of length 10", function () {
assert.isAtLeast(random().length, 10);
assert.equal(random().length, 10);
});
it("Should support custom charset", function () {
var charset = "abc";
var result = random({ charset: charset });
assert.equal(result.length, 10);
for (var i = 0; i < result.length; ++i) {
assert.isAtLeast(charset.indexOf(result.charAt(i)), 0);
}
});
it("Should ensure unique string with `isUnique` option", function () {
assert.notEqual(random({ isUnique: true }), random({ isUnique: true }));
Expand Down

0 comments on commit 2a20eeb

Please sign in to comment.