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