hasProp.js 2.5 KB

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