randomatic
Advanced tools
Comparing version 0.1.4 to 1.0.0
{ | ||
"name": "randomatic", | ||
"version": "0.1.4", | ||
"version": "1.0.0", | ||
"main": [ | ||
"index.js" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Jon Schlinkert", | ||
"homepage": "https://github.com/jonschlinkert" | ||
} | ||
] | ||
} |
86
index.js
@@ -1,8 +0,16 @@ | ||
/** | ||
* randomatic | ||
* Inspired by http://stackoverflow.com/a/10727155/1267639 | ||
/*! | ||
* randomatic <https://github.com/jonschlinkert/randomatic> | ||
* | ||
* Copyright (c) 2014 Jon Schlinkert | ||
* Licensed under the MIT License (MIT). | ||
* Licensed under the MIT License (MIT) | ||
* | ||
* Originally inspired by <http://stackoverflow.com/a/10727155/1267639> | ||
*/ | ||
var isNumber = require('is-number'); | ||
/** | ||
* Available mask characters | ||
*/ | ||
var type = { | ||
@@ -15,34 +23,60 @@ lower: 'abcdefghijklmnopqrstuvwxyz', | ||
module.exports = function (chars, length, opts) { | ||
opts = opts || {}; | ||
/** | ||
* Generate random character sequences of a specified `length`, | ||
* based on the given `pattern`. | ||
* | ||
* @param {String} `pattern` The pattern to use for generating the random string. | ||
* @param {String} `length` The length of the string to generate. | ||
* @param {String} `options` | ||
* @return {String} | ||
* @api public | ||
*/ | ||
module.exports = function randomatic(pattern, length, options) { | ||
if (arguments.length === 1 && typeof pattern === 'number') { | ||
options = length; | ||
length = pattern; | ||
pattern = '*'; | ||
} | ||
var opts = options || {}; | ||
var mask = ''; | ||
var res = ''; | ||
opts.chars = opts.chars || ''; | ||
// if `length` is an object, since `chars` is currently the only | ||
// option, use the length of the special chars as `length` | ||
if(typeof length === 'object') { | ||
opts = length; | ||
length = opts.chars.length; | ||
if (isNumber(pattern)) { | ||
length = pattern; | ||
} else { | ||
length = opts.chars.length; | ||
} | ||
pattern = opts.chars; | ||
} | ||
// if no `length` is defined, use the length of the pattern | ||
if(typeof length === 'undefined') { | ||
length = chars.length; | ||
length = pattern.length; | ||
} | ||
type.custom = opts.chars; | ||
type.all = Object.keys(type).map(function (key) { | ||
return type[key]; | ||
}).join(''); | ||
// Characters to be used | ||
if (pattern.indexOf('?') > -1) mask += opts.chars; | ||
if (pattern.indexOf('a') > -1) mask += type.lower; | ||
if (pattern.indexOf('A') > -1) mask += type.upper; | ||
if (pattern.indexOf('0') > -1) mask += type.numeric; | ||
if (pattern.indexOf('!') > -1) mask += type.special; | ||
if (pattern.indexOf('*') > -1) mask += type.all; | ||
var mask = ''; | ||
var result = []; | ||
if (mask.length === 0 || mask == null) { | ||
mask += pattern; | ||
} | ||
// Allow a custom string to be randomized (opts.chars) | ||
if (chars.indexOf('?') > -1) {mask += type.custom;} | ||
if (chars.indexOf('a') > -1) {mask += type.lower; } | ||
if (chars.indexOf('A') > -1) {mask += type.upper; } | ||
if (chars.indexOf('0') > -1) {mask += type.numeric; } | ||
if (chars.indexOf('!') > -1) {mask += type.special; } | ||
if (chars.indexOf('*') > -1) {mask += type.all; } | ||
for (var i = length; i > 0; --i) { | ||
result.push(mask[Math.round(Math.random() * (mask.length - 1))]); | ||
var len = mask.length - 1; | ||
while (length--) { | ||
res += mask[parseInt(Math.random() * len)]; | ||
} | ||
return result.join(''); | ||
}; | ||
return res; | ||
}; |
{ | ||
"name": "randomatic", | ||
"description": "Generate randomized strings of a specified length from patterns of numeric, alpha-numeric, alphabetical, special or custom characters.", | ||
"version": "0.1.4", | ||
"description": "Generate randomized strings of a specified length, fast. Only the length is necessary, but you can optionally generate patterns using any combination of numeric, alpha-numeric, alphabetical, special or custom characters.", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/randomatic", | ||
@@ -17,9 +17,11 @@ "author": { | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/randomatic/blob/master/LICENSE-MIT" | ||
} | ||
], | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/randomatic/blob/master/LICENSE-MIT" | ||
}, | ||
"main": "index.js", | ||
"devDependencies": { | ||
"mocha": "^2.0.1", | ||
"should": "^4.4.1" | ||
}, | ||
"keywords": [ | ||
@@ -37,7 +39,5 @@ "alpha", | ||
], | ||
"devDependencies": { | ||
"chai": "~1.9.1", | ||
"mocha": "~1.18.2", | ||
"verb": "~0.2.5" | ||
"dependencies": { | ||
"is-number": "^0.1.1" | ||
} | ||
} |
102
README.md
@@ -1,15 +0,12 @@ | ||
# randomatic [![NPM version](https://badge.fury.io/js/randomatic.png)](http://badge.fury.io/js/randomatic) | ||
# randomatic [![NPM version](https://badge.fury.io/js/randomatic.svg)](http://badge.fury.io/js/randomatic) | ||
> Generate randomized strings of a specified length from patterns of numeric, alpha-numeric, alphabetical, special or custom characters. | ||
> Generate randomized strings of a specified length, fast. Only the length is necessary, but you can optionally generate patterns using any combination of numeric, alpha-numeric, alphabetical, special or custom characters. | ||
## Quickstart | ||
## Install with [npm](npmjs.org) | ||
### [npm](https://npmjs.org/) | ||
```bash | ||
npm i randomatic --save | ||
``` | ||
### Install with [bower](https://github.com/bower/bower) | ||
### [bower](https://github.com/bower/bower) | ||
```bash | ||
@@ -19,25 +16,38 @@ bower install randomatic --save | ||
## Tests | ||
Run the tests | ||
## Usage | ||
```bash | ||
mocha | ||
```js | ||
var randomize = require('randomatic'); | ||
``` | ||
## Documentation | ||
```js | ||
var randomize = require('randomatic'); | ||
## API | ||
``` | ||
randomize(pattern, length, options); | ||
``` | ||
### Params | ||
- `pattern` **{String}**: The pattern to use for randomizing | ||
- `length` **{Object}**: The length of the string to generate | ||
The following parameters can be passed (`randomize(Pattern, Length, Options)`) | ||
#### pattern | ||
Type: `String` (required) | ||
### pattern | ||
Default: `undefined` | ||
> The pattern to use for randomizing | ||
Patterns can contain any combination of the below characters, specified in any order. | ||
**Example:** | ||
To generate a 10-character randomized string using all available characters: | ||
```js | ||
randomize('*', 10); | ||
//=> | ||
randomize('Aa0!', 10); | ||
//=> | ||
``` | ||
* `a`: Lowercase alpha characters (`abcdefghijklmnopqrstuvwxyz'`) | ||
@@ -50,13 +60,9 @@ * `A`: Uppercase alpha characters (`ABCDEFGHIJKLMNOPQRSTUVWXYZ'`) | ||
Patterns can contain any combination of the above characters, in any order. For example, `randomize('Aa0!', 10)` will generate a 10-character randomized string with all of the specified patterns. | ||
#### length | ||
Type: `Number` (optional) | ||
### length | ||
Default: `undefined` | ||
> the length of the string to generate | ||
Specify the number of characters/digits to generate in the output. | ||
**Examples:** | ||
Examples: | ||
* `randomize('A', 5)` will generate a 5-character, uppercase, alphabetical, randomized string, e.g. `KDJWJ`. | ||
@@ -75,11 +81,17 @@ * `randomize('0', 2)` will generate a 2-digit random number | ||
...and so on. | ||
These are just examples, [see the tests](./test.js) for more use cases and examples. | ||
#### options | ||
Type: `Object` (optional) | ||
## options | ||
#### chars | ||
Type: `String` | ||
Default: `undefined` | ||
Currently the only option is `chars`, which allows you to define a custom string to be randomized. For example: | ||
Define a custom string to be randomized. | ||
**Example:** | ||
* `randomize('?', 20, {chars: 'jonschlinkert'})` will generate a 20-character randomized string from the letters contained in `jonschlinkert`. | ||
@@ -89,5 +101,6 @@ * `randomize('?', {chars: 'jonschlinkert'})` will generate a 13-character randomized string from the letters contained in `jonschlinkert`. | ||
## Usage Examples | ||
* `randomize('A', 4)` (_whitespace insenstive_) would result in randomized 4-digit uppercase letters, like, `ZAKH`, `UJSL`... and so on. | ||
* `randomize('A', 4)` (_whitespace insenstive_) would result in randomized 4-digit uppercase letters, like, `ZAKH`, `UJSL`... etc. | ||
* `randomize('AAAA')` is equivelant to `randomize('A', 4)` | ||
@@ -101,21 +114,26 @@ * `randomize('AAA0')` and `randomize('AA00')` and `randomize('A0A0')` are equivelant to `randomize('A0', 4)` | ||
_The order in which the characters are provided has no impact on the outcome._ | ||
_The order in which the characters are defined is insignificant._ | ||
## Contributing | ||
Find a bug? Have a feature request? Please [create an Issue](https://github.com/jonschlinkert/randomatic/issues). | ||
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality, | ||
and run `docs` in the command line to build the docs with [Verb](https://github.com/assemble/verb). | ||
Pull requests are also encouraged, and if you find this project useful please consider "starring" it to show your support! Thanks! | ||
## Running tests | ||
## Authors | ||
Install dev dependencies: | ||
```bash | ||
npm install -d && mocha | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/randomatic/issues) | ||
## Author | ||
**Jon Schlinkert** | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
## License | ||
Copyright (c) 2014 Jon Schlinkert, contributors. | ||
Copyright (c) 2014 Jon Schlinkert | ||
Released under the MIT license | ||
@@ -125,2 +143,2 @@ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on May 19, 2014._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on December 13, 2014. To update, run `npm i -g verb && verb`._ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
22425
2
12
342
0
138
1
1
+ Addedis-number@^0.1.1
+ Addedis-number@0.1.1(transitive)