A smart typeof operator replacement and robust checker
npm install @everget/typeofpnpm add @everget/typeof`
$3
`js
const typeOf = require('@everget/typeof');
// or
import typeOf from '@everget/typeof';
`
`js
let value = 'awesome string';
typeOf(value)();
// => 'string'
typeOf(value) == 'string';
// => true
typeOf(value).expect('string');
// => true
typeOf(value).expect('number');
// => TypeError: The value is of type string, but expected number
`
`js
/* NOTE: you must use == operator for correct work /
let isMap = (value) => typeOf(value)() == 'map';
let smth = new Map();
if (isMap(smth)) {
/* Do your staff /
}
`
`js
let isFunction = (value) => {
return typeOf(value).expect('function|asyncfunction|generatorfunction');
}
let smth = async () => {};
if (isFunction(smth)) {
/* Do your staff /
}
`
`js
function sum(a, b) {
typeOf(a).expect('number');
typeOf(b).expect('number');
return typeOf(a + b).expect('number') && (a + b);
}
sum(1, () => {});
// => TypeError: The value is of type function, but expected number
sum(1, 2);
// => 3
`
$3
#### ES
Value | Type
----------------------------------- | ----
{} | 'object'
Math | 'math'
JSON | 'object'
function() {} | 'function'
[] | 'array'
null | 'null'
(function() { return arguments })() | 'arguments'
new Error | 'error'
undefined | 'underfined'
#### ES6, ES7
Value | Type
----------------------------- | ----
Reflect | 'object'
class {} | 'function'
Proxy | 'function'
new Proxy({}, {}) | 'object'
() => {} | 'function'
function* () {} | 'generatorfunction'
async function() {} | 'asyncfunction'
Symbol | 'function'
Symbol() | 'symbol'
new Map | 'map'
new WeakMap | 'weakmap'
new Set | 'set'
new WeakSet | 'weakset'
[1, 2, 3].entries() | 'arrayiterator'
new Set().entries() | 'setiterator'
new Map().entries() | 'mapiterator'
''[Symbol.iterator]() | 'stringiterator'
new ArrayBuffer() | 'arraybuffer'
new DataView(new ArrayBuffer) | 'dataview'
#### Browser
Value | Type
---------------------------------- | ----
window | 'global'
document | 'htmldocument'
localStorage | 'storage'
new DOMException | 'domexception'
document.createDocumentFragment() | 'documentfragment'
document.createElement('a') | 'htmlanchorelement'
document.createElement('body') | 'htmlbodyelement'
document.createElement('template') | 'htmltemplateelement'
document.createTextNode('') | 'text'
document.createComment('') | 'comment'
#### Node.js
Value | Type
------- | ----
global | 'global'
process | 'process'
$3
`npm test``