.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / randomatic / index.js
1 /*!
2  * randomatic <https://github.com/jonschlinkert/randomatic>
3  *
4  * Copyright (c) 2014-2017, Jon Schlinkert.
5  * Released under the MIT License.
6  */
7
8 'use strict';
9
10 var isNumber = require('is-number');
11 var typeOf = require('kind-of');
12 var mathRandom = require('math-random');
13
14 /**
15  * Expose `randomatic`
16  */
17
18 module.exports = randomatic;
19 module.exports.isCrypto = !!mathRandom.cryptographic;
20
21 /**
22  * Available mask characters
23  */
24
25 var type = {
26   lower: 'abcdefghijklmnopqrstuvwxyz',
27   upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
28   number: '0123456789',
29   special: '~!@#$%^&()_+-={}[];\',.'
30 };
31
32 type.all = type.lower + type.upper + type.number + type.special;
33
34 /**
35  * Generate random character sequences of a specified `length`,
36  * based on the given `pattern`.
37  *
38  * @param {String} `pattern` The pattern to use for generating the random string.
39  * @param {String} `length` The length of the string to generate.
40  * @param {String} `options`
41  * @return {String}
42  * @api public
43  */
44
45 function randomatic(pattern, length, options) {
46   if (typeof pattern === 'undefined') {
47     throw new Error('randomatic expects a string or number.');
48   }
49
50   var custom = false;
51   if (arguments.length === 1) {
52     if (typeof pattern === 'string') {
53       length = pattern.length;
54
55     } else if (isNumber(pattern)) {
56       options = {};
57       length = pattern;
58       pattern = '*';
59     }
60   }
61
62   if (typeOf(length) === 'object' && length.hasOwnProperty('chars')) {
63     options = length;
64     pattern = options.chars;
65     length = pattern.length;
66     custom = true;
67   }
68
69   var opts = options || {};
70   var mask = '';
71   var res = '';
72
73   // Characters to be used
74   if (pattern.indexOf('?') !== -1) mask += opts.chars;
75   if (pattern.indexOf('a') !== -1) mask += type.lower;
76   if (pattern.indexOf('A') !== -1) mask += type.upper;
77   if (pattern.indexOf('0') !== -1) mask += type.number;
78   if (pattern.indexOf('!') !== -1) mask += type.special;
79   if (pattern.indexOf('*') !== -1) mask += type.all;
80   if (custom) mask += pattern;
81
82   // Characters to exclude
83   if (opts.exclude) {
84     var exclude = typeOf(opts.exclude) === 'string' ? opts.exclude : opts.exclude.join('');
85     exclude = exclude.replace(new RegExp('[\\]]+', 'g'), '');
86     mask = mask.replace(new RegExp('[' + exclude + ']+', 'g'), '');
87     
88     if(opts.exclude.indexOf(']') !== -1) mask = mask.replace(new RegExp('[\\]]+', 'g'), '');
89   }
90
91   while (length--) {
92     res += mask.charAt(parseInt(mathRandom() * mask.length, 10));
93   }
94   return res;
95 };