-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
73 lines (70 loc) · 2.78 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
/**
* Copyright Facebook, ratehub.
* All rights reserved.
*
* This code is intended to closely match the behaviour of checkPropTypes() from
* facebook/prop-types. The license for that code can be found here:
* https://github.com/facebook/prop-types/blob/be165febc8133dfbe2c45133db6d25664dd68ad8/LICENSE
*
* That function's source:
* https://github.com/facebook/prop-types/blob/be165febc8133dfbe2c45133db6d25664dd68ad8/checkPropTypes.js
*/
/**
* Check if the values match with the type specs
* Return a type error message or null
*
* @param {object} typeSpecs Map of name to a ReactPropType
* @param {object} values Runtime values that need to be type-checked
* @param {string} location e.g. "prop", "context", "child context"
* @param {string} componentName Name of the component for error messages.
* @param {?Function} getStack Returns the component stack.
*/
function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
if (process.env.NODE_ENV !== 'production') {
const ReactPropTypesSecret = require('prop-types/lib/ReactPropTypesSecret');
var name = componentName || 'React class';
for (var typeSpecName in typeSpecs) {
if (typeSpecs.hasOwnProperty(typeSpecName)) {
var error;
if (typeof typeSpecs[typeSpecName] !== 'function') {
return (name + ': ' + location + ' type `' + typeSpecName + '` is ' +
'invalid; it must be a function, usually from React.PropTypes.');
} else {
// Prop type validation may throw. In case they do, catch and save the
// exception as the error.
try {
error = typeSpecs[typeSpecName](values, typeSpecName, componentName,
location, null, ReactPropTypesSecret);
} catch (ex) {
error = ex;
}
}
if (error && !(error instanceof Error)) {
return (name + ': type specification of ' + location + ' `' +
typeSpecName + '` is invalid; the type checker function must ' +
'return `null` or an `Error` but returned a ' + typeof error + '. '+
'You may have forgotten to pass an argument to the type checker ' +
'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' +
'shape all require an argument).');
}
if (error instanceof Error) {
var stack = getStack && getStack() || '';
return 'Failed ' + location + ' type: ' + error.message + stack;
}
}
}
}
}
/**
* Same as checkPropTypes but throws on error
*/
function assertPropTypes() {
if (process.env.NODE_ENV !== 'production') {
var error = checkPropTypes.apply(null, arguments);
if (error) {
throw new Error(error);
}
}
}
module.exports = checkPropTypes;
module.exports.assertPropTypes = assertPropTypes;