-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
307 lines (238 loc) · 8.07 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
var inherits = require('inherits')
function TfTypeError (type, value) {
this.tfError = Error.call(this)
this.tfType = type
this.tfValue = value
var message
Object.defineProperty(this, 'message', {
get: function () {
if (message) return message
message = tfErrorString(type, value)
return message
}
})
}
inherits(TfTypeError, Error)
Object.defineProperty(TfTypeError, 'stack', { get: function () { return this.tfError.stack } })
function TfPropertyTypeError (type, property, value, error) {
this.tfError = error || Error.call(this)
this.tfProperty = property
this.tfType = type
this.tfValue = value
var message
Object.defineProperty(this, 'message', {
get: function () {
if (message) return message
if (type) {
message = tfPropertyErrorString(type, property, value)
} else {
message = 'Unexpected property "' + property + '"'
}
return message
}
})
}
inherits(TfPropertyTypeError, Error)
Object.defineProperty(TfPropertyTypeError, 'stack', { get: function () { return this.tfError.stack } })
TfPropertyTypeError.prototype.asChildOf = function (property) {
return new TfPropertyTypeError(this.tfType, property + '.' + this.tfProperty, this.tfValue, this.tfError)
}
function getFunctionName (fn) {
return fn.name || fn.toString().match(/function (.*?)\s*\(/)[1]
}
function getValueTypeName (value) {
if (nativeTypes.Null(value)) return ''
return getFunctionName(value.constructor)
}
function getValue (value) {
if (nativeTypes.Function(value)) return ''
if (nativeTypes.String(value)) return JSON.stringify(value)
if (value && nativeTypes.Object(value)) return ''
return value
}
function tfJSON (type) {
if (nativeTypes.Function(type)) return type.toJSON ? type.toJSON() : getFunctionName(type)
if (nativeTypes.Array(type)) return 'Array'
if (type && nativeTypes.Object(type)) return 'Object'
return type || ''
}
function stfJSON (type) {
type = tfJSON(type)
return nativeTypes.Object(type) ? JSON.stringify(type) : type
}
function tfErrorString (type, value) {
var valueTypeName = getValueTypeName(value)
var valueValue = getValue(value)
return 'Expected ' + stfJSON(type) + ', got' + (valueTypeName !== '' ? ' ' + valueTypeName : '') + (valueValue !== '' ? ' ' + valueValue : '')
}
function tfPropertyErrorString (type, name, value) {
return tfErrorString('property \"' + stfJSON(name) + '\" of type ' + stfJSON(type), value)
}
var nativeTypes = {
Array: function (value) { return value !== null && value !== undefined && value.constructor === Array },
Boolean: function (value) { return typeof value === 'boolean' },
Buffer: function (value) { return Buffer.isBuffer(value) },
Function: function (value) { return typeof value === 'function' },
Null: function (value) { return value === undefined || value === null },
Number: function (value) { return typeof value === 'number' },
Object: function (value) { return typeof value === 'object' },
String: function (value) { return typeof value === 'string' },
'': function () { return true }
}
var otherTypes = {
arrayOf: function arrayOf (type) {
function arrayOf (value, strict) {
if (!nativeTypes.Array(value)) return false
return value.every(function (x) {
return typeforce(type, x, strict, arrayOf)
})
}
arrayOf.toJSON = function () { return [tfJSON(type)] }
return arrayOf
},
maybe: function maybe (type) {
function maybe (value, strict) {
return nativeTypes.Null(value) || typeforce(type, value, strict, maybe)
}
maybe.toJSON = function () { return '?' + stfJSON(type) }
return maybe
},
object: function object (type) {
function object (value, strict) {
if (!nativeTypes.Object(value)) return false
if (nativeTypes.Null(value)) return false
var propertyName
try {
for (propertyName in type) {
var propertyType = type[propertyName]
var propertyValue = value[propertyName]
typeforce(propertyType, propertyValue, strict)
}
} catch (e) {
if (e instanceof TfPropertyTypeError) {
throw e.asChildOf(propertyName)
} else if (e instanceof TfTypeError) {
throw new TfPropertyTypeError(e.tfType, propertyName, e.tfValue, e.tfError)
}
throw e
}
if (strict) {
for (propertyName in value) {
if (type[propertyName]) continue
throw new TfPropertyTypeError(undefined, propertyName)
}
}
return true
}
object.toJSON = function () { return tfJSON(type) }
return object
},
map: function map (propertyType, propertyKeyType) {
function map (value, strict) {
typeforce(nativeTypes.Object, value, strict)
if (nativeTypes.Null(value)) return false
var propertyName
try {
for (propertyName in value) {
if (propertyKeyType) {
typeforce(propertyKeyType, propertyName, strict)
}
var propertyValue = value[propertyName]
typeforce(propertyType, propertyValue, strict)
}
} catch (e) {
if (e instanceof TfPropertyTypeError) {
throw e.asChildOf(propertyName)
} else if (e instanceof TfTypeError) {
throw new TfPropertyTypeError(e.tfType, propertyKeyType || propertyName, e.tfValue)
}
throw e
}
return true
}
if (propertyKeyType) {
map.toJSON = function () { return '{' + stfJSON(propertyKeyType) + ': ' + stfJSON(propertyType) + '}' }
} else {
map.toJSON = function () { return '{' + stfJSON(propertyType) + '}' }
}
return map
},
oneOf: function oneOf () {
var types = [].slice.call(arguments)
function oneOf (value, strict) {
return types.some(function (type) {
try {
return typeforce(type, value, strict)
} catch (e) {
if (e instanceof TfTypeError || e instanceof TfPropertyTypeError) return false
throw e
}
})
}
oneOf.toJSON = function () { return types.map(stfJSON).join('|') }
return oneOf
},
quacksLike: function quacksLike (type) {
function quacksLike (value, strict) {
return type === getValueTypeName(value)
}
quacksLike.toJSON = function () { return type }
return quacksLike
},
tuple: function tuple () {
var types = [].slice.call(arguments)
function tuple (value, strict) {
return types.every(function (type, i) {
return typeforce(type, value[i], strict)
})
}
tuple.toJSON = function () { return '(' + types.map(stfJSON).join(', ') + ')' }
return tuple
},
value: function value (expected) {
function value (actual) {
return actual === expected
}
value.toJSON = function () { return expected }
return value
}
}
function compile (type) {
if (nativeTypes.String(type)) {
if (type[0] === '?') return otherTypes.maybe(compile(type.slice(1)))
return nativeTypes[type] || otherTypes.quacksLike(type)
} else if (type && nativeTypes.Object(type)) {
if (nativeTypes.Array(type)) return otherTypes.arrayOf(compile(type[0]))
var compiled = {}
for (var propertyName in type) {
compiled[propertyName] = compile(type[propertyName])
}
return otherTypes.object(compiled)
} else if (nativeTypes.Function(type)) {
return type
}
return otherTypes.value(type)
}
function typeforce (type, value, strict, surrogate) {
if (nativeTypes.Function(type)) {
if (type(value, strict)) return true
throw new TfTypeError(surrogate || type, value)
}
// JIT
return typeforce(compile(type), value, strict)
}
// assign all types to typeforce function
var typeName
Object.keys(nativeTypes).forEach(function (typeName) {
var nativeType = nativeTypes[typeName]
nativeType.toJSON = function () { return typeName }
typeforce[typeName] = nativeType
})
for (typeName in otherTypes) {
typeforce[typeName] = otherTypes[typeName]
}
module.exports = typeforce
module.exports.compile = compile
// export Error objects
module.exports.TfTypeError = TfTypeError
module.exports.TfPropertyTypeError = TfPropertyTypeError