Projekt

Obecné

Profil

Stáhnout (1.9 KB) Statistiky
| Větev: | Revize:
1
import _typeof from "../../helpers/esm/typeof";
2
import wrapNativeSuper from "./wrapNativeSuper";
3
import getPrototypeOf from "./getPrototypeOf";
4
import possibleConstructorReturn from "./possibleConstructorReturn";
5
import inherits from "./inherits";
6
export default function _wrapRegExp(re, groups) {
7
  _wrapRegExp = function _wrapRegExp(re, groups) {
8
    return new BabelRegExp(re, undefined, groups);
9
  };
10

    
11
  var _RegExp = wrapNativeSuper(RegExp);
12

    
13
  var _super = RegExp.prototype;
14

    
15
  var _groups = new WeakMap();
16

    
17
  function BabelRegExp(re, flags, groups) {
18
    var _this = _RegExp.call(this, re, flags);
19

    
20
    _groups.set(_this, groups || _groups.get(re));
21

    
22
    return _this;
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 = [];
46
        args.push.apply(args, arguments);
47

    
48
        if (_typeof(args[args.length - 1]) !== "object") {
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
}
(80-80/80)