1+ 'use strict'
2+
3+ const types = {
4+ undefined : 'undefined' ,
5+ string : 'string' ,
6+ number : 'number' ,
7+ boolean : 'boolean' ,
8+ array : 'array' ,
9+ object : 'object' ,
10+ function : 'function'
11+ }
12+
13+ const validators = {
14+ [ types . array ] : ( value ) => {
15+ return typeof value === 'object' && Object . prototype . toString . call ( value ) === '[object Array]'
16+ } ,
17+ [ types . object ] : ( value ) => {
18+ return typeof value === 'object' && Object . prototype . toString . call ( value ) !== '[object Array]'
19+ }
20+ }
21+
22+ function isTypeOf ( value , type ) {
23+ let func = validators [ type ]
24+ if ( func ) {
25+ return func ( value )
26+ } else {
27+ return typeof value === type
28+ }
29+ }
30+
31+ function isUndefined ( value ) {
32+ return isTypeOf ( value , types . undefined )
33+ }
34+
35+ function isString ( value ) {
36+ return isTypeOf ( value , types . string )
37+ }
38+
39+ function isNumber ( value ) {
40+ return isTypeOf ( value , types . number )
41+ }
42+
43+ function isBool ( value ) {
44+ return isTypeOf ( value , types . boolean )
45+ }
46+
47+ function isArray ( value ) {
48+ return isTypeOf ( value , types . array )
49+ }
50+
51+ function isObject ( value ) {
52+ return isTypeOf ( value , types . object )
53+ }
54+
55+ function isFunction ( value ) {
56+ return isTypeOf ( value , types . function )
57+ }
58+
59+ function isOneOf ( value , values ) {
60+ for ( let val of values ) {
61+ if ( val === value ) {
62+ return true
63+ }
64+ }
65+ return false
66+ }
67+
68+ export {
69+ types ,
70+ isTypeOf ,
71+ isUndefined ,
72+ isString ,
73+ isNumber ,
74+ isBool ,
75+ isArray ,
76+ isObject ,
77+ isFunction ,
78+ isOneOf
79+ }
0 commit comments