cucumber-expressions
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -1,21 +0,36 @@ | ||
class Argument { | ||
constructor(offset, value, transformedValue) { | ||
"use strict"; | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Argument = function () { | ||
function Argument(offset, value, parameter) { | ||
_classCallCheck(this, Argument); | ||
this._offset = offset; | ||
this._value = value; | ||
this._transformedValue = transformedValue; | ||
this._parameter = parameter; | ||
} | ||
get offset() { | ||
return this._offset; | ||
} | ||
_createClass(Argument, [{ | ||
key: "offset", | ||
get: function get() { | ||
return this._offset; | ||
} | ||
}, { | ||
key: "value", | ||
get: function get() { | ||
return this._value; | ||
} | ||
}, { | ||
key: "transformedValue", | ||
get: function get() { | ||
return this._parameter.transform(this._value); | ||
} | ||
}]); | ||
get value() { | ||
return this._value; | ||
} | ||
return Argument; | ||
}(); | ||
get transformedValue() { | ||
return this._transformedValue; | ||
} | ||
} | ||
module.exports = Argument; |
@@ -1,12 +0,14 @@ | ||
const Argument = require('./argument'); | ||
'use strict'; | ||
const buildArguments = (regexp, text, parameters) => { | ||
const m = regexp.exec(text); | ||
var Argument = require('./argument'); | ||
var buildArguments = function buildArguments(regexp, text, parameters) { | ||
var m = regexp.exec(text); | ||
if (!m) return null; | ||
let parameterIndex = 0; | ||
let offset = 0; | ||
return m.slice(1).map(value => { | ||
var parameterIndex = 0; | ||
var offset = 0; | ||
return m.slice(1).map(function (value) { | ||
offset = text.indexOf(value, offset); | ||
const transformedValue = parameters[parameterIndex++].transform(value); | ||
return new Argument(offset, value, transformedValue); | ||
var parameter = parameters[parameterIndex++]; | ||
return new Argument(offset, value, parameter); | ||
}); | ||
@@ -13,0 +15,0 @@ }; |
@@ -1,79 +0,160 @@ | ||
const TransformMatcher = require('./parameter_matcher'); | ||
const GeneratedExpression = require('./generated_expression'); | ||
'use strict'; | ||
class CucumberExpressionGenerator { | ||
constructor(parameterRegistry) { | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var TransformMatcher = require('./parameter_matcher'); | ||
var GeneratedExpression = require('./generated_expression'); | ||
var CucumberExpressionGenerator = function () { | ||
function CucumberExpressionGenerator(parameterRegistry) { | ||
_classCallCheck(this, CucumberExpressionGenerator); | ||
this._parameterRegistry = parameterRegistry; | ||
} | ||
generateExpression(text) { | ||
const parameterNames = []; | ||
const parameterMatchers = this._createTransformMatchers(text); | ||
const parameters = []; | ||
const usageByTypeName = {}; | ||
_createClass(CucumberExpressionGenerator, [{ | ||
key: 'generateExpression', | ||
value: function generateExpression(text) { | ||
var parameterNames = []; | ||
var parameterMatchers = this._createTransformMatchers(text); | ||
var parameters = []; | ||
var usageByTypeName = {}; | ||
let expression = ""; | ||
let pos = 0; | ||
var expression = ""; | ||
var pos = 0; | ||
while (true) { | ||
// eslint-disable-line no-constant-condition | ||
let matchingTransformMatchers = []; | ||
for (const parameterMatcher of parameterMatchers) { | ||
const advancedTransformMatcher = parameterMatcher.advanceTo(pos); | ||
if (advancedTransformMatcher.find) { | ||
matchingTransformMatchers.push(advancedTransformMatcher); | ||
while (true) { | ||
// eslint-disable-line no-constant-condition | ||
var matchingTransformMatchers = []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = parameterMatchers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var parameterMatcher = _step.value; | ||
var advancedTransformMatcher = parameterMatcher.advanceTo(pos); | ||
if (advancedTransformMatcher.find) { | ||
matchingTransformMatchers.push(advancedTransformMatcher); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
if (matchingTransformMatchers.length > 0) { | ||
matchingTransformMatchers = matchingTransformMatchers.sort(TransformMatcher.compare); | ||
const bestTransformMatcher = matchingTransformMatchers[0]; | ||
const parameter = bestTransformMatcher.parameter; | ||
parameters.push(parameter); | ||
if (matchingTransformMatchers.length > 0) { | ||
matchingTransformMatchers = matchingTransformMatchers.sort(TransformMatcher.compare); | ||
var bestTransformMatcher = matchingTransformMatchers[0]; | ||
var parameter = bestTransformMatcher.parameter; | ||
parameters.push(parameter); | ||
const parameterName = this._getParameterName(parameter.typeName, usageByTypeName); | ||
parameterNames.push(parameterName); | ||
var parameterName = this._getParameterName(parameter.typeName, usageByTypeName); | ||
parameterNames.push(parameterName); | ||
expression += text.slice(pos, bestTransformMatcher.start); | ||
expression += `{${parameter.typeName}}`; | ||
expression += text.slice(pos, bestTransformMatcher.start); | ||
expression += '{' + parameter.typeName + '}'; | ||
pos = bestTransformMatcher.start + bestTransformMatcher.group.length; | ||
} else { | ||
break; | ||
pos = bestTransformMatcher.start + bestTransformMatcher.group.length; | ||
} else { | ||
break; | ||
} | ||
if (pos >= text.length) { | ||
break; | ||
} | ||
} | ||
if (pos >= text.length) { | ||
break; | ||
} | ||
expression += text.slice(pos); | ||
return new GeneratedExpression(expression, parameterNames, parameters); | ||
} | ||
}, { | ||
key: '_getParameterName', | ||
value: function _getParameterName(typeName, usageByTypeName) { | ||
var count = usageByTypeName[typeName]; | ||
count = count ? count + 1 : 1; | ||
usageByTypeName[typeName] = count; | ||
expression += text.slice(pos); | ||
return new GeneratedExpression(expression, parameterNames, parameters); | ||
} | ||
return count == 1 ? typeName : '' + typeName + count; | ||
} | ||
}, { | ||
key: '_createTransformMatchers', | ||
value: function _createTransformMatchers(text) { | ||
var parameterMatchers = []; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
_getParameterName(typeName, usageByTypeName) { | ||
let count = usageByTypeName[typeName]; | ||
count = count ? count + 1 : 1; | ||
usageByTypeName[typeName] = count; | ||
try { | ||
for (var _iterator2 = this._parameterRegistry.parameters[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var parameter = _step2.value; | ||
return count == 1 ? typeName : `${typeName}${count}`; | ||
} | ||
parameterMatchers = parameterMatchers.concat(this._createTransformMatchers2(parameter, text)); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
_createTransformMatchers(text) { | ||
let parameterMatchers = []; | ||
for (let parameter of this._parameterRegistry.parameters) { | ||
parameterMatchers = parameterMatchers.concat(this._createTransformMatchers2(parameter, text)); | ||
return parameterMatchers; | ||
} | ||
return parameterMatchers; | ||
} | ||
}, { | ||
key: '_createTransformMatchers2', | ||
value: function _createTransformMatchers2(parameter, text) { | ||
var result = []; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
_createTransformMatchers2(parameter, text) { | ||
const result = []; | ||
for (let captureGroupRegexp of parameter.captureGroupRegexps) { | ||
result.push(new TransformMatcher(parameter, captureGroupRegexp, text)); | ||
try { | ||
for (var _iterator3 = parameter.captureGroupRegexps[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var captureGroupRegexp = _step3.value; | ||
result.push(new TransformMatcher(parameter, captureGroupRegexp, text)); | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
return result; | ||
} | ||
} | ||
}]); | ||
return CucumberExpressionGenerator; | ||
}(); | ||
module.exports = CucumberExpressionGenerator; |
@@ -1,4 +0,10 @@ | ||
const matchPattern = require('./build_arguments'); | ||
"use strict"; | ||
class CucumberExpression { | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var matchPattern = require('./build_arguments'); | ||
var CucumberExpression = function () { | ||
/** | ||
@@ -9,12 +15,14 @@ * @param expression | ||
*/ | ||
constructor(expression, types, parameterRegistry) { | ||
const parameterPattern = /\{([^}:]+)(:([^}]+))?}/g; | ||
const optionalPattern = /\(([^)]+)\)/g; | ||
function CucumberExpression(expression, types, parameterRegistry) { | ||
_classCallCheck(this, CucumberExpression); | ||
var parameterPattern = /\{([^}:]+)(:([^}]+))?}/g; | ||
var optionalPattern = /\(([^)]+)\)/g; | ||
this._expression = expression; | ||
this._parameters = []; | ||
let regexp = "^"; | ||
let typeIndex = 0; | ||
let match; | ||
let matchOffset = 0; | ||
var regexp = "^"; | ||
var typeIndex = 0; | ||
var match = void 0; | ||
var matchOffset = 0; | ||
@@ -28,13 +36,13 @@ // Does not include (){} because they have special meaning | ||
while ((match = parameterPattern.exec(expression)) !== null) { | ||
const parameterName = match[1]; | ||
const typeName = match[3]; | ||
var parameterName = match[1]; | ||
var typeName = match[3]; | ||
// eslint-disable-next-line no-console | ||
if (typeName && typeof console !== 'undefined' && typeof console.error == 'function') { | ||
// eslint-disable-next-line no-console | ||
console.error(`Cucumber expression parameter syntax {${parameterName}:${typeName}} is deprecated. Please use {${typeName}} instead.`); | ||
console.error("Cucumber expression parameter syntax {" + parameterName + ":" + typeName + "} is deprecated. Please use {" + typeName + "} instead."); | ||
} | ||
const type = types.length <= typeIndex ? null : types[typeIndex++]; | ||
var type = types.length <= typeIndex ? null : types[typeIndex++]; | ||
let parameter; | ||
var parameter = void 0; | ||
if (type) { | ||
@@ -50,8 +58,10 @@ parameter = parameterRegistry.lookupByType(type); | ||
if (!parameter) { | ||
parameter = parameterRegistry.createAnonymousLookup(s => s); | ||
parameter = parameterRegistry.createAnonymousLookup(function (s) { | ||
return s; | ||
}); | ||
} | ||
this._parameters.push(parameter); | ||
const text = expression.slice(matchOffset, match.index); | ||
const captureRegexp = getCaptureRegexp(parameter.captureGroupRegexps); | ||
var text = expression.slice(matchOffset, match.index); | ||
var captureRegexp = getCaptureRegexp(parameter.captureGroupRegexps); | ||
matchOffset = parameterPattern.lastIndex; | ||
@@ -66,23 +76,29 @@ regexp += text; | ||
match(text) { | ||
return matchPattern(this._regexp, text, this._parameters); | ||
} | ||
_createClass(CucumberExpression, [{ | ||
key: "match", | ||
value: function match(text) { | ||
return matchPattern(this._regexp, text, this._parameters); | ||
} | ||
}, { | ||
key: "source", | ||
get: function get() { | ||
return this._expression; | ||
} | ||
}]); | ||
get source() { | ||
return this._expression; | ||
} | ||
} | ||
return CucumberExpression; | ||
}(); | ||
function getCaptureRegexp(captureGroupRegexps) { | ||
if (captureGroupRegexps.length === 1) { | ||
return `(${captureGroupRegexps[0]})`; | ||
return "(" + captureGroupRegexps[0] + ")"; | ||
} | ||
const captureGroups = captureGroupRegexps.map(group => { | ||
return `(?:${group})`; | ||
var captureGroups = captureGroupRegexps.map(function (group) { | ||
return "(?:" + group + ")"; | ||
}); | ||
return `(${captureGroups.join('|')})`; | ||
return "(" + captureGroups.join('|') + ")"; | ||
} | ||
module.exports = CucumberExpression; |
@@ -1,3 +0,11 @@ | ||
class GeneratedExpression { | ||
constructor(expression, parameterNames, parameters) { | ||
"use strict"; | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var GeneratedExpression = function () { | ||
function GeneratedExpression(expression, parameterNames, parameters) { | ||
_classCallCheck(this, GeneratedExpression); | ||
this._expression = expression; | ||
@@ -8,23 +16,34 @@ this._parameterNames = parameterNames; | ||
get source() { | ||
return this._expression; | ||
} | ||
_createClass(GeneratedExpression, [{ | ||
key: "source", | ||
get: function get() { | ||
return this._expression; | ||
} | ||
/** | ||
* Returns an array of parameter names to use in generated function/method signatures | ||
* | ||
* @returns {Array.<String>} | ||
*/ | ||
get parameterNames() { | ||
return this._parameterNames; | ||
} | ||
/** | ||
* Returns an array of parameter names to use in generated function/method signatures | ||
* | ||
* @returns {Array.<String>} | ||
*/ | ||
/** | ||
* @returns {Array.<Parameter>} | ||
*/ | ||
get parameters() { | ||
return this._parameters; | ||
} | ||
} | ||
}, { | ||
key: "parameterNames", | ||
get: function get() { | ||
return this._parameterNames; | ||
} | ||
/** | ||
* @returns {Array.<Parameter>} | ||
*/ | ||
}, { | ||
key: "parameters", | ||
get: function get() { | ||
return this._parameters; | ||
} | ||
}]); | ||
return GeneratedExpression; | ||
}(); | ||
module.exports = GeneratedExpression; |
@@ -1,13 +0,15 @@ | ||
const CucumberExpression = require("./cucumber_expression"); | ||
const RegularExpression = require("./regular_expression"); | ||
const CucumberExpressionGenerator = require("./cucumber_expression_generator"); | ||
const ParameterRegistry = require("./parameter_registry"); | ||
const Parameter = require("./parameter"); | ||
"use strict"; | ||
var CucumberExpression = require("./cucumber_expression"); | ||
var RegularExpression = require("./regular_expression"); | ||
var CucumberExpressionGenerator = require("./cucumber_expression_generator"); | ||
var ParameterRegistry = require("./parameter_registry"); | ||
var Parameter = require("./parameter"); | ||
module.exports = { | ||
CucumberExpression, | ||
RegularExpression, | ||
CucumberExpressionGenerator, | ||
ParameterRegistry, | ||
Parameter | ||
CucumberExpression: CucumberExpression, | ||
RegularExpression: RegularExpression, | ||
CucumberExpressionGenerator: CucumberExpressionGenerator, | ||
ParameterRegistry: ParameterRegistry, | ||
Parameter: Parameter | ||
}; |
@@ -1,4 +0,11 @@ | ||
class ParameterMatcher { | ||
"use strict"; | ||
constructor(parameter, captureGroupRegexp, text, matchPosition) { | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var ParameterMatcher = function () { | ||
function ParameterMatcher(parameter, captureGroupRegexp, text, matchPosition) { | ||
_classCallCheck(this, ParameterMatcher); | ||
this._parameter = parameter; | ||
@@ -9,35 +16,45 @@ this._captureGroupRegexp = captureGroupRegexp; | ||
const regexp = new RegExp(`(${captureGroupRegexp})`); | ||
var regexp = new RegExp("(" + captureGroupRegexp + ")"); | ||
this._match = regexp.exec(text.slice(this._matchPosition)); | ||
} | ||
get parameter() { | ||
return this._parameter; | ||
} | ||
_createClass(ParameterMatcher, [{ | ||
key: "advanceTo", | ||
value: function advanceTo(newMatchPosition) { | ||
return new ParameterMatcher(this._parameter, this._captureGroupRegexp, this._text, newMatchPosition); | ||
} | ||
}, { | ||
key: "parameter", | ||
get: function get() { | ||
return this._parameter; | ||
} | ||
}, { | ||
key: "find", | ||
get: function get() { | ||
return this._match; | ||
} | ||
}, { | ||
key: "start", | ||
get: function get() { | ||
return this._matchPosition + this._match.index; | ||
} | ||
}, { | ||
key: "group", | ||
get: function get() { | ||
return this._match[1]; | ||
} | ||
}], [{ | ||
key: "compare", | ||
value: function compare(a, b) { | ||
var posComparison = a.start - b.start; | ||
if (posComparison != 0) return posComparison; | ||
var lengthComparison = b.group.length - a.group.length; | ||
if (lengthComparison != 0) return lengthComparison; | ||
return 0; | ||
} | ||
}]); | ||
advanceTo(newMatchPosition) { | ||
return new ParameterMatcher(this._parameter, this._captureGroupRegexp, this._text, newMatchPosition); | ||
} | ||
return ParameterMatcher; | ||
}(); | ||
get find() { | ||
return this._match; | ||
} | ||
get start() { | ||
return this._matchPosition + this._match.index; | ||
} | ||
get group() { | ||
return this._match[1]; | ||
} | ||
static compare(a, b) { | ||
const posComparison = a.start - b.start; | ||
if (posComparison != 0) return posComparison; | ||
const lengthComparison = b.group.length - a.group.length; | ||
if (lengthComparison != 0) return lengthComparison; | ||
return 0; | ||
} | ||
} | ||
module.exports = ParameterMatcher; |
@@ -1,5 +0,15 @@ | ||
const Parameter = require('./parameter'); | ||
'use strict'; | ||
class ParameterRegistry { | ||
constructor() { | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Parameter = require('./parameter'); | ||
var ParameterRegistry = function () { | ||
function ParameterRegistry() { | ||
_classCallCheck(this, ParameterRegistry); | ||
this._parametersByTypeName = new Map(); | ||
@@ -9,72 +19,104 @@ this._parametersByCaptureGroupRegexp = new Map(); | ||
const FIXNUM_REGEXPS = ["-?\\d+", "\\d+"]; | ||
const FLOATING_POINT_REGEXPS = ["-?\\d*\\.?\\d+"]; | ||
var INTEGER_REGEXPS = [/-?\d+/, /\d+/]; | ||
var FLOAT_REGEXPS = [/-?\d*\.?\d+/]; | ||
this.addParameter(new Parameter('int', Number, FIXNUM_REGEXPS, parseInt)); | ||
this.addParameter(new Parameter('float', Number, FLOATING_POINT_REGEXPS, parseFloat)); | ||
this.addParameter(new Parameter('int', Number, INTEGER_REGEXPS, parseInt)); | ||
this.addParameter(new Parameter('float', Number, FLOAT_REGEXPS, parseFloat)); | ||
} | ||
get parameters() { | ||
return this._parametersByTypeName.values(); | ||
} | ||
lookupByType(type) { | ||
if (typeof type === 'function') { | ||
return this.lookupByFunction(type); | ||
} else if (typeof type === 'string') { | ||
return this.lookupByTypeName(type); | ||
} else { | ||
throw new Error(`Type must be string or function, but was ${type} of type ${typeof type}`); | ||
_createClass(ParameterRegistry, [{ | ||
key: 'lookupByType', | ||
value: function lookupByType(type) { | ||
if (typeof type === 'function') { | ||
return this.lookupByFunction(type); | ||
} else if (typeof type === 'string') { | ||
return this.lookupByTypeName(type); | ||
} else { | ||
throw new Error('Type must be string or function, but was ' + type + ' of type ' + (typeof type === 'undefined' ? 'undefined' : _typeof(type))); | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'lookupByFunction', | ||
value: function lookupByFunction(fn) { | ||
if (fn.name) { | ||
var prefix = fn.name[0]; | ||
var looksLikeConstructor = prefix.toUpperCase() === prefix; | ||
lookupByFunction(fn) { | ||
if (fn.name) { | ||
const prefix = fn.name[0]; | ||
const looksLikeConstructor = prefix.toUpperCase() === prefix; | ||
let parameter; | ||
if (looksLikeConstructor) { | ||
parameter = this._parametersByConstructorName.get(fn.name); | ||
} | ||
if (!parameter) { | ||
const factory = s => { | ||
if (looksLikeConstructor) { | ||
return new fn(s); | ||
} else { | ||
return fn(s); | ||
} | ||
}; | ||
return this.createAnonymousLookup(factory); | ||
var parameter = void 0; | ||
if (looksLikeConstructor) { | ||
parameter = this._parametersByConstructorName.get(fn.name); | ||
} | ||
if (!parameter) { | ||
var factory = function factory(s) { | ||
if (looksLikeConstructor) { | ||
return new fn(s); | ||
} else { | ||
return fn(s); | ||
} | ||
}; | ||
return this.createAnonymousLookup(factory); | ||
} else { | ||
return parameter; | ||
} | ||
} else { | ||
return parameter; | ||
return this.createAnonymousLookup(fn); | ||
} | ||
} else { | ||
return this.createAnonymousLookup(fn); | ||
} | ||
} | ||
}, { | ||
key: 'lookupByTypeName', | ||
value: function lookupByTypeName(typeName) { | ||
return this._parametersByTypeName.get(typeName); | ||
} | ||
}, { | ||
key: 'lookupByCaptureGroupRegexp', | ||
value: function lookupByCaptureGroupRegexp(captureGroupRegexp) { | ||
return this._parametersByCaptureGroupRegexp.get(captureGroupRegexp); | ||
} | ||
}, { | ||
key: 'createAnonymousLookup', | ||
value: function createAnonymousLookup(fn) { | ||
return new Parameter(null, null, [".+"], fn); | ||
} | ||
}, { | ||
key: 'addParameter', | ||
value: function addParameter(parameter) { | ||
this._parametersByConstructorName.set(parameter.constructorFunction.name, parameter); | ||
lookupByTypeName(typeName) { | ||
return this._parametersByTypeName.get(typeName); | ||
} | ||
this._parametersByTypeName.set(parameter.typeName, parameter); | ||
lookupByCaptureGroupRegexp(captureGroupRegexp) { | ||
return this._parametersByCaptureGroupRegexp.get(captureGroupRegexp); | ||
} | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
createAnonymousLookup(fn) { | ||
return new Parameter(null, null, [".+"], fn); | ||
} | ||
try { | ||
for (var _iterator = parameter.captureGroupRegexps[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var captureGroupRegexp = _step.value; | ||
addParameter(parameter) { | ||
this._parametersByConstructorName.set(parameter.constructorFunction.name, parameter); | ||
this._parametersByCaptureGroupRegexp.set(captureGroupRegexp, parameter); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'parameters', | ||
get: function get() { | ||
return this._parametersByTypeName.values(); | ||
} | ||
}]); | ||
this._parametersByTypeName.set(parameter.typeName, parameter); | ||
return ParameterRegistry; | ||
}(); | ||
for (let captureGroupRegexp of parameter.captureGroupRegexps) { | ||
this._parametersByCaptureGroupRegexp.set(captureGroupRegexp, parameter); | ||
} | ||
} | ||
} | ||
module.exports = ParameterRegistry; |
@@ -1,3 +0,11 @@ | ||
class Parameter { | ||
constructor(typeName, constructorFunction, captureGroupRegexps, transform) { | ||
'use strict'; | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Parameter = function () { | ||
function Parameter(typeName, constructorFunction, captureGroupRegexps, transform) { | ||
_classCallCheck(this, Parameter); | ||
this._typeName = typeName; | ||
@@ -9,24 +17,34 @@ this._constructorFunction = constructorFunction; | ||
get typeName() { | ||
return this._typeName; | ||
} | ||
_createClass(Parameter, [{ | ||
key: 'transform', | ||
value: function transform(string) { | ||
return this._transform(string); | ||
} | ||
}, { | ||
key: 'typeName', | ||
get: function get() { | ||
return this._typeName; | ||
} | ||
}, { | ||
key: 'constructorFunction', | ||
get: function get() { | ||
return this._constructorFunction; | ||
} | ||
}, { | ||
key: 'captureGroupRegexps', | ||
get: function get() { | ||
return this._captureGroupRegexps; | ||
} | ||
}]); | ||
get constructorFunction() { | ||
return this._constructorFunction; | ||
} | ||
return Parameter; | ||
}(); | ||
get captureGroupRegexps() { | ||
return this._captureGroupRegexps; | ||
} | ||
transform(string) { | ||
return this._transform(string); | ||
} | ||
} | ||
function stringArray(captureGroupRegexps) { | ||
const array = Array.isArray(captureGroupRegexps) ? captureGroupRegexps : [captureGroupRegexps]; | ||
return array.map(r => typeof r == 'string' ? r : r.source); | ||
var array = Array.isArray(captureGroupRegexps) ? captureGroupRegexps : [captureGroupRegexps]; | ||
return array.map(function (r) { | ||
return typeof r == 'string' ? r : r.source; | ||
}); | ||
} | ||
module.exports = Parameter; |
@@ -1,17 +0,25 @@ | ||
const matchPattern = require('./build_arguments'); | ||
'use strict'; | ||
class RegularExpression { | ||
constructor(regexp, types, parameterRegistry) { | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var matchPattern = require('./build_arguments'); | ||
var RegularExpression = function () { | ||
function RegularExpression(regexp, types, parameterRegistry) { | ||
_classCallCheck(this, RegularExpression); | ||
this._regexp = regexp; | ||
this._parameters = []; | ||
const CAPTURE_GROUP_PATTERN = /\(([^(]+)\)/g; | ||
var CAPTURE_GROUP_PATTERN = /\(([^(]+)\)/g; | ||
let typeIndex = 0; | ||
let match; | ||
var typeIndex = 0; | ||
var match = void 0; | ||
while ((match = CAPTURE_GROUP_PATTERN.exec(regexp.source)) !== null) { | ||
const captureGroupPattern = match[1]; | ||
const type = types.length <= typeIndex ? null : types[typeIndex++]; | ||
var captureGroupPattern = match[1]; | ||
var type = types.length <= typeIndex ? null : types[typeIndex++]; | ||
let parameter; | ||
var parameter = void 0; | ||
if (type) { | ||
@@ -24,3 +32,5 @@ parameter = parameterRegistry.lookupByType(type); | ||
if (!parameter) { | ||
parameter = parameterRegistry.createAnonymousLookup(s => s); | ||
parameter = parameterRegistry.createAnonymousLookup(function (s) { | ||
return s; | ||
}); | ||
} | ||
@@ -31,11 +41,17 @@ this._parameters.push(parameter); | ||
match(text) { | ||
return matchPattern(this._regexp, text, this._parameters); | ||
} | ||
_createClass(RegularExpression, [{ | ||
key: 'match', | ||
value: function match(text) { | ||
return matchPattern(this._regexp, text, this._parameters); | ||
} | ||
}, { | ||
key: 'getSource', | ||
value: function getSource() { | ||
return this._regexp.toString(); | ||
} | ||
}]); | ||
getSource() { | ||
return this._regexp.toString(); | ||
} | ||
} | ||
return RegularExpression; | ||
}(); | ||
module.exports = RegularExpression; |
{ | ||
"name": "cucumber-expressions", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Cucumber Expressions - a simpler alternative to Regular Expressions", | ||
@@ -33,2 +33,3 @@ "main": "dist/index.js", | ||
"babel-cli": "^6.23.0", | ||
"babel-preset-es2015": "^6.22.0", | ||
"eslint": "^3.15.0", | ||
@@ -35,0 +36,0 @@ "eslint-config-eslint": "^3.0.0", |
class Argument { | ||
constructor(offset, value, transformedValue) { | ||
constructor(offset, value, parameter) { | ||
this._offset = offset | ||
this._value = value | ||
this._transformedValue = transformedValue | ||
this._parameter = parameter | ||
} | ||
@@ -17,3 +17,3 @@ | ||
get transformedValue() { | ||
return this._transformedValue | ||
return this._parameter.transform(this._value) | ||
} | ||
@@ -20,0 +20,0 @@ } |
@@ -10,4 +10,4 @@ const Argument = require('./argument') | ||
offset = text.indexOf(value, offset) | ||
const transformedValue = parameters[parameterIndex++].transform(value) | ||
return new Argument(offset, value, transformedValue) | ||
const parameter = parameters[parameterIndex++] | ||
return new Argument(offset, value, parameter) | ||
}) | ||
@@ -14,0 +14,0 @@ } |
@@ -9,7 +9,7 @@ const Parameter = require('./parameter') | ||
const FIXNUM_REGEXPS = ["-?\\d+", "\\d+"] | ||
const FLOATING_POINT_REGEXPS = ["-?\\d*\\.?\\d+"] | ||
const INTEGER_REGEXPS = [/-?\d+/, /\d+/] | ||
const FLOAT_REGEXPS = [/-?\d*\.?\d+/] | ||
this.addParameter(new Parameter('int', Number, FIXNUM_REGEXPS, parseInt)) | ||
this.addParameter(new Parameter('float', Number, FLOATING_POINT_REGEXPS, parseFloat)) | ||
this.addParameter(new Parameter('int', Number, INTEGER_REGEXPS, parseInt)) | ||
this.addParameter(new Parameter('float', Number, FLOAT_REGEXPS, parseFloat)) | ||
} | ||
@@ -16,0 +16,0 @@ |
@@ -58,2 +58,15 @@ /* eslint-env mocha */ | ||
it("defers transformation until queried from argument", () => { | ||
parameterRegistry.addParameter(new Parameter( | ||
'throwing', | ||
() => null, | ||
/bad/, | ||
s => { throw new Error(`Can't transform [${s}]`) } | ||
)) | ||
const expression = new CucumberExpression("I have a {throwing} parameter", [], parameterRegistry) | ||
const args = expression.match("I have a bad parameter") | ||
assert.throws(() => args[0].transformedValue, Error, "Can't transform [bad]") | ||
}) | ||
// JavaScript-specific | ||
@@ -68,8 +81,10 @@ it("matches untyped parameters with explicit type name", () => { | ||
it("creates arguments using async transform", async () => { | ||
/// [add-async-parameter] | ||
parameterRegistry.addParameter(new Parameter( | ||
'asyncColor', | ||
Color, | ||
['red|blue|yellow', '(?:dark|light) (?:red|blue|yellow)'], | ||
[/red|blue|yellow/], | ||
async s => new Color(s) | ||
)) | ||
/// [add-async-parameter] | ||
@@ -76,0 +91,0 @@ const expression = new CucumberExpression("I have a {asyncColor} ball", ['asyncColor'], parameterRegistry) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
56314
38
1313
1
6