source: imaps-frontend/node_modules/jsx-ast-utils/src/hasProp.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import propName from './propName';
2
3const 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 */
12export 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 */
33export 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 */
43export 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}
Note: See TracBrowser for help on using the repository browser.