[d565449] | 1 | import propName from './propName';
|
---|
| 2 |
|
---|
| 3 | const DEFAULT_OPTIONS = {
|
---|
| 4 | spreadStrict: true,
|
---|
| 5 | ignoreCase: true,
|
---|
| 6 | };
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * Returns boolean indicating whether an prop exists on the props
|
---|
| 10 | * property of a JSX element node.
|
---|
| 11 | */
|
---|
| 12 | export default function hasProp(props = [], prop = '', options = DEFAULT_OPTIONS) {
|
---|
| 13 | const propToCheck = options.ignoreCase ? prop.toUpperCase() : prop;
|
---|
| 14 |
|
---|
| 15 | return props.some((attribute) => {
|
---|
| 16 | // If the props contain a spread prop, then refer to strict param.
|
---|
| 17 | if (attribute.type === 'JSXSpreadAttribute') {
|
---|
| 18 | return !options.spreadStrict;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | const currentProp = options.ignoreCase
|
---|
| 22 | ? propName(attribute).toUpperCase()
|
---|
| 23 | : propName(attribute);
|
---|
| 24 |
|
---|
| 25 | return propToCheck === currentProp;
|
---|
| 26 | });
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * Given the props on a node and a list of props to check, this returns a boolean
|
---|
| 31 | * indicating if any of them exist on the node.
|
---|
| 32 | */
|
---|
| 33 | export function hasAnyProp(nodeProps = [], props = [], options = DEFAULT_OPTIONS) {
|
---|
| 34 | const propsToCheck = typeof props === 'string' ? props.split(' ') : props;
|
---|
| 35 |
|
---|
| 36 | return propsToCheck.some((prop) => hasProp(nodeProps, prop, options));
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Given the props on a node and a list of props to check, this returns a boolean
|
---|
| 41 | * indicating if all of them exist on the node
|
---|
| 42 | */
|
---|
| 43 | export function hasEveryProp(nodeProps = [], props = [], options = DEFAULT_OPTIONS) {
|
---|
| 44 | const propsToCheck = typeof props === 'string' ? props.split(' ') : props;
|
---|
| 45 |
|
---|
| 46 | return propsToCheck.every((prop) => hasProp(nodeProps, prop, options));
|
---|
| 47 | }
|
---|