Comparing version 1.3.0 to 1.4.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [1.4.0](https://github.com///compare/v1.3.0...v1.4.0) (2019-11-29) | ||
### Features | ||
- `charset` option for `String.random` ([2a20eeb](https://github.com///commit/2a20eebc5ae784e5c1aacd2c54433fe92a9464c9)) | ||
- `String.prototype.includes` implementation ([ceebe8d](https://github.com///commit/ceebe8dfd6f479d6a7e7b6cd79369291869ee2dd)) | ||
## [1.3.0](https://github.com///compare/v1.2.1...v1.3.0) (2019-11-28) | ||
@@ -7,0 +14,0 @@ |
@@ -24,1 +24,9 @@ # `String.random(options = { ... })` _(ext/string/random)_ | ||
Desired length of result string | ||
### `charset: null` | ||
Fixed list of possible characters | ||
```javascript | ||
random({ charset: "abc" }); // "bacbccbbac" | ||
``` |
{ | ||
"name": "ext", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "JavaScript utilities with respect to emerging standard", | ||
@@ -5,0 +5,0 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (http://www.medikoo.com/)", |
@@ -28,3 +28,7 @@ # ext | ||
- [`entries`](docs/object/entries.md) | ||
- `String` | ||
- [`random`](docs/string/random.md) | ||
- `String.prototype` | ||
- [`includes`](docs/string_/includes.md) | ||
- `Thenable.prototype` | ||
- [`finally`](docs/thenable_/finally.md) |
"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"); | ||
@@ -10,4 +11,13 @@ var generated = Object.create(null), random = Math.random, uniqTryLimit = 100; | ||
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; | ||
@@ -22,5 +32,6 @@ while (str.length < length) str += getChunk(); | ||
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) { | ||
@@ -27,0 +38,0 @@ var count = 0; |
@@ -11,4 +11,12 @@ "use strict"; | ||
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 () { | ||
@@ -15,0 +23,0 @@ assert.notEqual(random({ isUnique: true }), random({ isUnique: true })); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25543
48
434
34