1 | "use strict";
|
2 | var __values = (this && this.__values) || function (o) {
|
3 | var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
|
4 | if (m) return m.call(o);
|
5 | return {
|
6 | next: function () {
|
7 | if (o && i >= o.length) o = void 0;
|
8 | return { value: o && o[i++], done: !o };
|
9 | }
|
10 | };
|
11 | };
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var Errors_1 = require("./Errors");
|
14 | var ILLEGAL_PARAMETER_NAME_PATTERN = /([[\]()$.|?*+])/;
|
15 | var UNESCAPE_PATTERN = function () { return /(\\([[$.|?*+\]]))/g; };
|
16 | var ParameterType = (function () {
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function ParameterType(name, regexps, type, transform, useForSnippets, preferForRegexpMatch) {
|
26 | this.name = name;
|
27 | this.type = type;
|
28 | this.useForSnippets = useForSnippets;
|
29 | this.preferForRegexpMatch = preferForRegexpMatch;
|
30 | if (transform === undefined) {
|
31 | transform = function (s) { return s; };
|
32 | }
|
33 | if (useForSnippets === undefined) {
|
34 | this.useForSnippets = true;
|
35 | }
|
36 | if (preferForRegexpMatch === undefined) {
|
37 | this.preferForRegexpMatch = false;
|
38 | }
|
39 | if (name) {
|
40 | ParameterType.checkParameterTypeName(name);
|
41 | }
|
42 | this.regexpStrings = stringArray(regexps);
|
43 | this.transformFn = transform;
|
44 | }
|
45 | ParameterType.compare = function (pt1, pt2) {
|
46 | if (pt1.preferForRegexpMatch && !pt2.preferForRegexpMatch) {
|
47 | return -1;
|
48 | }
|
49 | if (pt2.preferForRegexpMatch && !pt1.preferForRegexpMatch) {
|
50 | return 1;
|
51 | }
|
52 | return pt1.name.localeCompare(pt2.name);
|
53 | };
|
54 | ParameterType.checkParameterTypeName = function (typeName) {
|
55 | var unescapedTypeName = typeName.replace(UNESCAPE_PATTERN(), '$2');
|
56 | var match = unescapedTypeName.match(ILLEGAL_PARAMETER_NAME_PATTERN);
|
57 | if (match) {
|
58 | throw new Errors_1.CucumberExpressionError("Illegal character '" + match[1] + "' in parameter name {" + unescapedTypeName + "}");
|
59 | }
|
60 | };
|
61 | ParameterType.prototype.transform = function (thisObj, groupValues) {
|
62 | return this.transformFn.apply(thisObj, groupValues);
|
63 | };
|
64 | return ParameterType;
|
65 | }());
|
66 | exports.default = ParameterType;
|
67 | function stringArray(regexps) {
|
68 | var array = Array.isArray(regexps) ? regexps : [regexps];
|
69 | return array.map(function (r) {
|
70 | return r instanceof RegExp ? regexpSource(r) : r;
|
71 | });
|
72 | }
|
73 | function regexpSource(regexp) {
|
74 | var e_1, _a;
|
75 | var flags = regexpFlags(regexp);
|
76 | try {
|
77 | for (var _b = __values(['g', 'i', 'm', 'y']), _c = _b.next(); !_c.done; _c = _b.next()) {
|
78 | var flag = _c.value;
|
79 | if (flags.indexOf(flag) !== -1) {
|
80 | throw new Errors_1.CucumberExpressionError("ParameterType Regexps can't use flag '" + flag + "'");
|
81 | }
|
82 | }
|
83 | }
|
84 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
85 | finally {
|
86 | try {
|
87 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
88 | }
|
89 | finally { if (e_1) throw e_1.error; }
|
90 | }
|
91 | return regexp.source;
|
92 | }
|
93 |
|
94 |
|
95 | function regexpFlags(regexp) {
|
96 | var flags = regexp.flags;
|
97 | if (flags === undefined) {
|
98 | flags = '';
|
99 | if (regexp.ignoreCase) {
|
100 | flags += 'i';
|
101 | }
|
102 | if (regexp.global) {
|
103 | flags += 'g';
|
104 | }
|
105 | if (regexp.multiline) {
|
106 | flags += 'm';
|
107 | }
|
108 | }
|
109 | return flags;
|
110 | }
|
111 |
|
\ | No newline at end of file |