-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (107 loc) · 3.77 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
123
124
125
126
127
128
129
130
import RealPropTypes from 'prop-types'
import { setPropTypes as setRealPropTypes, getContext as getRealContext, withContext as withRealContext } from 'recompose'
// export depending on env
let setPropTypes
let PropTypes
let getContext
let withContext
const loop = (object, callback) => {
for (let i in object) {
object.hasOwnProperty(i) && callback(i, object[i])
}
}
const mapArgs = (args) => {
if (typeof args !== 'object') {
return args
}
let mappedArgs = args.length !== undefined ? [] : {}
loop(args, (key, arg) => {
if (arg && arg.ptc) {
if (RealPropTypes[arg.type]) {
let realArg
if (arg.args) {
realArg = RealPropTypes[arg.type](...mapArgs(arg.args))
} else {
realArg = RealPropTypes[arg.type]
}
if (arg.required) {
realArg = realArg.isRequired
}
mappedArgs[key] = realArg
} else {
throw new Error(`Unknown propType ${arg.type}`)
}
} else {
mappedArgs[key] = typeof arg !== 'object' ? arg : mapArgs(arg)
}
})
return mappedArgs
}
if (process.env.NODE_ENV === 'production' && process.env.PROPER_TYPES !== 'show') {
setPropTypes = setRealPropTypes
PropTypes = RealPropTypes
getContext = getRealContext
withContext = withRealContext
} else {
setPropTypes = (properProps) => (Component) => {
let propTypes = {}
let properTypes = {}
loop(properProps, (key, prop) => {
let propType = {}
if (!prop || !prop.ptc) {
propTypes[key] = prop
properTypes[key] = prop === null ? 'null' : typeof prop
} else if (RealPropTypes[prop.type]) {
if (prop.args) {
propType = RealPropTypes[prop.type](...mapArgs(prop.args))
} else {
propType = RealPropTypes[prop.type]
}
if (prop.required) {
propType = propType.isRequired
}
propTypes[key] = propType
properTypes[key] = { ...prop }
} else {
throw new Error(`Unknown propType ${prop.type}`)
}
})
Component.propTypes = { ...Component.propTypes, ...propTypes }
Component.properTypes = { ...Component.properTypes, ...properTypes }
return Component
}
const buildType = (type) => {
let reply = { type, ptc: true }
return { ...reply, isRequired: { ...reply, required: true } }
}
const buildArgType = (type) => {
return (...args) => {
let reply = { type, args, ptc: true }
return { ...reply, isRequired: { ...reply, required: true } }
}
}
getContext = (...context) => getRealContext(...mapArgs(context))
withContext = (...context) => withRealContext(...mapArgs(context))
// propTypes <-> properTypes mapping
PropTypes = {
// simple types
any: buildType('any'),
array: buildType('array'),
bool: buildType('bool'),
func: buildType('func'),
number: buildType('number'),
object: buildType('object'),
string: buildType('string'),
symbol: buildType('symbol'),
node: buildType('node'),
element: buildType('element'),
// types that take args
instanceOf: buildArgType('instanceOf'),
oneOf: buildArgType('oneOf'),
oneOfType: buildArgType('oneOfType'),
arrayOf: buildArgType('arrayOf'),
objectOf: buildArgType('objectOf'),
shape: buildArgType('shape'),
}
}
export { setPropTypes, PropTypes, getContext, withContext }