-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (58 loc) · 1.77 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const Type = require("./modules/Type.js");
const RangeContainer = require("./modules/rangeContainer.js");
const NUMBER = 'NUMBER';
const LOWER = 'LOWER';
const UPPER = 'UPPER';
let algorithms = {
alphaNumeric :[1,2,3],
alphaNumericLowerCase : [1,3],
alpha : [2,3],
alphaLower :[3],
alphaUpper : [2],
numeric:[1],
};
let rangeContainer = new RangeContainer([
new Type(NUMBER,1,48,57 ),
new Type(UPPER ,2,65,90 ),
new Type(LOWER ,3,97,122),
]);
module.exports = class Random {
static chooseChar(ranNum){
return String.fromCharCode(ranNum);
}
static chooseNum({min,max}){
return Math.floor(Math.random() * (max-min) + min)
}
static generate(length,algorithm){
let id = '';
while(length--){
let config = algorithms[algorithm];
let range = rangeContainer.selectRange(config);
let randomNumber = this.chooseNum(range);
let character = this.chooseChar(randomNumber);
id += character;
}
return id;
}
static RandomBetween (min, max){
return Random.chooseNum({min,max});
}
static randomStringAlphaNumeric(length){
return Random.generate(length,"alphaNumeric");
}
static randomStringAlphaNumericLower(length){
return Random.generate(length,"alphaNumericLowerCase");
}
static randomStringLowerCase(length) {
return Random.generate(length,"alphaLower");
}
static randomStringUpperCase(length) {
return Random.generate(length,"alphaUpper");
}
static randomStringMixedCase(length) {
return Random.generate(length,"alpha");
}
static randomStringNumeric(length){
return Random.generate(length,"numeric");
}
}