-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_checks.js
63 lines (54 loc) · 1.75 KB
/
type_checks.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
"use strict";
/**
Created by Complynx on 22.03.2019,
http://complynx.net
<[email protected]> Daniel Drizhuk
*/
let toStr = ((ts)=>(o)=>ts.call(o))(Object.prototype.toString);
export function isString(a) {
return (typeof a === "string") || (a instanceof String);
}
export function isObject(obj) {
return toStr(obj) === '[object Object]';
}
export function isFunction(obj) {
return typeof(obj) === 'function' || (obj instanceof Function);
}
export function arrayLike(item){
if(typeof(item)=== "string") return false; //string is mostly like array, but it is still a primitive
return (item != null && isInteger(item.length) && item.length >= 0
&& toStr(item) !== '[object Function]'); //Can't remember, why/where the last test is necessary
}
let GeneratorClass = (function*(){}).constructor;
export function isGenerator(item) {
return item instanceof GeneratorClass;
}
let AsyncFunctionClass = (async function(){}).constructor;
export function isAsyncFunction(item) {
return item instanceof AsyncFunctionClass;
}
export function isNumber(obj){
if(isString(obj) || typeof obj === 'number'){
if(isString(obj) && (obj === '' || obj.trim() === '')) return false;
try{
return !isNaN(obj) && obj !== null;
}catch(e){
return false; // case it's Symbol
}
}
return false;
}
export function isInteger(x) {
return !isNaN(x) && !((x=parseFloat(x))%1) && x<=Number.MAX_SAFE_INTEGER && x>=Number.MIN_SAFE_INTEGER;
}
export function isArrayBufferView(obj) {
return obj?.buffer instanceof ArrayBuffer
}
export function isBlobPart(obj) {
return isString(obj) ||
(
obj instanceof ArrayBuffer ||
obj instanceof Blob ||
isArrayBufferView(obj)
);
}