/
index.js
202 lines (168 loc) · 6.22 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
function isNum(value,floats=null){
if(!value) return false;
let regexp = new RegExp(`^[1-9][0-9]*.[0-9]{${floats}}$|^0.[0-9]{${floats}}$`);
return typeof value === 'number' && floats?regexp.test(String(value)):true;
}
function anysicIntLength(minLength,maxLength){
let result_str = '';
if(minLength){
switch(maxLength){
case undefined:
result_str = result_str.concat(`{${minLength-1}}`);
break;
case null:
result_str = result_str.concat(`{${minLength-1},}`);
break;
default:
result_str = result_str.concat(`{${minLength-1},${maxLength-1}}`);
}
}else{
result_str = result_str.concat('*');
}
return result_str;
}
function isInt(value,minLength=null,maxLength=undefined){
if(!isNum(value)) return false;
let regexp = new RegExp(`^-?[1-9][0-9]${anysicIntLength(minLength,maxLength)}$`);
return regexp.test(value.toString());
}
function isPInt(value,minLength=null,maxLength=undefined) {
if(!isNum(value)) return false;
let regexp = new RegExp(`^[1-9][0-9]${anysicIntLength(minLength,maxLength)}$`);
return regexp.test(value.toString());
}
function isNInt(value,minLength=null,maxLength=undefined){
if(!isNum(value)) return false;
let regexp = new RegExp(`^-[1-9][0-9]${anysicIntLength(minLength,maxLength)}$`);
return regexp.test(value.toString());
}
function checkIntRange(value,minInt,maxInt=9007199254740991){
return Boolean(isInt(value) && (Boolean(minInt!=undefined && minInt!=null)?value>=minInt:true) && (value<=maxInt));
}
function isTel(value) {
if(!value) return false;
return /^1[3,4,5,6,7,8,9][0-9]{9}$/.test(value.toString());
}
function isFax(str) {
if(!str) return false;
return /^([0-9]{3,4})?[0-9]{7,8}$|^([0-9]{3,4}-)?[0-9]{7,8}$/.test(str);
}
function isEmail(str) {
if(!str) return false;
return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(str);
}
function isQQ(value) {
if(!value) return false;
return /^[1-9][0-9]{4,12}$/.test(value.toString());
}
function isURL(str) {
if(!str) return false;
return /^(https:\/\/|http:\/\/|ftp:\/\/|rtsp:\/\/|mms:\/\/)?[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/.test(str);
}
function isIP(str) {
if(!str) return false;
return /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/.test(str);
}
function isIPv6(str){
if(!str) return false;
return Boolean(str.match(/:/g)?str.match(/:/g).length<=7:false && /::/.test(str)?/^([\da-f]{1,4}(:|::)){1,6}[\da-f]{1,4}$/i.test(str):/^([\da-f]{1,4}:){7}[\da-f]{1,4}$/i.test(str));
}
function isIDCard(str){
if(!str) return false;
return /^[1-9][0-9]{5}(18|19|(2[0-9]))[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}[0-9Xx]$/.test(str);
}
function isPostCode(value){
if(!value) return false;
return /^[1-9][0-9]{5}$/.test(value.toString());
}
function same(firstValue,secondValue){
return firstValue===secondValue;
}
function lengthRange(str,minLength,maxLength=9007199254740991) {
if(!str) return false;
return Boolean(str.length >= minLength && str.length <= maxLength);
}
function letterBegin(str){
if(!str) return false;
return /^[A-z]/.test(str);
}
function startByLetter(str){
if(!str) return false;
return /^[A-z]/.test(str);
}
function startBy(str,keyword,escape=true){
if(!str && !keyword) return false;
if(escape){
keyword = keyword.replace(/[\ \~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\\\[\]\{\}\;\:\"\'\,\<\.\>\/\?]/g,'\\$&');
}
let regexp = new RegExp(`^${escape?`(${keyword})`:keyword}`);
return regexp.test(str);
}
function endBy(str,keyword,escape=true){
if(!str && !keyword) return false;
if(escape){
keyword = keyword.replace(/[\ \~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\\\[\]\{\}\;\:\"\'\,\<\.\>\/\?]/g,'\\$&');
}
let regexp = new RegExp(`${escape?`(${keyword})`:keyword}$`);
return regexp.test(str);
}
function pureNum(str) {
if(!str) return false;
return /^[0-9]*$/.test(str);
}
function anysicPunctuation(str){
if(!str) return null;
let arr = str.split('').map(item => {
return '\\' + item;
});
return arr.join('|');
}
function getPunctuation(str){
return anysicPunctuation(str) || '\\~|\\`|\\!|\\@|\\#|\\$|\\%|\\^|\\&|\\*|\\(|\\)|\\-|\\_|\\+|\\=|\\||\\\|\\[|\\]|\\{|\\}|\\;|\\:|\\"|\\\'|\\,|\\<|\\.|\\>|\\/|\\?';
}
function getExcludePunctuation(str){
let regexp = new RegExp(`[${anysicPunctuation(str)}]`,'g');
return getPunctuation(' ~`!@#$%^&*()-_+=\[]{};:"\',<.>/?'.replace(regexp,''));
}
function getLIPTypes(str,punctuation=null){
if(!str) return 0;
let p_regexp = new RegExp('['+getPunctuation(punctuation)+']');
return /[A-z]/.test(str) + /[0-9]/.test(str) + p_regexp.test(str);
}
function pureLIP(str,num=1,punctuation=null){
if(!str) return false;
let regexp = new RegExp(`[^A-z0-9|${getPunctuation(punctuation)}]`);
return Boolean(!regexp.test(str) && getLIPTypes(str,punctuation)>= num);
}
function clearSpaces(str){
if(!str) return '';
return str.replace(/[ ]/g,'');
}
function clearCNChars(str){
if(!str) return '';
return str.replace(/[\u4e00-\u9fa5]/g,'');
}
function clearCNCharsAndSpaces(str){
if(!str) return '';
return str.replace(/[\u4e00-\u9fa5 ]/g,'');
}
function clearPunctuation(str,excludePunctuation=null){
if(!str) return '';
let regexp = new RegExp(`[${getExcludePunctuation(excludePunctuation)}]`,'g');
return str.replace(regexp,'');
}
function haveSpace(str) {
if(!str) return false;
return /[ ]/.test(str);
}
function haveCNChars(str){
if(!str) return false;
return /[\u4e00-\u9fa5]/.test(str);
}
module.exports = {
isNum,isInt,isPInt,isNInt,checkIntRange,
isTel,isFax,isEmail,isQQ,isURL,isIP,isIPv6,isIDCard,isPostCode,
same,lengthRange,letterBegin,startByLetter,startBy,endBy,pureNum,getLIPTypes,pureLIP,
clearSpaces,clearCNChars,clearCNCharsAndSpaces,clearPunctuation,
haveSpace,haveCNChars
}