Projekt

Obecné

Profil

Stáhnout (1.95 KB) Statistiky
| Větev: | Revize:
1
var _typeof = require("../helpers/typeof");
2

    
3
var wrapNativeSuper = require("./wrapNativeSuper");
4

    
5
var getPrototypeOf = require("./getPrototypeOf");
6

    
7
var possibleConstructorReturn = require("./possibleConstructorReturn");
8

    
9
var inherits = require("./inherits");
10

    
11
function _wrapRegExp(re, groups) {
12
  module.exports = _wrapRegExp = function _wrapRegExp(re, groups) {
13
    return new BabelRegExp(re, undefined, groups);
14
  };
15

    
16
  var _RegExp = wrapNativeSuper(RegExp);
17

    
18
  var _super = RegExp.prototype;
19

    
20
  var _groups = new WeakMap();
21

    
22
  function BabelRegExp(re, flags, groups) {
23
    var _this = _RegExp.call(this, re, flags);
24

    
25
    _groups.set(_this, groups || _groups.get(re));
26

    
27
    return _this;
28
  }
29

    
30
  inherits(BabelRegExp, _RegExp);
31

    
32
  BabelRegExp.prototype.exec = function (str) {
33
    var result = _super.exec.call(this, str);
34

    
35
    if (result) result.groups = buildGroups(result, this);
36
    return result;
37
  };
38

    
39
  BabelRegExp.prototype[Symbol.replace] = function (str, substitution) {
40
    if (typeof substitution === "string") {
41
      var groups = _groups.get(this);
42

    
43
      return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) {
44
        return "$" + groups[name];
45
      }));
46
    } else if (typeof substitution === "function") {
47
      var _this = this;
48

    
49
      return _super[Symbol.replace].call(this, str, function () {
50
        var args = [];
51
        args.push.apply(args, arguments);
52

    
53
        if (_typeof(args[args.length - 1]) !== "object") {
54
          args.push(buildGroups(args, _this));
55
        }
56

    
57
        return substitution.apply(this, args);
58
      });
59
    } else {
60
      return _super[Symbol.replace].call(this, str, substitution);
61
    }
62
  };
63

    
64
  function buildGroups(result, re) {
65
    var g = _groups.get(re);
66

    
67
    return Object.keys(g).reduce(function (groups, name) {
68
      groups[name] = result[g[name]];
69
      return groups;
70
    }, Object.create(null));
71
  }
72

    
73
  return _wrapRegExp.apply(this, arguments);
74
}
75

    
76
module.exports = _wrapRegExp;
(80-80/80)