random-char
Advanced tools
Comparing version 1.0.3 to 1.0.4
44
index.js
'use strict'; | ||
var isNil = require('is-nil'); | ||
var isObject = require('is-object'); | ||
var toString = require('to-str'); | ||
@@ -14,18 +15,41 @@ var randomNatural = require('random-natural'); | ||
pools.alpha = pools.lower + pools.upper; | ||
pools['all'] = pools.lower + pools.upper + pools.number + pools.symbol; | ||
pools['default'] = pools['all']; | ||
pools['undefined'] = pools['all']; | ||
pools.alpha = pools.lower + pools.upper; | ||
pools['all'] = pools.lower + pools.upper + pools.number + pools.symbol; | ||
module.exports = function (options) { | ||
module.exports = function (pool) { | ||
if (!isObject(options)) { | ||
if (isNil(options)) { | ||
options = { pool: pools.all }; | ||
} else { | ||
options = toString(options); | ||
options = { pool: pools[options] || options }; | ||
} | ||
} | ||
if (isNil(pool)) { | ||
pool = 'all'; | ||
var pool; | ||
if (options.pool) { | ||
pool = options.pool; | ||
} else if (options.lower) { | ||
pool = pools.lower; | ||
} else if (options.upper) { | ||
pool = pools.upper; | ||
} else if (options.alpha) { | ||
pool = pools.alpha; | ||
} else if (options.number) { | ||
pool = pools.number; | ||
} else if (options.symbol) { | ||
pool = pools.symbol; | ||
} else { | ||
pool = pools.all; | ||
} | ||
pool = toString(pool) || 'all'; | ||
pool = pools[pool] || pool; | ||
pool = toString(pool); | ||
return pool.charAt(randomNatural(0, pool.length - 1)); | ||
return pool.charAt(randomNatural({ | ||
min: 0, | ||
max: pool.length - 1, | ||
inspected: true | ||
})); | ||
}; |
{ | ||
"name": "random-char", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Return a random character.", | ||
@@ -51,6 +51,6 @@ "main": "index.js", | ||
"is-nil": "^1.0.1", | ||
"onchange": "^2.4.0", | ||
"random-natural": "^1.0.2", | ||
"is-object": "^1.0.1", | ||
"random-natural": "^1.0.3", | ||
"to-str": "^1.0.0" | ||
} | ||
} |
@@ -19,2 +19,5 @@ # random-char | ||
> For more use-cases see the [tests](https://github.com/mock-end/random-char/blob/master/test/spec/index.js) | ||
```js | ||
@@ -24,7 +27,10 @@ var randomChar = require('random-char'); | ||
// API | ||
// - randomChar(); | ||
// - randomChar(pool); | ||
// - randomChar([poolName]); | ||
// - randomChar([options]); | ||
randomChar(); | ||
// => 'k' | ||
``` | ||
By default `randomChar()` will return a string with random character from the following pool. | ||
By default it will return a string with random character from the following pool: | ||
@@ -35,26 +41,48 @@ ``` | ||
Optionally specify a pool and the character will be generated with characters only from that pool. | ||
Optionally specify a pool: | ||
Specify pool by name: | ||
```js | ||
randomChar('alpha'); // or | ||
randomChar({alpha: true}); | ||
// => 'm' | ||
- lower - `'abcdefghijklmnopqrstuvwxyz'` | ||
- upper - `'ABCDEFGHIJKLMNOPQRSTUVWXYZ'` | ||
- number - `'0123456789'` | ||
- symbol - `'~!@#$%^&()*_+-={}[]'` | ||
- alpha - `'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'` | ||
- all - `'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&()*_+-={}[]'` | ||
randomChar('upper'); // or | ||
randomChar({upper: true}); | ||
// => 'Z' | ||
randomChar('lower'); // or | ||
randomChar({lower: true}); | ||
// => 'j' | ||
```js | ||
randomChar('upper'); | ||
// => 'M' | ||
randomChar('number'); // or | ||
randomChar({number: true}); | ||
// => '7' | ||
randomChar('symbols'); // or | ||
randomChar({symbols: true}); | ||
// => '%' | ||
``` | ||
Specify pool by candidate characters: | ||
Optionally specify a pool and the character will be generated with characters only from that pool: | ||
```js | ||
randomChar('aeiou'); | ||
// => 'i' | ||
randomChar('abcde'); // or | ||
randomChar({pool: 'abcde'}); | ||
// => 'c' | ||
``` | ||
## Related | ||
- [random-integral](https://github.com/mock-end/random-integral) - Return a random integer. | ||
- [random-natural](https://github.com/mock-end/random-natural) - Return a random natural number. | ||
- [random-decimal](https://github.com/mock-end/random-decimal) - Return a random decimal. | ||
- [random-floating](https://github.com/mock-end/random-floating) - Return a random floating point number. | ||
- [random-index](https://github.com/mock-end/random-index) - Return a random array-like index. | ||
- [random-binary](https://github.com/mock-end/random-binary) - Return a random binary number. | ||
- [random-octal](https://github.com/mock-end/random-octal) - Return a random octal number. | ||
- [random-hexadecimal](https://github.com/mock-end/random-hexadecimal) - Return a random hexadecimal number. | ||
- [random-unicode](https://github.com/mock-end/random-unicode) - Return a random unicode. | ||
- [random-bool](https://github.com/mock-end/random-bool) - Return a random boolean (true/false). | ||
## Contributing | ||
@@ -61,0 +89,0 @@ |
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
6299
45
90
+ Addedis-object@^1.0.1
- Removedonchange@^2.4.0
- Removedanymatch@1.3.2(transitive)
- Removedarr-diff@2.0.04.0.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarr-union@3.1.0(transitive)
- Removedarray-unique@0.2.10.3.2(transitive)
- Removedarrify@1.0.1(transitive)
- Removedassign-symbols@1.0.0(transitive)
- Removedasync-each@1.0.6(transitive)
- Removedatob@2.1.2(transitive)
- Removedbase@0.11.2(transitive)
- Removedbinary-extensions@1.13.1(transitive)
- Removedbindings@1.5.0(transitive)
- Removedbraces@1.8.52.3.2(transitive)
- Removedcache-base@1.0.1(transitive)
- Removedchokidar@1.5.2(transitive)
- Removedclass-utils@0.3.6(transitive)
- Removedcollection-visit@1.0.0(transitive)
- Removedcomponent-emitter@1.3.1(transitive)
- Removedcopy-descriptor@0.1.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedcross-spawn@4.0.2(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddecode-uri-component@0.2.2(transitive)
- Removeddefine-property@0.2.51.0.02.0.2(transitive)
- Removedexpand-brackets@0.1.52.1.4(transitive)
- Removedexpand-range@1.8.2(transitive)
- Removedextend-shallow@2.0.13.0.2(transitive)
- Removedextglob@0.3.22.0.4(transitive)
- Removedfile-uri-to-path@1.0.0(transitive)
- Removedfilename-regex@2.0.1(transitive)
- Removedfill-range@2.2.44.0.0(transitive)
- Removedfor-in@1.0.2(transitive)
- Removedfor-own@0.1.5(transitive)
- Removedfragment-cache@0.2.1(transitive)
- Removedfsevents@1.2.13(transitive)
- Removedget-value@2.0.6(transitive)
- Removedglob-base@0.3.0(transitive)
- Removedglob-parent@2.0.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhas-value@0.3.11.0.0(transitive)
- Removedhas-values@0.1.41.0.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-accessor-descriptor@1.0.1(transitive)
- Removedis-binary-path@1.0.1(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-data-descriptor@1.0.1(transitive)
- Removedis-descriptor@0.1.71.0.3(transitive)
- Removedis-dotfile@1.0.3(transitive)
- Removedis-equal-shallow@0.1.3(transitive)
- Removedis-extendable@0.1.11.0.1(transitive)
- Removedis-extglob@1.0.0(transitive)
- Removedis-glob@2.0.1(transitive)
- Removedis-number@2.1.03.0.04.0.0(transitive)
- Removedis-plain-object@2.0.4(transitive)
- Removedis-posix-bracket@0.1.1(transitive)
- Removedis-primitive@2.0.0(transitive)
- Removedis-windows@1.0.2(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedisobject@2.1.03.0.1(transitive)
- Removedkind-of@3.2.24.0.06.0.3(transitive)
- Removedlru-cache@4.1.5(transitive)
- Removedmap-cache@0.2.2(transitive)
- Removedmap-visit@1.0.0(transitive)
- Removedmath-random@1.0.4(transitive)
- Removedmicromatch@2.3.113.1.10(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmixin-deep@1.3.2(transitive)
- Removedms@2.0.0(transitive)
- Removednan@2.22.0(transitive)
- Removednanomatch@1.2.13(transitive)
- Removednormalize-path@2.1.1(transitive)
- Removedobject-copy@0.1.0(transitive)
- Removedobject-visit@1.0.1(transitive)
- Removedobject.omit@2.0.1(transitive)
- Removedobject.pick@1.3.0(transitive)
- Removedonchange@2.5.0(transitive)
- Removedparse-glob@3.0.4(transitive)
- Removedpascalcase@0.1.1(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedposix-character-classes@0.1.1(transitive)
- Removedpreserve@0.2.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedpseudomap@1.0.2(transitive)
- Removedrandomatic@3.1.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedreaddirp@2.2.1(transitive)
- Removedregex-cache@0.4.4(transitive)
- Removedregex-not@1.0.2(transitive)
- Removedremove-trailing-separator@1.1.0(transitive)
- Removedrepeat-element@1.1.4(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedresolve-url@0.2.1(transitive)
- Removedret@0.1.15(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsafe-regex@1.1.0(transitive)
- Removedset-value@2.0.1(transitive)
- Removedsnapdragon@0.8.2(transitive)
- Removedsnapdragon-node@2.1.1(transitive)
- Removedsnapdragon-util@3.0.1(transitive)
- Removedsource-map@0.5.7(transitive)
- Removedsource-map-resolve@0.5.3(transitive)
- Removedsource-map-url@0.4.1(transitive)
- Removedsplit-string@3.1.0(transitive)
- Removedstatic-extend@0.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedto-object-path@0.3.0(transitive)
- Removedto-regex@3.0.2(transitive)
- Removedto-regex-range@2.1.1(transitive)
- Removedunion-value@1.0.1(transitive)
- Removedunset-value@1.0.0(transitive)
- Removedurix@0.1.0(transitive)
- Removeduse@3.1.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwhich@1.3.1(transitive)
- Removedyallist@2.1.2(transitive)
Updatedrandom-natural@^1.0.3