-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (47 loc) · 1.11 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
'use strict';
let regex = /^\[object\ (\w+)\]$/;
let is = function (type, object) {
if (arguments.length === 1) {
let match = Object.prototype.toString.call(type).match(regex);
if (match && match.length === 2) {
return match[1].toLowerCase();
} else {
throw new Error('Invalid object');
}
} else if (arguments.length === 2 && typeof type === 'string') {
type = type.toLowerCase();
let typeOf = is(object);
return typeOf ? type === typeOf : false;
} else if (arguments.length === 2) {
throw new Error('Invalid type');
} else {
throw new Error('At least 1 argument is required');
}
};
module.exports = {
is: is,
isNumber: object => {
return is('number', object);
},
isObject: object => {
return is('object', object);
},
isBoolean: object => {
return is('boolean', object);
},
isNull: object => {
return is('null', object);
},
isBuffer: object => {
return is('uint8array', object);
},
isUndefined: object => {
return is('undefined', object);
},
isArray: Array.isArray || function (object) {
return is('array', object);
},
isString: object => {
return is('string', object);
},
};