37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
import * as typechecker from 'typechecker'
|
|
|
|
// TODO add observable types and other custom uci types
|
|
|
|
const customTypeChecker = Object.assign({}, typechecker) // typechecker is frozen
|
|
|
|
// add custom types
|
|
customTypeChecker.isPromise = function isPromise(obj) {
|
|
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
|
|
}
|
|
|
|
customTypeChecker.isBuffer = function isBuffer(obj) {
|
|
return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
|
|
}
|
|
|
|
customTypeChecker.isSymbol = function isSymbol(x) {
|
|
return typeof x === 'symbol'
|
|
|| typeof x === 'object' && Object.prototype.toString.call(x) === '[object Symbol]';
|
|
}
|
|
|
|
// Add custom types to typeMap
|
|
let customTypeMap = {
|
|
buffer: customTypeChecker.isBuffer,
|
|
promise: customTypeChecker.isPromise
|
|
}
|
|
|
|
customTypeMap = Object.assign(customTypeMap, customTypeChecker.typeMap)
|
|
|
|
customTypeChecker.getType = function customGetType(value, _typeMap) {
|
|
return typechecker.getType(value, _typeMap || customTypeMap)
|
|
}
|
|
|
|
const typeOf = customTypeChecker.getType
|
|
|
|
export default customTypeChecker
|
|
export { customTypeChecker as check, typeOf }
|