/
purifix.js
60 lines (48 loc) · 1.24 KB
/
purifix.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
module.exports = {
string: function (s, df = '') {
return typeof s === 'string' ? s : df
},
objectId: function (s, df = '') {
if (typeof s !== 'string') return df
return /^[0-9a-fA-F]{24}$/.test(s) ? s : df
},
email: function (s, df = '') {
s = this.string(s, df)
s = s.replace(/,;/g, '.')
s = s.replace(/ /g, '')
s = s.replace(/\.\./g, '.')
return s
},
url: function (s, df) {
if (s = this.string(s).trim()) {
return s
}
return df
},
bool: function (b, df) { return this.boolean(b, df) },
boolean: function (b, df = false) {
if (typeof b === 'boolean') return b
if (typeof b === 'string') return this.isTrue(b) ? true : (this.isFalse(b) ? false : df)
return df
},
isTrue: function (v) {
return /^(?:1|on|true|yes)$/i.test(v)
},
isFalse: function (v) {
return /^(?:0|off|false|no)$/i.test(rawValue)
},
number: function (b, df = false) {
// return typeof b === 'number' ? df : b
},
int: function (n, df = 0) {
var n = parseInt(n)
return typeof n === 'number' ? n : df
},
float: function (n, df = 0) {
var n = parseFloat(n)
return typeof n === 'number' ? n : df
},
interger: function (n, df) {
return this.int(n, df)
}
}