index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  4. */
  5. 'use strict';
  6. /**
  7. * The `RegExpTree` class provides runtime support for `compat-transpiler`
  8. * module from `regexp-tree`.
  9. *
  10. * E.g. it tracks names of the capturing groups, in order to access the
  11. * names on the matched result.
  12. *
  13. * It's a thin-wrapper on top of original regexp.
  14. */
  15. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  16. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  17. var RegExpTree = function () {
  18. /**
  19. * Initializes a `RegExpTree` instance.
  20. *
  21. * @param RegExp - a regular expression
  22. *
  23. * @param Object state:
  24. *
  25. * An extra state which may store any related to transformation
  26. * data, for example, names of the groups.
  27. *
  28. * - flags - original flags
  29. * - groups - names of the groups, and their indices
  30. * - source - original source
  31. */
  32. function RegExpTree(re, _ref) {
  33. var flags = _ref.flags,
  34. groups = _ref.groups,
  35. source = _ref.source;
  36. _classCallCheck(this, RegExpTree);
  37. this._re = re;
  38. this._groups = groups;
  39. // Original props.
  40. this.flags = flags;
  41. this.source = source || re.source;
  42. this.dotAll = flags.includes('s');
  43. // Inherited directly from `re`.
  44. this.global = re.global;
  45. this.ignoreCase = re.ignoreCase;
  46. this.multiline = re.multiline;
  47. this.sticky = re.sticky;
  48. this.unicode = re.unicode;
  49. }
  50. /**
  51. * Facade wrapper for RegExp `test` method.
  52. */
  53. _createClass(RegExpTree, [{
  54. key: 'test',
  55. value: function test(string) {
  56. return this._re.test(string);
  57. }
  58. /**
  59. * Facade wrapper for RegExp `compile` method.
  60. */
  61. }, {
  62. key: 'compile',
  63. value: function compile(string) {
  64. return this._re.compile(string);
  65. }
  66. /**
  67. * Facade wrapper for RegExp `toString` method.
  68. */
  69. }, {
  70. key: 'toString',
  71. value: function toString() {
  72. if (!this._toStringResult) {
  73. this._toStringResult = '/' + this.source + '/' + this.flags;
  74. }
  75. return this._toStringResult;
  76. }
  77. /**
  78. * Facade wrapper for RegExp `exec` method.
  79. */
  80. }, {
  81. key: 'exec',
  82. value: function exec(string) {
  83. var result = this._re.exec(string);
  84. if (!this._groups || !result) {
  85. return result;
  86. }
  87. result.groups = {};
  88. for (var group in this._groups) {
  89. var groupNumber = this._groups[group];
  90. result.groups[group] = result[groupNumber];
  91. }
  92. return result;
  93. }
  94. }]);
  95. return RegExpTree;
  96. }();
  97. module.exports = {
  98. RegExpTree: RegExpTree
  99. };