linkComponents.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @fileoverview Utility functions for propWrapperFunctions setting
  3. */
  4. 'use strict';
  5. const iterFrom = require('es-iterator-helpers/Iterator.from');
  6. const map = require('es-iterator-helpers/Iterator.prototype.map');
  7. /** TODO: type {(string | { name: string, linkAttribute: string })[]} */
  8. /** @type {any} */
  9. const DEFAULT_LINK_COMPONENTS = ['a'];
  10. const DEFAULT_LINK_ATTRIBUTE = 'href';
  11. /** TODO: type {(string | { name: string, formAttribute: string })[]} */
  12. /** @type {any} */
  13. const DEFAULT_FORM_COMPONENTS = ['form'];
  14. const DEFAULT_FORM_ATTRIBUTE = 'action';
  15. function getFormComponents(context) {
  16. const settings = context.settings || {};
  17. const formComponents = /** @type {typeof DEFAULT_FORM_COMPONENTS} */ (
  18. DEFAULT_FORM_COMPONENTS.concat(settings.formComponents || [])
  19. );
  20. return new Map(map(iterFrom(formComponents), (value) => {
  21. if (typeof value === 'string') {
  22. return [value, DEFAULT_FORM_ATTRIBUTE];
  23. }
  24. return [value.name, value.formAttribute];
  25. }));
  26. }
  27. function getLinkComponents(context) {
  28. const settings = context.settings || {};
  29. const linkComponents = /** @type {typeof DEFAULT_LINK_COMPONENTS} */ (
  30. DEFAULT_LINK_COMPONENTS.concat(settings.linkComponents || [])
  31. );
  32. return new Map(map(iterFrom(linkComponents), (value) => {
  33. if (typeof value === 'string') {
  34. return [value, DEFAULT_LINK_ATTRIBUTE];
  35. }
  36. return [value.name, value.linkAttribute];
  37. }));
  38. }
  39. module.exports = {
  40. getFormComponents,
  41. getLinkComponents,
  42. };