-
Notifications
You must be signed in to change notification settings - Fork 23
/
extra.js
91 lines (78 loc) · 2.16 KB
/
extra.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
var NATIVE = require('./native')
var ERRORS = require('./errors')
function _Buffer (value) {
return Buffer.isBuffer(value)
}
function Hex (value) {
return typeof value === 'string' && /^([0-9a-f]{2})+$/i.test(value)
}
function _LengthN (type, length) {
var name = type.toJSON()
function Length (value) {
if (!type(value)) return false
if (value.length === length) return true
throw ERRORS.tfCustomError(name + '(Length: ' + length + ')', name + '(Length: ' + value.length + ')')
}
Length.toJSON = function () { return name }
return Length
}
var _ArrayN = _LengthN.bind(null, NATIVE.Array)
var _BufferN = _LengthN.bind(null, _Buffer)
var _HexN = _LengthN.bind(null, Hex)
var _StringN = _LengthN.bind(null, NATIVE.String)
function Range (a, b, f) {
f = f || NATIVE.Number
function _range (value, strict) {
return f(value, strict) && (value > a) && (value < b)
}
_range.toJSON = function () {
return `${f.toJSON()} between [${a}, ${b}]`
}
return _range
}
var INT53_MAX = Math.pow(2, 53) - 1
function Finite (value) {
return typeof value === 'number' && isFinite(value)
}
function Int8 (value) { return ((value << 24) >> 24) === value }
function Int16 (value) { return ((value << 16) >> 16) === value }
function Int32 (value) { return (value | 0) === value }
function Int53 (value) {
return typeof value === 'number' &&
value >= -INT53_MAX &&
value <= INT53_MAX &&
Math.floor(value) === value
}
function UInt8 (value) { return (value & 0xff) === value }
function UInt16 (value) { return (value & 0xffff) === value }
function UInt32 (value) { return (value >>> 0) === value }
function UInt53 (value) {
return typeof value === 'number' &&
value >= 0 &&
value <= INT53_MAX &&
Math.floor(value) === value
}
var types = {
ArrayN: _ArrayN,
Buffer: _Buffer,
BufferN: _BufferN,
Finite: Finite,
Hex: Hex,
HexN: _HexN,
Int8: Int8,
Int16: Int16,
Int32: Int32,
Int53: Int53,
Range: Range,
StringN: _StringN,
UInt8: UInt8,
UInt16: UInt16,
UInt32: UInt32,
UInt53: UInt53
}
for (var typeName in types) {
types[typeName].toJSON = function (t) {
return t
}.bind(null, typeName)
}
module.exports = types