[6a3a178] | 1 | var _typeof = require("@babel/runtime/helpers/typeof")["default"];
|
---|
| 2 |
|
---|
| 3 | var setPrototypeOf = require("./setPrototypeOf.js");
|
---|
| 4 |
|
---|
| 5 | var inherits = require("./inherits.js");
|
---|
| 6 |
|
---|
| 7 | function _wrapRegExp() {
|
---|
| 8 | module.exports = _wrapRegExp = function _wrapRegExp(re, groups) {
|
---|
| 9 | return new BabelRegExp(re, undefined, groups);
|
---|
| 10 | };
|
---|
| 11 |
|
---|
| 12 | module.exports["default"] = module.exports, module.exports.__esModule = true;
|
---|
| 13 | var _super = RegExp.prototype;
|
---|
| 14 |
|
---|
| 15 | var _groups = new WeakMap();
|
---|
| 16 |
|
---|
| 17 | function BabelRegExp(re, flags, groups) {
|
---|
| 18 | var _this = new RegExp(re, flags);
|
---|
| 19 |
|
---|
| 20 | _groups.set(_this, groups || _groups.get(re));
|
---|
| 21 |
|
---|
| 22 | return setPrototypeOf(_this, BabelRegExp.prototype);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | inherits(BabelRegExp, RegExp);
|
---|
| 26 |
|
---|
| 27 | BabelRegExp.prototype.exec = function (str) {
|
---|
| 28 | var result = _super.exec.call(this, str);
|
---|
| 29 |
|
---|
| 30 | if (result) result.groups = buildGroups(result, this);
|
---|
| 31 | return result;
|
---|
| 32 | };
|
---|
| 33 |
|
---|
| 34 | BabelRegExp.prototype[Symbol.replace] = function (str, substitution) {
|
---|
| 35 | if (typeof substitution === "string") {
|
---|
| 36 | var groups = _groups.get(this);
|
---|
| 37 |
|
---|
| 38 | return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) {
|
---|
| 39 | return "$" + groups[name];
|
---|
| 40 | }));
|
---|
| 41 | } else if (typeof substitution === "function") {
|
---|
| 42 | var _this = this;
|
---|
| 43 |
|
---|
| 44 | return _super[Symbol.replace].call(this, str, function () {
|
---|
| 45 | var args = arguments;
|
---|
| 46 |
|
---|
| 47 | if (_typeof(args[args.length - 1]) !== "object") {
|
---|
| 48 | args = [].slice.call(args);
|
---|
| 49 | args.push(buildGroups(args, _this));
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | return substitution.apply(this, args);
|
---|
| 53 | });
|
---|
| 54 | } else {
|
---|
| 55 | return _super[Symbol.replace].call(this, str, substitution);
|
---|
| 56 | }
|
---|
| 57 | };
|
---|
| 58 |
|
---|
| 59 | function buildGroups(result, re) {
|
---|
| 60 | var g = _groups.get(re);
|
---|
| 61 |
|
---|
| 62 | return Object.keys(g).reduce(function (groups, name) {
|
---|
| 63 | groups[name] = result[g[name]];
|
---|
| 64 | return groups;
|
---|
| 65 | }, Object.create(null));
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return _wrapRegExp.apply(this, arguments);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | module.exports = _wrapRegExp;
|
---|
| 72 | module.exports["default"] = module.exports, module.exports.__esModule = true; |
---|