forked from fazlulkarimweb/string-sanitizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (108 loc) · 3.37 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
'use strict';
exports.sanitize = function (str) {
return str.replace(/[^a-zA-Z0-9]/g, '');
};
exports.sanitize.keepUnicode = function (str) {
return str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
};
exports.sanitize.keepSpace = function (str) {
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
return str2.replace(/ /g, ' ');
};
exports.sanitize.addFullstop = function (str) {
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
return str2.replace(/ /g, '.');
};
exports.sanitize.addUnderscore = function (str) {
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
return str2.replace(/ /g, '_');
};
exports.sanitize.addDash = function (str) {
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
return str2.replace(/ /g, '-');
};
exports.sanitize.removeNumber = function (str) {
return str.replace(/[^a-zA-Z]/g, '');
};
exports.sanitize.removeText = function (str) {
return str.replace(/[^0-9]/g, '');
};
exports.sanitize.keepNumber = function (str) {
return str.replace(/[^a-zA-Z0-9]/g, '');
};
exports.addFullstop = function (str) {
return str.replace(/ /g, '.');
};
exports.addUnderscore = function (str) {
return str.replace(/ /g, '_');
};
exports.addDash = function (str) {
return str.replace(/ /g, '-');
};
// Remove Space without sanitizing
exports.removeSpace = function (str) {
return str.replace(/\s+/g, '');
};
exports.removeUnderscore = function (str) {
return str.replace(/_+/g, '');
};
exports.validate = function (str) {
console.log(
`Use validate.isEmail or validate.isUsername for further validation`
);
return 'Use validate.isEmail or validate.isUsername for further validation';
};
//Username & Email
exports.validate.isEmail = function (str) {
const regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (regex.test(str)) {
return str;
} else {
return false;
}
};
exports.validate.isUsername = function (str) {
const regex = /^[a-z][a-z]+\d*$|^[a-z]\d{2,}$/i;
if (regex.test(str)) {
return str.toLowerCase();
} else {
return false;
}
};
// To check a password between 6 to 15 characters which contain at least one numeric digit and a special character
exports.validate.isPassword6to15 = function (str) {
const regex = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,15}$/;
if (regex.test(str)) {
return str;
} else {
return false;
}
};
// 7 to 20 characters which contain only characters, numeric digits, underscore and first character must be a letter
exports.validate.isPassword7to20 = function (str) {
const regex = /^[A-Za-z]\w{7,20}$/;
if (regex.test(str)) {
return str;
} else {
return false;
}
};
// 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter
exports.validate.isPassword6to20 = function (str) {
const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
if (regex.test(str)) {
return str;
} else {
return false;
}
};
// To check a password between 8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character
exports.validate.isPassword8to15 = function (str) {
const regex =
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
if (regex.test(str)) {
return str;
} else {
return false;
}
};