-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { shuffle, initOptions, toArr, getRandomRes, testRange } = require('./util');
const NUMBERS = '0123456789';
const CHARS_LOWER = 'abcdefghijklmnopqrstuvwxyz';
const CHARS_UPPER = CHARS_LOWER.toUpperCase();
const SYMBOLS = "!@#$%^&*()[]";
class getPassWord {
constructor() {
}
getMyPassWord(opts) {
let { type, length } = initOptions(opts, {
length: 8,
type: 1
})
testRange(length <= 0 || length > 1000, 'Error length');
testRange(!([1, 2, 3, 4].includes(type)) , 'type should be 1 or 2 or 3 or 4');
testRange(length < type, 'length shound not less then type');
let result = '';
let evertLen = Math.floor(length / type);
switch(type) {
case 1 :
result += getRandomRes(NUMBERS, evertLen);
break;
case 2 :
result += getRandomRes(NUMBERS, evertLen) + getRandomRes(CHARS_LOWER, length - evertLen);
break;
case 3 :
result += getRandomRes(NUMBERS, evertLen) + getRandomRes(CHARS_LOWER, evertLen) + getRandomRes(CHARS_UPPER, length - 2 * evertLen);
break;
case 4 :
result += getRandomRes(NUMBERS, evertLen) + getRandomRes(CHARS_LOWER, evertLen) + getRandomRes(CHARS_UPPER, evertLen) + getRandomRes(SYMBOLS, length - 3 * evertLen);
break;
}
return shuffle(toArr(result)).join('');
}
}
module.exports = getPassWord;