-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
122 lines (116 loc) · 3.69 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
// Require Internal Dependencies
const { getObjectType, isTypeOf, isObjectOfType } = require("./src/utils");
/**
* @constant Primitives
* @description All JavaScript Primitives
* @type {Set<string>}
*/
const Primitives = new Set(["string", "number", "boolean", "undefined", "symbol", "bigint"]);
/**
* @constant TypedArrayTypes
* @description All JavaScript Typed Array Types
* @type {Set<string>}
*/
const TypedArrayTypes = new Set([
"Int8Array",
"Uint8Array",
"Uint8ClampedArray",
"Int16Array",
"Uint16Array",
"Int32Array",
"Uint32Array",
"Float32Array",
"Float64Array"
]);
/**
* @function nullOrUndefined
* @param {number} value
* @returns {number}
*/
function nullOrUndefined(value) {
return value === null || typeof value === "undefined";
}
// Export all methods
module.exports = {
undefined: isTypeOf("undefined"),
void: isTypeOf("undefined"),
string: isTypeOf("string"),
number(value) {
return !Number.isNaN(value) && isTypeOf("number")(value);
},
boolean: isTypeOf("boolean"),
bool: isTypeOf("boolean"),
symbol: isTypeOf("symbol"),
bigint: isTypeOf("bigint"),
func: isTypeOf("function"),
nullValue: (value) => value === null,
nullOrUndefined,
array: Array.isArray,
buffer: Buffer.isBuffer,
primitive(value) {
return value === null || Primitives.has(typeof value);
},
promise: isObjectOfType("Promise"),
generatorFunction: isObjectOfType("GeneratorFunction"),
asyncFunction: isObjectOfType("AsyncFunction"),
boundFunction(value) {
// eslint-disable-next-line no-prototype-builtins
return isTypeOf("function")(value) && !value.hasOwnProperty("prototype");
},
regExp: isObjectOfType("RegExp"),
date: isObjectOfType("Date"),
error: isObjectOfType("Error"),
map: isObjectOfType("Map"),
set: isObjectOfType("Set"),
weakMap: isObjectOfType("WeakMap"),
weakSet: isObjectOfType("WeakSet"),
int8Array: isObjectOfType("Int8Array"),
uint8Array: isObjectOfType("Uint8Array"),
uint8ClampedArray: isObjectOfType("uint8ClampedArray"),
int16Array: isObjectOfType("int16Array"),
uint16Array: isObjectOfType("uint16Array"),
int32Array: isObjectOfType("int32Array"),
uint32Array: isObjectOfType("uint32Array"),
float32Array: isObjectOfType("float32Array"),
float64Array: isObjectOfType("float64Array"),
arrayBuffer: isObjectOfType("ArrayBuffer"),
sharedArrayBuffer: isObjectOfType("SharedArrayBuffer"),
dataView: isObjectOfType("DataView"),
nan: (value) => Number.isNaN(value),
integer: (value) => Number.isInteger(value),
truthy: (value) => Boolean(value),
falsy: (value) => !value,
emptyString(value) {
return typeof value === "string" && value === "";
},
plainObject(value) {
if (getObjectType(value) !== "Object") {
return false;
}
const prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.getPrototypeOf({});
},
typedArray(value) {
return TypedArrayTypes.has(getObjectType(value));
},
directInstanceOf(instance, focusClass) {
return Object.getPrototypeOf(instance) === focusClass.prototype;
},
classObject(value) {
return isTypeOf("function")(value) && value.toString().startsWith("class ");
},
object(value) {
return !nullOrUndefined(value) && (isTypeOf("function")(value) || typeof value === "object");
},
iterable(value) {
return !nullOrUndefined(value) && isTypeOf("function")(value[Symbol.iterator]);
},
asyncIterable(value) {
return !nullOrUndefined(value) && isTypeOf("function")(value[Symbol.asyncIterator]);
},
generator(value) {
const isFn = isTypeOf("function");
return !nullOrUndefined(value) && isFn(value[Symbol.iterator]) && isFn(value.next) && isFn(value.throw);
},
utils: { getObjectType }
};