-
Notifications
You must be signed in to change notification settings - Fork 8
/
Types.js
88 lines (79 loc) · 1.73 KB
/
Types.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
const validator = require('validator')
const _String = String
const _Date = Date
const _Buffer = Buffer
exports.String = function String (actual, key, parent) {
/* istanbul ignore else */
if (key != null) {
parent[key] = _String(actual)
}
return true
}
exports.Number = function Number (actual, key, parent) {
if (isNaN(+actual)) {
return false
}
/* istanbul ignore else */
if (key != null) {
parent[key] = +actual
}
return true
}
exports.Integer = function Integer (actual, key, parent) {
if (isNaN(parseInt(actual))) {
return false
}
/* istanbul ignore else */
if (key != null) {
parent[key] = parseInt(actual)
}
return true
}
exports.Double = function Double (actual, key, parent) {
if (isNaN(parseFloat(actual))) {
return false
}
/* istanbul ignore else */
if (key != null) {
parent[key] = parseFloat(actual)
}
return true
}
exports.Date = function Date (actual, key, parent) {
const date = new _Date(actual)
if (date.toString() === 'Invalid Date') {
return false
}
/* istanbul ignore else */
if (key != null) {
parent[key] = date
}
return true
}
exports.Buffer = function Buffer (actual, key, parent) {
const isBuffer = _Buffer.isBuffer(actual)
if (isBuffer) {
return true
}
// try convert to buffer
// string, Buffer, ArrayBuffer, Array, or array-like object
try {
/* istanbul ignore else */
if (key != null) {
parent[key] = _Buffer.from(actual)
}
return true
} catch (e) {
return false
}
}
exports.Boolean = function Boolean (actual, key, parent) {
/* istanbul ignore else */
if (key != null) {
parent[key] = !!actual
}
return true
}
exports.Mixed = function (actual, key, parent) {
return true
}