types: enforce consistent type checking
This commit is contained in:
parent
166053a3e5
commit
35542e115d
10 changed files with 99 additions and 44 deletions
src
38
src/types.js
Normal file
38
src/types.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
module.exports = function enforce(type, value) {
|
||||
switch (type) {
|
||||
// http://jsperf.com/array-typecheck-2
|
||||
case 'Array': {
|
||||
if (value != null && value.constructor === Array) return
|
||||
break
|
||||
}
|
||||
|
||||
// http://jsperf.com/boolean-typecheck
|
||||
case 'Boolean': {
|
||||
if (typeof value === 'boolean') return
|
||||
break
|
||||
}
|
||||
|
||||
case 'Buffer': {
|
||||
if (Buffer.isBuffer(value)) return
|
||||
break
|
||||
}
|
||||
|
||||
// http://jsperf.com/number-constructor-v-isnan
|
||||
case 'Number': {
|
||||
if (typeof value === 'number') return
|
||||
break
|
||||
}
|
||||
|
||||
// http://jsperf.com/string-typecheck-2
|
||||
case 'String': {
|
||||
if (value != null && value.constructor === String) return
|
||||
break
|
||||
}
|
||||
|
||||
default: {
|
||||
if (value instanceof type) return
|
||||
}
|
||||
}
|
||||
|
||||
throw new TypeError('Expected ' + (type.name || type) + ', got ' + value)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue