simple-random
Version:
A simple flexible javascript library that creates random alpha-numeric strings. Works in both NodeJS and the browser.
57 lines (46 loc) • 2.2 kB
Markdown
//travis-ci.org/dortzur/simple-random.svg?branch=master)](https://travis-ci.org/dortzur/simple-random) [](http://badge.fury.io/js/simple-random) [](https://www.npmjs.com/package/simple-random)
A simple flexible javascript library that creates random alpha-numeric strings. Very useful when creating random names for files, folders. Can also create secure random strings for temporary passwords and salts.
Works in both NodeJS and the browser.
if the `secure` option is set to `true`, we can create a random byte seed with a cryptography library as opposed to Math.random().
In Node we use the default `crypto` library.
In webpack / browser versions we use the Web Cryptography API if supported.
```bash
npm install simple-random --save
```
```javascript
var sr = require('simple-random');
var randomString = sr(); // Generates a 16 character alpha-numeric string.
// Example output: "pnxTcl2nOBqTNFQR"
```
```javascript
var sr=require('simple-random/browser');
var randomString = sr(); // Generates a 16 character alpha-numeric string.
// Example output: "pnxTcl2nOBqTNFQR"
```
```html
<script src="simple-random/dist/simple_random.js"></script>
<script>
var randomString = window.simpleRandom();
//Secure Random
var secureRandom;
if (simpleRandom.isSecureSupported){
secureRandom = simpleRandom({secure:true})
}
</script>
```
- `length`: The length of the alpha-numeric string (default 16).
- `digits`: should allow digits in random string (default true).
- `letters`: should allow letters in random string (default true).
- `caseSensitive`: if set to false, only lowercase letter characters will be used (default true).
- `secure`: Whether or not to use the `crypto` library for secure random byte seed as opposed to `Math.random()` (default false).
- `prefix`: Prefix.
- `suffix`: Suffix.
- `chars`: A string containing all the characters to draw from, defaults to all alpha-numeric characters (overrides digits, letters and caseSensitive flags).
[![Build Status](https: