-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (153 loc) · 5.04 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const getTypeTemplate = require("./lib/tokenTemplates/getTypeTemplate");
const getSyncToken = require("./lib/sync/genSync");
const getAsyncToken = require("./lib/async/genAsync");
const getTokenWithMyOwnCharacters = require("./lib/async/withMyOwnCharacters");
const syncValidatorTest = require("./lib/sync/syncValidator");
const asyncValidatorTest = require("./lib/async/asyncValidator");
const typeAndLengthCheck = require("./lib/errorHandlers/typeAndLengthCheck");
const validatorParametersCheck = require("./lib/errorHandlers/validatorParametersCheck");
const generator = {
currentType: "",
/**
* @description Create a Token with your own characters
*
* @param {string} characters string Sample: "abc123" generate a token with a,b,c,1,2,3 characters
* @param {number} length length of the token
* @example withMyOwnCharacters("abc123", 100)
* @returns {Promise<string>}
*/
withMyOwnCharacters: (characters, length) => {
this.currentType = "withMyOwnCharacters";
const isValid = typeAndLengthCheck(characters, length, this.currentType);
if (typeof isValid === "string") {
throw new Error(isValid);
} else {
return getTokenWithMyOwnCharacters(characters, length);
}
},
/**
* @description sync token generator
*
* @param {string} type "normal", "normal+", "medium", "medium+", "extra" or "onlyNumbers"
*
* - "normal" = (a-z)
* - "normal+" = (A-Z)
* - "medium" = (a-z + 0-9)
* - "medium+" = (A-Z + 0-9)
* - "extra" = (a-Z + 0-9)
* - "onlyNumbers" = (0-9)
*
* @param {number} length length of token
* @example genSync("extra", 100)
* @returns {string}
*/
genSync: (type, length) => {
this.currentType = "genSync";
const isValid = typeAndLengthCheck(type, length, this.currentType);
if (typeof isValid === "string") {
throw new Error(isValid);
} else {
let typeTemplate = getTypeTemplate(type);
return getSyncToken(typeTemplate, length);
}
},
/**
* @description async token generator
*
* @param {string} type "normal", "normal+", "medium", "medium+", "extra" or "onlyNumbers"
*
* - "normal" = (a-z)
* - "normal+" = (A-Z)
* - "medium" = (a-z + 0-9)
* - "medium+" = (A-Z + 0-9)
* - "extra" = (a-Z + 0-9)
* - "onlyNumbers" = (0-9)
*
* @param {number} length length of token
* @example genAsync("extra", 100)
* @returns {Promise<string>}
*/
genAsync: (type, length) => {
this.currentType = "genAsync";
const isValid = typeAndLengthCheck(type, length, this.currentType);
if (typeof isValid === "string") {
throw new Error(isValid);
} else {
let typeTemplate = getTypeTemplate(type);
return getAsyncToken(typeTemplate, length);
}
},
/**
* @description sync validator for genSync() and genAsync() or other Token
*
* @param {string} type same type as the generated Token
*
* @param {number} length same length as the generated Token
*
* @param {string} token The received token from genSync() or genAsync()
*
* @param {string | undefined} allowedPlusCharacters (This is optional) extra allowed characters in string -> "!%/"
*
* @example syncValidator("extra", 50, token, "")
* @returns {boolean}
*/
syncValidator: (type, length, token, allowedPlusCharacters) => {
this.currentType = "syncValidator";
const isValid = typeAndLengthCheck(type, length, this.currentType);
const checkValidatorPar = validatorParametersCheck(
token,
allowedPlusCharacters,
this.currentType
);
if (typeof isValid === "string") {
throw new Error(isValid);
} else if (typeof checkValidatorPar === "string") {
throw new Error(checkValidatorPar);
} else {
let typeTemplate = getTypeTemplate(type);
return syncValidatorTest(
typeTemplate,
length,
token,
allowedPlusCharacters
);
}
},
/**
* @description async validator for genSync() and genAsync() or other Token
*
* @param {string} type same type as the generated Token
*
* @param {number} length same length as the generated Token
*
* @param {string} token The received token from genSync() or genAsync()
*
* @param {string | undefined} allowedPlusCharacters (This is optional) extra allowed characters in string -> "!%/"
*
* @example asyncValidator("extra", 50, token, "")
* @returns {Promise<boolean>}
*/
asyncValidator: (type, length, token, allowedPlusCharacters) => {
this.currentType = "asyncValidator";
const isValid = typeAndLengthCheck(type, length, this.currentType);
const checkValidatorPar = validatorParametersCheck(
token,
allowedPlusCharacters,
this.currentType
);
if (typeof isValid === "string") {
throw new Error(isValid);
} else if (typeof checkValidatorPar === "string") {
throw new Error(checkValidatorPar);
} else {
let typeTemplate = getTypeTemplate(type);
return asyncValidatorTest(
typeTemplate,
length,
token,
allowedPlusCharacters
);
}
},
};
module.exports = generator;