Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 924B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const {htmlEscape} = require('escape-goat');
  3. module.exports = (template, data) => {
  4. if (typeof template !== 'string') {
  5. throw new TypeError(`Expected a \`string\` in the first argument, got \`${typeof template}\``);
  6. }
  7. if (typeof data !== 'object') {
  8. throw new TypeError(`Expected an \`object\` or \`Array\` in the second argument, got \`${typeof data}\``);
  9. }
  10. const doubleBraceRegex = /{{(.*?)}}/g;
  11. if (doubleBraceRegex.test(template)) {
  12. template = template.replace(doubleBraceRegex, (_, key) => {
  13. let result = data;
  14. for (const property of key.split('.')) {
  15. result = result ? result[property] : '';
  16. }
  17. return htmlEscape(String(result));
  18. });
  19. }
  20. const braceRegex = /{(.*?)}/g;
  21. return template.replace(braceRegex, (_, key) => {
  22. let result = data;
  23. for (const property of key.split('.')) {
  24. result = result ? result[property] : '';
  25. }
  26. return String(result);
  27. });
  28. };