explode.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. const t = __importStar(require("@babel/types"));
  23. if (!(Array.isArray(t.TYPES) &&
  24. t.TYPES.every((t) => typeof t === 'string'))) {
  25. throw new Error('@babel/types TYPES does not match the expected type.');
  26. }
  27. const FLIPPED_ALIAS_KEYS = t
  28. .FLIPPED_ALIAS_KEYS;
  29. const TYPES = new Set(t.TYPES);
  30. if (!(FLIPPED_ALIAS_KEYS &&
  31. // tslint:disable-next-line: strict-type-predicates
  32. typeof FLIPPED_ALIAS_KEYS === 'object' &&
  33. Object.keys(FLIPPED_ALIAS_KEYS).every((key) => Array.isArray(FLIPPED_ALIAS_KEYS[key]) &&
  34. // tslint:disable-next-line: strict-type-predicates
  35. FLIPPED_ALIAS_KEYS[key].every((v) => typeof v === 'string')))) {
  36. throw new Error('@babel/types FLIPPED_ALIAS_KEYS does not match the expected type.');
  37. }
  38. /**
  39. * This serves thre functions:
  40. *
  41. * 1. Take any "aliases" and explode them to refecence the concrete types
  42. * 2. Normalize all handlers to have an `{enter, exit}` pair, rather than raw functions
  43. * 3. make the enter and exit handlers arrays, so that multiple handlers can be merged
  44. */
  45. function explode(input) {
  46. const results = {};
  47. for (const key in input) {
  48. const aliases = FLIPPED_ALIAS_KEYS[key];
  49. if (aliases) {
  50. for (const concreteKey of aliases) {
  51. if (concreteKey in results) {
  52. if (typeof input[key] === 'function') {
  53. results[concreteKey].enter.push(input[key]);
  54. }
  55. else {
  56. if (input[key].enter)
  57. results[concreteKey].enter.push(input[key].enter);
  58. if (input[key].exit)
  59. results[concreteKey].exit.push(input[key].exit);
  60. }
  61. }
  62. else {
  63. if (typeof input[key] === 'function') {
  64. results[concreteKey] = {
  65. enter: [input[key]],
  66. exit: [],
  67. };
  68. }
  69. else {
  70. results[concreteKey] = {
  71. enter: input[key].enter ? [input[key].enter] : [],
  72. exit: input[key].exit ? [input[key].exit] : [],
  73. };
  74. }
  75. }
  76. }
  77. }
  78. else if (TYPES.has(key)) {
  79. if (key in results) {
  80. if (typeof input[key] === 'function') {
  81. results[key].enter.push(input[key]);
  82. }
  83. else {
  84. if (input[key].enter)
  85. results[key].enter.push(input[key].enter);
  86. if (input[key].exit)
  87. results[key].exit.push(input[key].exit);
  88. }
  89. }
  90. else {
  91. if (typeof input[key] === 'function') {
  92. results[key] = {
  93. enter: [input[key]],
  94. exit: [],
  95. };
  96. }
  97. else {
  98. results[key] = {
  99. enter: input[key].enter ? [input[key].enter] : [],
  100. exit: input[key].exit ? [input[key].exit] : [],
  101. };
  102. }
  103. }
  104. }
  105. }
  106. return results;
  107. }
  108. exports.default = explode;
  109. //# sourceMappingURL=explode.js.map