Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

random-char

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

random-char - npm Package Compare versions

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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc