UNPKG

1.08 kBJavaScriptView Raw
1/**
2 * Checks if only one of the listed properties is in use. An error is given
3 * if multiple have a value
4 *
5 * @param props
6 * @param propName
7 * @param componentName
8 * @returns {Error|undefined}
9 */
10'use strict';
11
12exports.__esModule = true;
13exports['default'] = createSinglePropFromChecker;
14
15function createSinglePropFromChecker() {
16 for (var _len = arguments.length, arrOfProps = Array(_len), _key = 0; _key < _len; _key++) {
17 arrOfProps[_key] = arguments[_key];
18 }
19
20 function validate(props, propName, componentName) {
21 var usedPropCount = arrOfProps.map(function (listedProp) {
22 return props[listedProp];
23 }).reduce(function (acc, curr) {
24 return acc + (curr !== undefined ? 1 : 0);
25 }, 0);
26
27 if (usedPropCount > 1) {
28 var first = arrOfProps[0];
29 var others = arrOfProps.slice(1);
30
31 var message = others.join(', ') + ' and ' + first;
32 return new Error('Invalid prop \'' + propName + '\', only one of the following ' + ('may be provided: ' + message));
33 }
34 }
35 return validate;
36}
37
38module.exports = exports['default'];
\No newline at end of file