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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const fill = require('fill-range');
  3. const utils = require('./utils');
  4. const compile = (ast, options = {}) => {
  5. let walk = (node, parent = {}) => {
  6. let invalidBlock = utils.isInvalidBrace(parent);
  7. let invalidNode = node.invalid === true && options.escapeInvalid === true;
  8. let invalid = invalidBlock === true || invalidNode === true;
  9. let prefix = options.escapeInvalid === true ? '\\' : '';
  10. let output = '';
  11. if (node.isOpen === true) {
  12. return prefix + node.value;
  13. }
  14. if (node.isClose === true) {
  15. return prefix + node.value;
  16. }
  17. if (node.type === 'open') {
  18. return invalid ? (prefix + node.value) : '(';
  19. }
  20. if (node.type === 'close') {
  21. return invalid ? (prefix + node.value) : ')';
  22. }
  23. if (node.type === 'comma') {
  24. return node.prev.type === 'comma' ? '' : (invalid ? node.value : '|');
  25. }
  26. if (node.value) {
  27. return node.value;
  28. }
  29. if (node.nodes && node.ranges > 0) {
  30. let args = utils.reduce(node.nodes);
  31. let range = fill(...args, { ...options, wrap: false, toRegex: true });
  32. if (range.length !== 0) {
  33. return args.length > 1 && range.length > 1 ? `(${range})` : range;
  34. }
  35. }
  36. if (node.nodes) {
  37. for (let child of node.nodes) {
  38. output += walk(child, node);
  39. }
  40. }
  41. return output;
  42. };
  43. return walk(ast);
  44. };
  45. module.exports = compile;