cucumber-expressions
Advanced tools
Comparing version 5.0.4 to 5.0.5
@@ -21,2 +21,4 @@ 'use strict'; | ||
// Does not include (){} characters because they have special meaning | ||
var ESCAPE_REGEXP = /([\\^[$.|?*+])/g; | ||
var PARAMETER_REGEXP = /{([^}]+)}/g; | ||
@@ -33,4 +35,5 @@ var OPTIONAL_REGEXP = /\(([^)]+)\)/g; | ||
// Does not include (){} because they have special meaning | ||
expression = expression.replace(/([\\^[$.|?*+])/g, '\\$1'); | ||
expression = expression.replace(ESCAPE_REGEXP, '\\$1'); | ||
// Create non-capturing, optional capture groups from parenthesis | ||
@@ -37,0 +40,0 @@ expression = expression.replace(OPTIONAL_REGEXP, '(?:$1)?'); |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -16,3 +16,3 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var captureGroupRegexp = new RegExp("(" + regexp + ")"); | ||
var captureGroupRegexp = new RegExp('(' + regexp + ')'); | ||
this._match = captureGroupRegexp.exec(text.slice(this._matchPosition)); | ||
@@ -22,3 +22,3 @@ } | ||
_createClass(ParameterTypeMatcher, [{ | ||
key: "advanceTo", | ||
key: 'advanceTo', | ||
value: function advanceTo(newMatchPosition) { | ||
@@ -28,3 +28,3 @@ return new ParameterTypeMatcher(this._parameterType, this._treeRegexp, this._text, newMatchPosition); | ||
}, { | ||
key: "parameterType", | ||
key: 'parameterType', | ||
get: function get() { | ||
@@ -34,8 +34,8 @@ return this._parameterType; | ||
}, { | ||
key: "find", | ||
key: 'find', | ||
get: function get() { | ||
return this._match; | ||
return this._match && this.group !== ''; | ||
} | ||
}, { | ||
key: "start", | ||
key: 'start', | ||
get: function get() { | ||
@@ -45,8 +45,8 @@ return this._matchPosition + this._match.index; | ||
}, { | ||
key: "group", | ||
key: 'group', | ||
get: function get() { | ||
return this._match[1]; | ||
return this._match[0]; | ||
} | ||
}], [{ | ||
key: "compare", | ||
key: 'compare', | ||
value: function compare(a, b) { | ||
@@ -53,0 +53,0 @@ var posComparison = a.start - b.start; |
@@ -96,6 +96,16 @@ 'use strict'; | ||
return array.map(function (r) { | ||
return typeof r === 'string' ? r : r.source; | ||
return typeof r === 'string' ? r : regexpSource(r); | ||
}); | ||
} | ||
function regexpSource(regexp) { | ||
var _arr = ['g', 'i', 'm', 'y']; | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var flag = _arr[_i]; | ||
if (regexp.flags.indexOf(flag) !== -1) throw new CucumberExpressionError('ParameterType Regexps can\'t use flag \'' + flag + '\''); | ||
} | ||
return regexp.source; | ||
} | ||
module.exports = ParameterType; |
{ | ||
"name": "cucumber-expressions", | ||
"version": "5.0.4", | ||
"version": "5.0.5", | ||
"description": "Cucumber Expressions - a simpler alternative to Regular Expressions", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
@@ -11,2 +11,4 @@ const Argument = require('./argument') | ||
constructor(expression, parameterTypeRegistry) { | ||
// Does not include (){} characters because they have special meaning | ||
const ESCAPE_REGEXP = /([\\^[$.|?*+])/g | ||
const PARAMETER_REGEXP = /{([^}]+)}/g | ||
@@ -23,4 +25,5 @@ const OPTIONAL_REGEXP = /\(([^)]+)\)/g | ||
// Does not include (){} because they have special meaning | ||
expression = expression.replace(/([\\^[$.|?*+])/g, '\\$1') | ||
expression = expression.replace(ESCAPE_REGEXP, '\\$1') | ||
// Create non-capturing, optional capture groups from parenthesis | ||
@@ -27,0 +30,0 @@ expression = expression.replace(OPTIONAL_REGEXP, '(?:$1)?') |
@@ -26,3 +26,3 @@ class ParameterTypeMatcher { | ||
get find() { | ||
return this._match | ||
return this._match && this.group !== '' | ||
} | ||
@@ -35,3 +35,3 @@ | ||
get group() { | ||
return this._match[1] | ||
return this._match[0] | ||
} | ||
@@ -38,0 +38,0 @@ |
@@ -81,5 +81,15 @@ const { CucumberExpressionError } = require('./errors') | ||
const array = Array.isArray(regexps) ? regexps : [regexps] | ||
return array.map(r => (typeof r === 'string' ? r : r.source)) | ||
return array.map(r => (typeof r === 'string' ? r : regexpSource(r))) | ||
} | ||
function regexpSource(regexp) { | ||
for (const flag of ['g', 'i', 'm', 'y']) { | ||
if (regexp.flags.indexOf(flag) !== -1) | ||
throw new CucumberExpressionError( | ||
`ParameterType Regexps can't use flag '${flag}'` | ||
) | ||
} | ||
return regexp.source | ||
} | ||
module.exports = ParameterType |
@@ -153,2 +153,33 @@ /* eslint-env mocha */ | ||
}) | ||
it('ignores parameter types with optional capture groups', () => { | ||
parameterTypeRegistry.defineParameterType( | ||
new ParameterType( | ||
'optional-flight', | ||
/(1st flight)?/, | ||
null, | ||
s => s, | ||
true, | ||
false | ||
) | ||
) | ||
parameterTypeRegistry.defineParameterType( | ||
new ParameterType( | ||
'optional-hotel', | ||
/(1st hotel)?/, | ||
null, | ||
s => s, | ||
true, | ||
false | ||
) | ||
) | ||
const expression = generator.generateExpressions( | ||
'I reach Stage4: 1st flight-1st hotl' | ||
)[0] | ||
assert.equal( | ||
expression.source, | ||
'I reach Stage{int}: {int}st flight{int}st hotl' | ||
) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
201823
54
2518