-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (67 loc) · 1.35 KB
/
index.js
File metadata and controls
79 lines (67 loc) · 1.35 KB
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
'use strict'
const types = {
undefined: 'undefined',
string: 'string',
number: 'number',
boolean: 'boolean',
array: 'array',
object: 'object',
function: 'function'
}
const validators = {
[types.array]: (value) => {
return typeof value === 'object' && Object.prototype.toString.call(value) === '[object Array]'
},
[types.object]: (value) => {
return typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]'
}
}
function isTypeOf (value, type) {
let func = validators[type]
if (func) {
return func(value)
} else {
return typeof value === type
}
}
function isUndefined (value) {
return isTypeOf(value, types.undefined)
}
function isString (value) {
return isTypeOf(value, types.string)
}
function isNumber (value) {
return isTypeOf(value, types.number)
}
function isBool (value) {
return isTypeOf(value, types.boolean)
}
function isArray (value) {
return isTypeOf(value, types.array)
}
function isObject (value) {
return isTypeOf(value, types.object)
}
function isFunction (value) {
return isTypeOf(value, types.function)
}
function isOneOf (value, values) {
for (let val of values) {
if (val === value) {
return true
}
}
return false
}
export {
types,
isTypeOf,
isUndefined,
isString,
isNumber,
isBool,
isArray,
isObject,
isFunction,
isOneOf
}