propWrapper.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * @fileoverview Utility functions for propWrapperFunctions setting
  3. */
  4. 'use strict';
  5. function searchPropWrapperFunctions(name, propWrapperFunctions) {
  6. const splitName = name.split('.');
  7. return Array.from(propWrapperFunctions).some((func) => {
  8. if (splitName.length === 2 && func.object === splitName[0] && func.property === splitName[1]) {
  9. return true;
  10. }
  11. return name === func || func.property === name;
  12. });
  13. }
  14. function getPropWrapperFunctions(context) {
  15. return new Set(context.settings.propWrapperFunctions || []);
  16. }
  17. function isPropWrapperFunction(context, name) {
  18. if (typeof name !== 'string') {
  19. return false;
  20. }
  21. const propWrapperFunctions = getPropWrapperFunctions(context);
  22. return searchPropWrapperFunctions(name, propWrapperFunctions);
  23. }
  24. function getExactPropWrapperFunctions(context) {
  25. const propWrapperFunctions = getPropWrapperFunctions(context);
  26. const exactPropWrappers = Array.from(propWrapperFunctions).filter((func) => func.exact === true);
  27. return new Set(exactPropWrappers);
  28. }
  29. function isExactPropWrapperFunction(context, name) {
  30. const exactPropWrappers = getExactPropWrapperFunctions(context);
  31. return searchPropWrapperFunctions(name, exactPropWrappers);
  32. }
  33. function formatPropWrapperFunctions(propWrapperFunctions) {
  34. return Array.from(propWrapperFunctions, (func) => {
  35. if (func.object && func.property) {
  36. return `'${func.object}.${func.property}'`;
  37. }
  38. if (func.property) {
  39. return `'${func.property}'`;
  40. }
  41. return `'${func}'`;
  42. }).join(', ');
  43. }
  44. module.exports = {
  45. formatPropWrapperFunctions,
  46. getExactPropWrapperFunctions,
  47. getPropWrapperFunctions,
  48. isExactPropWrapperFunction,
  49. isPropWrapperFunction,
  50. };