-
Notifications
You must be signed in to change notification settings - Fork 240
/
prop-types.js
109 lines (96 loc) · 2.92 KB
/
prop-types.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
/**
* @copyright Maichong Software Ltd. 2016 http://maichong.it
* @date 2016-10-11
* @author Liang <[email protected]>
*/
/* eslint no-inner-declarations:0 no-inner-declarations:0 */
// @flow
'use strict';
const numberTag = '[object Number]';
const boolTag = '[object Boolean]';
const stringTag = '[object String]';
const symbolTag = '[object Symbol]';
function isObjectLike(value: any): boolean {
return value != null && typeof value === 'object';
}
function objectToString(value: Object): string {
return Object.prototype.toString.call(value);
}
function getType(value: any): string {
if (Array.isArray(value)) {
return 'array';
}
const type = typeof value;
if (type === 'function') {
return 'func';
}
if (type === 'number' || (isObjectLike(value) && objectToString(value) === numberTag)) {
return 'number';
}
if (
value === true || value === false
|| (isObjectLike(value) && objectToString(value) === boolTag)
) {
return 'bool';
}
if (type === 'string' || (isObjectLike(value) && objectToString(value) === stringTag)) {
return 'string';
}
if (type === 'object' && value !== null) {
return 'object';
}
if (type === 'symbol' || (isObjectLike(value) && objectToString(value) === symbolTag)) {
return 'symbol';
}
return 'unknown';
}
function generate(name: string, allowNull?: boolean) {
const validator = function (props: any, propName: string, componentName: string): ?Error {
const value = props[propName];
if (value === undefined || (allowNull && value === null)) return null;
const type = getType(value);
return type === name ? null : new Error('组件"' + componentName + '"的属性"' + propName + '"类型声明为"' + name + '",却得到"' + type + '"');
};
validator.isRequired = function (props: any, propName: string, componentName: string): ?Error {
const value = props[propName];
if (value === undefined || value === null) {
return new Error('组件"' + componentName + '"的必要属性"' + propName + '"缺失,得到"' + value + '"');
}
return validator(props, propName, componentName);
};
return validator;
}
const any = function () {
};
if (__DEV__) {
any.isRequired = function (props: any, propName: string, componentName: string): ?Error {
const value = props[propName];
if (value === undefined) {
return new Error('组件"' + componentName + '"的必要属性"' + propName + '"缺失,得到"' + value + '"');
}
return null;
};
module.exports = {
number: generate('number'),
string: generate('string'),
func: generate('func', true),
array: generate('array'),
bool: generate('bool'),
object: generate('object', true),
symbol: generate('symbol'),
any: any
};
} else {
any.isRequired = function () {
};
module.exports = {
number: any,
string: any,
func: any,
array: any,
bool: any,
object: any,
symbol: any,
any: any
};
}