1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = hasProp;
|
---|
7 | exports.hasAnyProp = hasAnyProp;
|
---|
8 | exports.hasEveryProp = hasEveryProp;
|
---|
9 |
|
---|
10 | var _propName = require('./propName');
|
---|
11 |
|
---|
12 | var _propName2 = _interopRequireDefault(_propName);
|
---|
13 |
|
---|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
15 |
|
---|
16 | var DEFAULT_OPTIONS = {
|
---|
17 | spreadStrict: true,
|
---|
18 | ignoreCase: true
|
---|
19 | };
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Returns boolean indicating whether an prop exists on the props
|
---|
23 | * property of a JSX element node.
|
---|
24 | */
|
---|
25 | function hasProp() {
|
---|
26 | var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
---|
27 | var prop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
---|
28 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_OPTIONS;
|
---|
29 |
|
---|
30 | var propToCheck = options.ignoreCase ? prop.toUpperCase() : prop;
|
---|
31 |
|
---|
32 | return props.some(function (attribute) {
|
---|
33 | // If the props contain a spread prop, then refer to strict param.
|
---|
34 | if (attribute.type === 'JSXSpreadAttribute') {
|
---|
35 | return !options.spreadStrict;
|
---|
36 | }
|
---|
37 |
|
---|
38 | var currentProp = options.ignoreCase ? (0, _propName2.default)(attribute).toUpperCase() : (0, _propName2.default)(attribute);
|
---|
39 |
|
---|
40 | return propToCheck === currentProp;
|
---|
41 | });
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Given the props on a node and a list of props to check, this returns a boolean
|
---|
46 | * indicating if any of them exist on the node.
|
---|
47 | */
|
---|
48 | function hasAnyProp() {
|
---|
49 | var nodeProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
---|
50 | var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
---|
51 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_OPTIONS;
|
---|
52 |
|
---|
53 | var propsToCheck = typeof props === 'string' ? props.split(' ') : props;
|
---|
54 |
|
---|
55 | return propsToCheck.some(function (prop) {
|
---|
56 | return hasProp(nodeProps, prop, options);
|
---|
57 | });
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Given the props on a node and a list of props to check, this returns a boolean
|
---|
62 | * indicating if all of them exist on the node
|
---|
63 | */
|
---|
64 | function hasEveryProp() {
|
---|
65 | var nodeProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
---|
66 | var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
---|
67 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_OPTIONS;
|
---|
68 |
|
---|
69 | var propsToCheck = typeof props === 'string' ? props.split(' ') : props;
|
---|
70 |
|
---|
71 | return propsToCheck.every(function (prop) {
|
---|
72 | return hasProp(nodeProps, prop, options);
|
---|
73 | });
|
---|
74 | } |
---|