propWrapper.js 1.8 KB

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