UNPKG

4.1 kBJavaScriptView Raw
1"use strict";
2var __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};
12Object.defineProperty(exports, "__esModule", { value: true });
13var Errors_1 = require("./Errors");
14var ILLEGAL_PARAMETER_NAME_PATTERN = /([[\]()$.|?*+])/;
15var UNESCAPE_PATTERN = function () { return /(\\([[$.|?*+\]]))/g; };
16var ParameterType = /** @class */ (function () {
17 /**
18 * @param name {String} the name of the type
19 * @param regexps {Array.<RegExp>,RegExp,Array.<String>,String} that matches the type
20 * @param type {Function} the prototype (constructor) of the type. May be null.
21 * @param transform {Function} function transforming string to another type. May be null.
22 * @param useForSnippets {boolean} true if this should be used for snippets. Defaults to true.
23 * @param preferForRegexpMatch {boolean} true if this is a preferential type. Defaults to false.
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}());
66exports.default = ParameterType;
67function 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}
73function 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// Backport RegExp.flags for Node 4.x
94// https://github.com/nodejs/node/issues/8390
95function 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//# sourceMappingURL=ParameterType.js.map
\No newline at end of file