cucumber-expressions
Advanced tools
Comparing version 1.0.4 to 2.0.0
@@ -1,15 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
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, transformedValue) { | ||
_classCallCheck(this, Argument); | ||
class Argument { | ||
constructor(offset, value, transformedValue) { | ||
this._offset = offset; | ||
@@ -20,22 +8,15 @@ this._value = value; | ||
_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._transformedValue; | ||
} | ||
}]); | ||
get offset() { | ||
return this._offset; | ||
} | ||
return Argument; | ||
}(); | ||
get value() { | ||
return this._value; | ||
} | ||
exports.default = Argument; | ||
get transformedValue() { | ||
return this._transformedValue; | ||
} | ||
} | ||
module.exports = Argument; |
@@ -1,164 +0,79 @@ | ||
'use strict'; | ||
const TransformMatcher = require('./parameter_matcher'); | ||
const GeneratedExpression = require('./generated_expression'); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
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 _transform_matcher = require('./transform_matcher'); | ||
var _transform_matcher2 = _interopRequireDefault(_transform_matcher); | ||
var _generated_expression = require('./generated_expression'); | ||
var _generated_expression2 = _interopRequireDefault(_generated_expression); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var CucumberExpressionGenerator = function () { | ||
function CucumberExpressionGenerator(transformLookup) { | ||
_classCallCheck(this, CucumberExpressionGenerator); | ||
this._transformLookup = transformLookup; | ||
class CucumberExpressionGenerator { | ||
constructor(parameterRegistry) { | ||
this._parameterRegistry = parameterRegistry; | ||
} | ||
_createClass(CucumberExpressionGenerator, [{ | ||
key: 'generateExpression', | ||
value: function generateExpression(text, typed) { | ||
var argumentNames = []; | ||
var transformMatchers = this._createTransformMatchers(text); | ||
var transforms = []; | ||
generateExpression(text) { | ||
const parameterNames = []; | ||
const parameterMatchers = this._createTransformMatchers(text); | ||
const parameters = []; | ||
const usageByTypeName = {}; | ||
var expression = ""; | ||
var argCounter = 0; | ||
var pos = 0; | ||
let expression = ""; | ||
let pos = 0; | ||
while (true) { | ||
// eslint-disable-line no-constant-condition | ||
var matchingTransformMatchers = []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = transformMatchers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var transformMatcher = _step.value; | ||
var advancedTransformMatcher = transformMatcher.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; | ||
} | ||
} | ||
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); | ||
} | ||
} | ||
if (matchingTransformMatchers.length > 0) { | ||
var argumentName = 'arg' + ++argCounter; | ||
argumentNames.push(argumentName); | ||
matchingTransformMatchers = matchingTransformMatchers.sort(_transform_matcher2.default.compare); | ||
var bestTransformMatcher = matchingTransformMatchers[0]; | ||
transforms.push(bestTransformMatcher.transform); | ||
if (matchingTransformMatchers.length > 0) { | ||
matchingTransformMatchers = matchingTransformMatchers.sort(TransformMatcher.compare); | ||
const bestTransformMatcher = matchingTransformMatchers[0]; | ||
const parameter = bestTransformMatcher.parameter; | ||
parameters.push(parameter); | ||
expression += text.slice(pos, bestTransformMatcher.start); | ||
expression += '{' + argumentName; | ||
const parameterName = this._getParameterName(parameter.typeName, usageByTypeName); | ||
parameterNames.push(parameterName); | ||
if (typed) { | ||
expression += ':' + bestTransformMatcher.transform.typeName; | ||
} | ||
expression += "}"; | ||
pos = bestTransformMatcher.start + bestTransformMatcher.group.length; | ||
} else { | ||
break; | ||
} | ||
expression += text.slice(pos, bestTransformMatcher.start); | ||
expression += `{${parameter.typeName}}`; | ||
if (pos >= text.length) { | ||
break; | ||
} | ||
pos = bestTransformMatcher.start + bestTransformMatcher.group.length; | ||
} else { | ||
break; | ||
} | ||
expression += text.slice(pos); | ||
return new _generated_expression2.default(expression, argumentNames, transforms); | ||
if (pos >= text.length) { | ||
break; | ||
} | ||
} | ||
}, { | ||
key: '_createTransformMatchers', | ||
value: function _createTransformMatchers(text) { | ||
var transformMatchers = []; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = this._transformLookup.transforms[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var transform = _step2.value; | ||
expression += text.slice(pos); | ||
return new GeneratedExpression(expression, parameterNames, parameters); | ||
} | ||
transformMatchers = transformMatchers.concat(this._createTransformMatchers2(transform, text)); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
_getParameterName(typeName, usageByTypeName) { | ||
let count = usageByTypeName[typeName]; | ||
count = count ? count + 1 : 1; | ||
usageByTypeName[typeName] = count; | ||
return transformMatchers; | ||
return count == 1 ? typeName : `${typeName}${count}`; | ||
} | ||
_createTransformMatchers(text) { | ||
let parameterMatchers = []; | ||
for (let parameter of this._parameterRegistry.parameters) { | ||
parameterMatchers = parameterMatchers.concat(this._createTransformMatchers2(parameter, text)); | ||
} | ||
}, { | ||
key: '_createTransformMatchers2', | ||
value: function _createTransformMatchers2(transform, text) { | ||
var result = []; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
return parameterMatchers; | ||
} | ||
try { | ||
for (var _iterator3 = transform.captureGroupRegexps[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var captureGroupRegexp = _step3.value; | ||
result.push(new _transform_matcher2.default(transform, captureGroupRegexp, text)); | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
return result; | ||
_createTransformMatchers2(parameter, text) { | ||
const result = []; | ||
for (let captureGroupRegexp of parameter.captureGroupRegexps) { | ||
result.push(new TransformMatcher(parameter, captureGroupRegexp, text)); | ||
} | ||
}]); | ||
return result; | ||
} | ||
} | ||
return CucumberExpressionGenerator; | ||
}(); | ||
exports.default = CucumberExpressionGenerator; | ||
module.exports = CucumberExpressionGenerator; |
@@ -1,36 +0,23 @@ | ||
'use strict'; | ||
const matchPattern = require('./build_arguments'); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
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 _match_arguments = require('./match_arguments'); | ||
var _match_arguments2 = _interopRequireDefault(_match_arguments); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var CucumberExpression = function () { | ||
class CucumberExpression { | ||
/** | ||
* @param expression | ||
* @param types Array of type name (String) or types (function). Functions can be a regular function or a constructor | ||
* @param transformLookup | ||
* @param parameterRegistry | ||
*/ | ||
function CucumberExpression(expression, types, transformLookup) { | ||
_classCallCheck(this, CucumberExpression); | ||
constructor(expression, types, parameterRegistry) { | ||
const parameterPattern = /\{([^}:]+)(:([^}]+))?}/g; | ||
const optionalPattern = /\(([^)]+)\)/g; | ||
var parameterPattern = /\{([^}:]+)(:([^}]+))?}/g; | ||
var optionalPattern = /\(([^\)]+)\)/g; | ||
this._expression = expression; | ||
this._transforms = []; | ||
var regexp = "^"; | ||
var typeIndex = 0; | ||
var match = void 0; | ||
var matchOffset = 0; | ||
this._parameters = []; | ||
let regexp = "^"; | ||
let typeIndex = 0; | ||
let match; | ||
let matchOffset = 0; | ||
// Does not include (){} because they have special meaning | ||
expression = expression.replace(/([\\\^\[$.|?*+])/g, "\\$1"); | ||
// Create non-capturing, optional capture groups from parenthesis | ||
@@ -40,25 +27,29 @@ expression = expression.replace(optionalPattern, '(?:$1)?'); | ||
while ((match = parameterPattern.exec(expression)) !== null) { | ||
var parameterName = match[1]; | ||
var typeName = match[3]; | ||
var type = types.length <= typeIndex ? null : types[typeIndex++]; | ||
const parameterName = match[1]; | ||
const 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.`); | ||
} | ||
var transform = void 0; | ||
const type = types.length <= typeIndex ? null : types[typeIndex++]; | ||
let parameter; | ||
if (type) { | ||
transform = transformLookup.lookupByType(type); | ||
parameter = parameterRegistry.lookupByType(type); | ||
} | ||
if (!transform && typeName) { | ||
transform = transformLookup.lookupByTypeName(typeName, false); | ||
if (!parameter && typeName) { | ||
parameter = parameterRegistry.lookupByTypeName(typeName); | ||
} | ||
if (!transform) { | ||
transform = transformLookup.lookupByTypeName(parameterName, true); | ||
if (!parameter) { | ||
parameter = parameterRegistry.lookupByTypeName(parameterName); | ||
} | ||
if (!transform) { | ||
transform = transformLookup.createAnonymousLookup(function (s) { | ||
return s; | ||
}); | ||
if (!parameter) { | ||
parameter = parameterRegistry.createAnonymousLookup(s => s); | ||
} | ||
this._transforms.push(transform); | ||
this._parameters.push(parameter); | ||
var text = expression.slice(matchOffset, match.index); | ||
var captureRegexp = getCaptureRegexp(transform.captureGroupRegexps); | ||
const text = expression.slice(matchOffset, match.index); | ||
const captureRegexp = getCaptureRegexp(parameter.captureGroupRegexps); | ||
matchOffset = parameterPattern.lastIndex; | ||
@@ -73,29 +64,23 @@ regexp += text; | ||
_createClass(CucumberExpression, [{ | ||
key: 'match', | ||
value: function match(text) { | ||
return (0, _match_arguments2.default)(this._regexp, text, this._transforms); | ||
} | ||
}, { | ||
key: 'source', | ||
get: function get() { | ||
return this._expression; | ||
} | ||
}]); | ||
match(text) { | ||
return matchPattern(this._regexp, text, this._parameters); | ||
} | ||
return CucumberExpression; | ||
}(); | ||
get source() { | ||
return this._expression; | ||
} | ||
} | ||
function getCaptureRegexp(captureGroupRegexps) { | ||
if (captureGroupRegexps.length === 1) { | ||
return '(' + captureGroupRegexps[0] + ')'; | ||
return `(${captureGroupRegexps[0]})`; | ||
} | ||
var captureGroups = captureGroupRegexps.map(function (group) { | ||
return '(?:' + group + ')'; | ||
const captureGroups = captureGroupRegexps.map(group => { | ||
return `(?:${group})`; | ||
}); | ||
return '(' + captureGroups.join('|') + ')'; | ||
return `(${captureGroups.join('|')})`; | ||
} | ||
exports.default = CucumberExpression; | ||
module.exports = CucumberExpression; |
@@ -1,40 +0,29 @@ | ||
"use strict"; | ||
class GeneratedExpression { | ||
constructor(expression, parameterNames, parameters) { | ||
this._expression = expression; | ||
this._parameterNames = parameterNames; | ||
this._parameters = parameters; | ||
} | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
get source() { | ||
return this._expression; | ||
} | ||
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; }; }(); | ||
/** | ||
* Returns an array of parameter names to use in generated function/method signatures | ||
* | ||
* @returns {Array.<String>} | ||
*/ | ||
get parameterNames() { | ||
return this._parameterNames; | ||
} | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var GeneratedExpression = function () { | ||
function GeneratedExpression(expression, argumentNames, transforms) { | ||
_classCallCheck(this, GeneratedExpression); | ||
this._expression = expression; | ||
this._argumentNames = argumentNames; | ||
this._transforms = transforms; | ||
/** | ||
* @returns {Array.<Parameter>} | ||
*/ | ||
get parameters() { | ||
return this._parameters; | ||
} | ||
} | ||
_createClass(GeneratedExpression, [{ | ||
key: "source", | ||
get: function get() { | ||
return this._expression; | ||
} | ||
}, { | ||
key: "argumentNames", | ||
get: function get() { | ||
return this._argumentNames; | ||
} | ||
}, { | ||
key: "transforms", | ||
get: function get() { | ||
return this._transforms; | ||
} | ||
}]); | ||
return GeneratedExpression; | ||
}(); | ||
exports.default = GeneratedExpression; | ||
module.exports = GeneratedExpression; |
@@ -1,34 +0,13 @@ | ||
"use strict"; | ||
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"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.Transform = exports.TransformLookup = exports.CucumberExpressionGenerator = exports.RegularExpression = exports.CucumberExpression = undefined; | ||
var _cucumber_expression = require("./cucumber_expression"); | ||
var _cucumber_expression2 = _interopRequireDefault(_cucumber_expression); | ||
var _regular_expression = require("./regular_expression"); | ||
var _regular_expression2 = _interopRequireDefault(_regular_expression); | ||
var _cucumber_expression_generator = require("./cucumber_expression_generator"); | ||
var _cucumber_expression_generator2 = _interopRequireDefault(_cucumber_expression_generator); | ||
var _transform_lookup = require("./transform_lookup"); | ||
var _transform_lookup2 = _interopRequireDefault(_transform_lookup); | ||
var _transform = require("./transform"); | ||
var _transform2 = _interopRequireDefault(_transform); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.CucumberExpression = _cucumber_expression2.default; | ||
exports.RegularExpression = _regular_expression2.default; | ||
exports.CucumberExpressionGenerator = _cucumber_expression_generator2.default; | ||
exports.TransformLookup = _transform_lookup2.default; | ||
exports.Transform = _transform2.default; | ||
module.exports = { | ||
CucumberExpression, | ||
RegularExpression, | ||
CucumberExpressionGenerator, | ||
ParameterRegistry, | ||
Parameter | ||
}; |
@@ -1,63 +0,39 @@ | ||
'use strict'; | ||
const matchPattern = require('./build_arguments'); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
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 _match_arguments = require('./match_arguments'); | ||
var _match_arguments2 = _interopRequireDefault(_match_arguments); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var RegularExpression = function () { | ||
function RegularExpression(regexp, types, transformLookup) { | ||
_classCallCheck(this, RegularExpression); | ||
class RegularExpression { | ||
constructor(regexp, types, parameterRegistry) { | ||
this._regexp = regexp; | ||
this._transforms = []; | ||
this._parameters = []; | ||
var CAPTURE_GROUP_PATTERN = /\(([^(]+)\)/g; | ||
const CAPTURE_GROUP_PATTERN = /\(([^(]+)\)/g; | ||
var typeIndex = 0; | ||
var match = void 0; | ||
let typeIndex = 0; | ||
let match; | ||
while ((match = CAPTURE_GROUP_PATTERN.exec(regexp.source)) !== null) { | ||
var captureGroupPattern = match[1]; | ||
var type = types.length <= typeIndex ? null : types[typeIndex++]; | ||
const captureGroupPattern = match[1]; | ||
const type = types.length <= typeIndex ? null : types[typeIndex++]; | ||
var transform = void 0; | ||
let parameter; | ||
if (type) { | ||
transform = transformLookup.lookupByType(type); | ||
parameter = parameterRegistry.lookupByType(type); | ||
} | ||
if (!transform) { | ||
transform = transformLookup.lookupByCaptureGroupRegexp(captureGroupPattern); | ||
if (!parameter) { | ||
parameter = parameterRegistry.lookupByCaptureGroupRegexp(captureGroupPattern); | ||
} | ||
if (!transform) { | ||
transform = transformLookup.createAnonymousLookup(function (s) { | ||
return s; | ||
}); | ||
if (!parameter) { | ||
parameter = parameterRegistry.createAnonymousLookup(s => s); | ||
} | ||
this._transforms.push(transform); | ||
this._parameters.push(parameter); | ||
} | ||
} | ||
_createClass(RegularExpression, [{ | ||
key: 'match', | ||
value: function match(text) { | ||
return (0, _match_arguments2.default)(this._regexp, text, this._transforms); | ||
} | ||
}, { | ||
key: 'getSource', | ||
value: function getSource() { | ||
return this._regexp.toString(); | ||
} | ||
}]); | ||
match(text) { | ||
return matchPattern(this._regexp, text, this._parameters); | ||
} | ||
return RegularExpression; | ||
}(); | ||
getSource() { | ||
return this._regexp.toString(); | ||
} | ||
} | ||
exports.default = RegularExpression; | ||
module.exports = RegularExpression; |
@@ -5,6 +5,10 @@ I have {n} cuke(s) in my {bodypart} now | ||
--- | ||
I have {n:int} cuke(s) in my {bodypart} now | ||
I have {int} cuke(s) in my {bodypart} now | ||
I have 1 cuke in my belly now | ||
[1,"belly"] | ||
--- | ||
I have {int} cuke(s) and some \[]^$.|?*+ {something} | ||
I have 1 cuke and some \[]^$.|?*+ characters | ||
[1,"characters"] | ||
--- | ||
/I have (\d+) cukes? in my (.+) now/ | ||
@@ -11,0 +15,0 @@ I have 22 cukes in my belly now |
{ | ||
"name": "cucumber-expressions", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"description": "Cucumber Expressions - a simpler alternative to Regular Expressions", | ||
@@ -13,3 +13,3 @@ "main": "dist/index.js", | ||
"mocha": "mocha", | ||
"coverage": "ISTANBUL=true istanbul cover _mocha" | ||
"coverage": "node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- test" | ||
}, | ||
@@ -33,8 +33,7 @@ "repository": { | ||
"devDependencies": { | ||
"babel-cli": "^6.18.0", | ||
"babel-preset-es2015": "^6.18.0", | ||
"eslint": "^3.9.1", | ||
"babel-cli": "^6.23.0", | ||
"eslint": "^3.15.0", | ||
"eslint-config-eslint": "^3.0.0", | ||
"istanbul": "^1.1.0-alpha.1", | ||
"mocha": "^3.1.2" | ||
"istanbul": "1.0.0-alpha.2", | ||
"mocha": "^3.2.0" | ||
}, | ||
@@ -41,0 +40,0 @@ "files": [ |
@@ -9,5 +9,5 @@ var path = require('path') | ||
exec('npm run build', {cwd: path.join(__dirname, '..')}, function (err) { | ||
if (err) throw err; | ||
if (err) throw err | ||
}) | ||
} | ||
}) |
@@ -21,2 +21,2 @@ class Argument { | ||
export default Argument | ||
module.exports = Argument |
@@ -1,16 +0,16 @@ | ||
import TransformMatcher from './transform_matcher' | ||
import GeneratedExpression from './generated_expression' | ||
const TransformMatcher = require('./parameter_matcher') | ||
const GeneratedExpression = require('./generated_expression') | ||
class CucumberExpressionGenerator { | ||
constructor(transformLookup) { | ||
this._transformLookup = transformLookup | ||
constructor(parameterRegistry) { | ||
this._parameterRegistry = parameterRegistry | ||
} | ||
generateExpression(text, typed) { | ||
const argumentNames = [] | ||
const transformMatchers = this._createTransformMatchers(text) | ||
const transforms = [] | ||
generateExpression(text) { | ||
const parameterNames = [] | ||
const parameterMatchers = this._createTransformMatchers(text) | ||
const parameters = [] | ||
const usageByTypeName = {} | ||
let expression = "" | ||
let argCounter = 0 | ||
let pos = 0 | ||
@@ -20,4 +20,4 @@ | ||
let matchingTransformMatchers = [] | ||
for (let transformMatcher of transformMatchers) { | ||
const advancedTransformMatcher = transformMatcher.advanceTo(pos) | ||
for (const parameterMatcher of parameterMatchers) { | ||
const advancedTransformMatcher = parameterMatcher.advanceTo(pos) | ||
if (advancedTransformMatcher.find) { | ||
@@ -29,15 +29,13 @@ matchingTransformMatchers.push(advancedTransformMatcher) | ||
if (matchingTransformMatchers.length > 0) { | ||
const argumentName = `arg${++argCounter}` | ||
argumentNames.push(argumentName) | ||
matchingTransformMatchers = matchingTransformMatchers.sort(TransformMatcher.compare) | ||
const bestTransformMatcher = matchingTransformMatchers[0] | ||
transforms.push(bestTransformMatcher.transform) | ||
const parameter = bestTransformMatcher.parameter | ||
parameters.push(parameter) | ||
const parameterName = this._getParameterName(parameter.typeName, usageByTypeName) | ||
parameterNames.push(parameterName) | ||
expression += text.slice(pos, bestTransformMatcher.start) | ||
expression += `{${argumentName}` | ||
expression += `{${parameter.typeName}}` | ||
if (typed) { | ||
expression += `:${bestTransformMatcher.transform.typeName}` | ||
} | ||
expression += "}" | ||
pos = bestTransformMatcher.start + bestTransformMatcher.group.length | ||
@@ -54,17 +52,25 @@ } else { | ||
expression += text.slice(pos) | ||
return new GeneratedExpression(expression, argumentNames, transforms) | ||
return new GeneratedExpression(expression, parameterNames, parameters) | ||
} | ||
_getParameterName(typeName, usageByTypeName) { | ||
let count = usageByTypeName[typeName] | ||
count = count ? count + 1 : 1 | ||
usageByTypeName[typeName] = count | ||
return count == 1 ? typeName : `${typeName}${count}` | ||
} | ||
_createTransformMatchers(text) { | ||
let transformMatchers = [] | ||
for (let transform of this._transformLookup.transforms) { | ||
transformMatchers = transformMatchers.concat(this._createTransformMatchers2(transform, text)) | ||
let parameterMatchers = [] | ||
for (let parameter of this._parameterRegistry.parameters) { | ||
parameterMatchers = parameterMatchers.concat(this._createTransformMatchers2(parameter, text)) | ||
} | ||
return transformMatchers | ||
return parameterMatchers | ||
} | ||
_createTransformMatchers2(transform, text) { | ||
_createTransformMatchers2(parameter, text) { | ||
const result = [] | ||
for (let captureGroupRegexp of transform.captureGroupRegexps) { | ||
result.push(new TransformMatcher(transform, captureGroupRegexp, text)) | ||
for (let captureGroupRegexp of parameter.captureGroupRegexps) { | ||
result.push(new TransformMatcher(parameter, captureGroupRegexp, text)) | ||
} | ||
@@ -75,2 +81,2 @@ return result | ||
export default CucumberExpressionGenerator | ||
module.exports = CucumberExpressionGenerator |
@@ -1,2 +0,2 @@ | ||
import matchArguments from './match_arguments' | ||
const matchPattern = require('./build_arguments') | ||
@@ -7,10 +7,10 @@ class CucumberExpression { | ||
* @param types Array of type name (String) or types (function). Functions can be a regular function or a constructor | ||
* @param transformLookup | ||
* @param parameterRegistry | ||
*/ | ||
constructor(expression, types, transformLookup) { | ||
constructor(expression, types, parameterRegistry) { | ||
const parameterPattern = /\{([^}:]+)(:([^}]+))?}/g | ||
const optionalPattern = /\(([^\)]+)\)/g | ||
const optionalPattern = /\(([^)]+)\)/g | ||
this._expression = expression | ||
this._transforms = [] | ||
this._parameters = [] | ||
let regexp = "^" | ||
@@ -21,2 +21,5 @@ let typeIndex = 0 | ||
// Does not include (){} because they have special meaning | ||
expression = expression.replace(/([\\\^\[$.|?*+])/g, "\\$1") | ||
// Create non-capturing, optional capture groups from parenthesis | ||
@@ -28,21 +31,27 @@ expression = expression.replace(optionalPattern, '(?:$1)?') | ||
const 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.`) | ||
} | ||
const type = types.length <= typeIndex ? null : types[typeIndex++] | ||
let transform; | ||
let parameter | ||
if (type) { | ||
transform = transformLookup.lookupByType(type) | ||
parameter = parameterRegistry.lookupByType(type) | ||
} | ||
if (!transform && typeName) { | ||
transform = transformLookup.lookupByTypeName(typeName, false) | ||
if (!parameter && typeName) { | ||
parameter = parameterRegistry.lookupByTypeName(typeName) | ||
} | ||
if (!transform) { | ||
transform = transformLookup.lookupByTypeName(parameterName, true) | ||
if (!parameter) { | ||
parameter = parameterRegistry.lookupByTypeName(parameterName) | ||
} | ||
if (!transform) { | ||
transform = transformLookup.createAnonymousLookup(s => s) | ||
if (!parameter) { | ||
parameter = parameterRegistry.createAnonymousLookup(s => s) | ||
} | ||
this._transforms.push(transform) | ||
this._parameters.push(parameter) | ||
const text = expression.slice(matchOffset, match.index) | ||
const captureRegexp = getCaptureRegexp(transform.captureGroupRegexps) | ||
const captureRegexp = getCaptureRegexp(parameter.captureGroupRegexps) | ||
matchOffset = parameterPattern.lastIndex | ||
@@ -58,3 +67,3 @@ regexp += text | ||
match(text) { | ||
return matchArguments(this._regexp, text, this._transforms) | ||
return matchPattern(this._regexp, text, this._parameters) | ||
} | ||
@@ -79,2 +88,2 @@ | ||
export default CucumberExpression | ||
module.exports = CucumberExpression |
class GeneratedExpression { | ||
constructor(expression, argumentNames, transforms) { | ||
constructor(expression, parameterNames, parameters) { | ||
this._expression = expression | ||
this._argumentNames = argumentNames | ||
this._transforms = transforms | ||
this._parameterNames = parameterNames | ||
this._parameters = parameters | ||
} | ||
@@ -12,11 +12,19 @@ | ||
get argumentNames() { | ||
return this._argumentNames | ||
/** | ||
* Returns an array of parameter names to use in generated function/method signatures | ||
* | ||
* @returns {Array.<String>} | ||
*/ | ||
get parameterNames() { | ||
return this._parameterNames | ||
} | ||
get transforms() { | ||
return this._transforms | ||
/** | ||
* @returns {Array.<Parameter>} | ||
*/ | ||
get parameters() { | ||
return this._parameters | ||
} | ||
} | ||
export default GeneratedExpression | ||
module.exports = GeneratedExpression |
@@ -1,13 +0,13 @@ | ||
import CucumberExpression from "./cucumber_expression" | ||
import RegularExpression from "./regular_expression" | ||
import CucumberExpressionGenerator from "./cucumber_expression_generator" | ||
import TransformLookup from "./transform_lookup" | ||
import Transform from "./transform" | ||
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") | ||
export { | ||
module.exports = { | ||
CucumberExpression, | ||
RegularExpression, | ||
CucumberExpressionGenerator, | ||
TransformLookup, | ||
Transform | ||
ParameterRegistry, | ||
Parameter | ||
} |
@@ -1,7 +0,7 @@ | ||
import matchArguments from './match_arguments' | ||
const matchPattern = require('./build_arguments') | ||
class RegularExpression { | ||
constructor(regexp, types, transformLookup) { | ||
constructor(regexp, types, parameterRegistry) { | ||
this._regexp = regexp | ||
this._transforms = [] | ||
this._parameters = [] | ||
@@ -16,13 +16,13 @@ const CAPTURE_GROUP_PATTERN = /\(([^(]+)\)/g | ||
let transform; | ||
let parameter | ||
if (type) { | ||
transform = transformLookup.lookupByType(type) | ||
parameter = parameterRegistry.lookupByType(type) | ||
} | ||
if (!transform) { | ||
transform = transformLookup.lookupByCaptureGroupRegexp(captureGroupPattern) | ||
if (!parameter) { | ||
parameter = parameterRegistry.lookupByCaptureGroupRegexp(captureGroupPattern) | ||
} | ||
if (!transform) { | ||
transform = transformLookup.createAnonymousLookup(s => s) | ||
if (!parameter) { | ||
parameter = parameterRegistry.createAnonymousLookup(s => s) | ||
} | ||
this._transforms.push(transform) | ||
this._parameters.push(parameter) | ||
} | ||
@@ -32,3 +32,3 @@ } | ||
match(text) { | ||
return matchArguments(this._regexp, text, this._transforms) | ||
return matchPattern(this._regexp, text, this._parameters) | ||
} | ||
@@ -41,2 +41,2 @@ | ||
export default RegularExpression | ||
module.exports = RegularExpression |
/* eslint-env mocha */ | ||
import assert from 'assert' | ||
import CucumberExpressionGenerator from '../src/cucumber_expression_generator' | ||
import Transform from '../src/transform' | ||
import TransformLookup from '../src/transform_lookup' | ||
const assert = require('assert') | ||
const CucumberExpressionGenerator = require('../src/cucumber_expression_generator') | ||
const Parameter = require('../src/parameter') | ||
const ParameterRegistry = require('../src/parameter_registry') | ||
@@ -12,26 +12,24 @@ class Currency { | ||
let transformLookup, generator | ||
let parameterRegistry, generator | ||
function assertTypedExpression(expected, text) { | ||
assert.deepEqual(generator.generateExpression(text, true).source, expected) | ||
function assertExpression(expectedExpression, expectedArgumentNames, text) { | ||
const generatedExpression = generator.generateExpression(text) | ||
assert.deepEqual(generatedExpression.parameterNames, expectedArgumentNames) | ||
assert.equal(generatedExpression.source, expectedExpression) | ||
} | ||
function assertUntypedExpression(expected, text) { | ||
assert.deepEqual(generator.generateExpression(text, false).source, expected) | ||
} | ||
beforeEach(() => { | ||
transformLookup = new TransformLookup() | ||
generator = new CucumberExpressionGenerator(transformLookup) | ||
parameterRegistry = new ParameterRegistry() | ||
generator = new CucumberExpressionGenerator(parameterRegistry) | ||
}) | ||
it("documents expression generation", () => { | ||
const transformLookup = new TransformLookup() | ||
const parameterRegistry = new ParameterRegistry() | ||
/// [generate-expression] | ||
const generator = new CucumberExpressionGenerator(transformLookup) | ||
const generator = new CucumberExpressionGenerator(parameterRegistry) | ||
const undefinedStepText = "I have 2 cucumbers and 1.5 tomato" | ||
const generatedExpression = generator.generateExpression(undefinedStepText, true) | ||
assert.equal(generatedExpression.source, "I have {arg1:int} cucumbers and {arg2:float} tomato") | ||
assert.equal(generatedExpression.argumentNames[0], 'arg1') | ||
assert.equal(generatedExpression.transforms[1].typeName, 'float') | ||
const generatedExpression = generator.generateExpression(undefinedStepText) | ||
assert.equal(generatedExpression.source, "I have {int} cucumbers and {float} tomato") | ||
assert.equal(generatedExpression.parameterNames[0], 'int') | ||
assert.equal(generatedExpression.parameters[1].typeName, 'float') | ||
/// [generate-expression] | ||
@@ -41,8 +39,8 @@ }) | ||
it("generates expression for no args", () => { | ||
assertTypedExpression("hello", "hello") | ||
assertExpression("hello", [], "hello") | ||
}) | ||
it("generates expression for int double arg", () => { | ||
assertTypedExpression( | ||
"I have {arg1:int} cukes and {arg2:float} euro", | ||
it("generates expression for int float arg", () => { | ||
assertExpression( | ||
"I have {int} cukes and {float} euro", ["int", "float"], | ||
"I have 2 cukes and 1.5 euro") | ||
@@ -52,15 +50,15 @@ }) | ||
it("generates expression for just int", () => { | ||
assertTypedExpression( | ||
"{arg1:int}", | ||
assertExpression( | ||
"{int}", ["int"], | ||
"99999") | ||
}) | ||
it("generates expression without expression type", () => { | ||
assertUntypedExpression( | ||
"I have {arg1} cukes and {arg2} euro", | ||
"I have 2 cukes and 1.5 euro") | ||
it("numbers only second argument when builtin type is not reserved keyword", () => { | ||
assertExpression( | ||
"I have {float} cukes and {float} euro", ["float", "float2"], | ||
"I have 2.5 cukes and 1.5 euro") | ||
}) | ||
it("generates expression for custom type", () => { | ||
transformLookup.addTransform(new Transform( | ||
parameterRegistry.addParameter(new Parameter( | ||
'currency', | ||
@@ -72,4 +70,4 @@ Currency, | ||
assertTypedExpression( | ||
"I have a {arg1:currency} account", | ||
assertExpression( | ||
"I have a {currency} account", ["currency"], | ||
"I have a EUR account") | ||
@@ -79,3 +77,3 @@ }) | ||
it("prefers leftmost match when there is overlap", () => { | ||
transformLookup.addTransform(new Transform( | ||
parameterRegistry.addParameter(new Parameter( | ||
'currency', | ||
@@ -86,3 +84,3 @@ Currency, | ||
)) | ||
transformLookup.addTransform(new Transform( | ||
parameterRegistry.addParameter(new Parameter( | ||
'date', | ||
@@ -94,12 +92,12 @@ Date, | ||
assertTypedExpression( | ||
"a{arg1:date}defg", | ||
assertExpression( | ||
"a{date}defg", ["date"], | ||
"abcdefg") | ||
}) | ||
it("exposes transforms in generated expression", () => { | ||
const expression = generator.generateExpression("I have 2 cukes and 1.5 euro", true) | ||
const typeNames = expression.transforms.map(transform => transform.typeName) | ||
it("exposes parameter type names in generated expression", () => { | ||
const expression = generator.generateExpression("I have 2 cukes and 1.5 euro") | ||
const typeNames = expression.parameters.map(parameter => parameter.typeName) | ||
assert.deepEqual(typeNames, ['int', 'float']) | ||
}) | ||
}) |
/* eslint-env mocha */ | ||
import assert from 'assert' | ||
import CucumberExpression from '../src/cucumber_expression' | ||
import TransformLookup from '../src/transform_lookup' | ||
const assert = require('assert') | ||
const CucumberExpression = require('../src/cucumber_expression') | ||
const TransformLookup = require('../src/parameter_registry') | ||
@@ -24,3 +24,3 @@ describe(CucumberExpression.name, () => { | ||
assertRegexp( | ||
"I have {n:float} cukes in my {bodypart} at {time:int} o'clock", | ||
"I have {float} cukes in my {bodypart} at {int} o'clock", | ||
/^I have (-?\d*\.?\d+) cukes in my (.+) at ((?:-?\d+)|(?:\d+)) o'clock$/ | ||
@@ -27,0 +27,0 @@ ) |
/* eslint-env mocha */ | ||
import assert from 'assert' | ||
import { CucumberExpression, TransformLookup } from '../src/index' | ||
const assert = require('assert') | ||
const { CucumberExpression, ParameterRegistry } = require('../src/index') | ||
describe(CucumberExpression.name, () => { | ||
it("documents match arguments", () => { | ||
const transformLookup = new TransformLookup() | ||
const parameterRegistry = new ParameterRegistry() | ||
@@ -12,3 +12,3 @@ /// [capture-match-arguments] | ||
const types = ['int', null] | ||
const expression = new CucumberExpression(expr, types, transformLookup) | ||
const expression = new CucumberExpression(expr, types, parameterRegistry) | ||
const args = expression.match("I have 7 cukes in my belly now") | ||
@@ -20,8 +20,8 @@ assert.equal(7, args[0].transformedValue) | ||
it("transforms nothing by default", () => { | ||
it("does no transform by default", () => { | ||
assert.deepEqual(match("{what}", "22"), ["22"]) | ||
}) | ||
it("transforms to int by expression type", () => { | ||
assert.deepEqual(match("{what:int}", "22"), [22]) | ||
it("transforms to int by parameter type", () => { | ||
assert.deepEqual(match("{int}", "22"), [22]) | ||
}) | ||
@@ -33,9 +33,9 @@ | ||
it("doesn't match a float to an int", () => { | ||
assert.deepEqual(match("{what:int}", "1.22"), null) | ||
it("doesn't match a float with an int parameter", () => { | ||
assert.deepEqual(match("{int}", "1.22"), null) | ||
}) | ||
it("transforms to float by expression type", () => { | ||
assert.deepEqual(match("{what:float}", "0.22"), [0.22]) | ||
assert.deepEqual(match("{what:float}", ".22"), [0.22]) | ||
it("transforms to float by parameter type", () => { | ||
assert.deepEqual(match("{float}", "0.22"), [0.22]) | ||
assert.deepEqual(match("{float}", ".22"), [0.22]) | ||
}) | ||
@@ -48,17 +48,18 @@ | ||
it("doesn't transform unknown type", () => { | ||
assert.throws( | ||
() => match("{what:unknown}", "something"), | ||
/No transform for type name "unknown"/ | ||
) | ||
it("leaves unknown type untransformed", () => { | ||
assert.deepEqual(match("{unknown}", "something"), ['something']) | ||
}) | ||
it("supports deprecated {name:type} syntax for now", () => { | ||
assert.deepEqual(match("{param:unknown}", "something"), ['something']) | ||
}) | ||
it("exposes source", () => { | ||
const expr = "I have {n:int} cuke(s) in my {bodypart} now"; | ||
assert.equal(new CucumberExpression(expr, [], new TransformLookup()).source, expr) | ||
const expr = "I have {int} cuke(s) in my {bodypart} now" | ||
assert.equal(new CucumberExpression(expr, [], new ParameterRegistry()).source, expr) | ||
}) | ||
it("exposes offset and value", () => { | ||
const expr = "I have {n:int} cuke(s) in my {bodypart} now"; | ||
const expression = new CucumberExpression(expr, [], new TransformLookup()) | ||
const expr = "I have {int} cuke(s) in my {bodypart} now" | ||
const expression = new CucumberExpression(expr, [], new ParameterRegistry()) | ||
const arg1 = expression.match("I have 800 cukes in my brain now")[0] | ||
@@ -68,6 +69,37 @@ assert.equal(arg1.offset, 7) | ||
}) | ||
describe('RegExp special characters', () => { | ||
['\\', '[', ']', '^', '$', '.', '|', '?', '*', '+'].forEach((character) => { | ||
it(`escapes ${character}`, () => { | ||
const expr = `I have {int} cuke(s) and ${character}` | ||
const expression = new CucumberExpression(expr, [], new ParameterRegistry()) | ||
const arg1 = expression.match(`I have 800 cukes and ${character}`)[0] | ||
assert.equal(arg1.offset, 7) | ||
assert.equal(arg1.value, "800") | ||
}) | ||
}) | ||
it(`escapes .`, () => { | ||
const expr = `I have {int} cuke(s) and .` | ||
const expression = new CucumberExpression(expr, [], new ParameterRegistry()) | ||
assert.equal(expression.match(`I have 800 cukes and 3`), null) | ||
const arg1 = expression.match(`I have 800 cukes and .`)[0] | ||
assert.equal(arg1.offset, 7) | ||
assert.equal(arg1.value, "800") | ||
}) | ||
it(`escapes |`, () => { | ||
const expr = `I have {int} cuke(s) and a|b` | ||
const expression = new CucumberExpression(expr, [], new ParameterRegistry()) | ||
assert.equal(expression.match(`I have 800 cukes and a`), null) | ||
assert.equal(expression.match(`I have 800 cukes and b`), null) | ||
const arg1 = expression.match(`I have 800 cukes and a|b`)[0] | ||
assert.equal(arg1.offset, 7) | ||
assert.equal(arg1.value, "800") | ||
}) | ||
}) | ||
}) | ||
const match = (expression, text, types) => { | ||
const cucumberExpression = new CucumberExpression(expression, types || [], new TransformLookup()) | ||
const cucumberExpression = new CucumberExpression(expression, types || [], new ParameterRegistry()) | ||
const args = cucumberExpression.match(text) | ||
@@ -74,0 +106,0 @@ if (!args) return null |
/* eslint-env mocha */ | ||
import fs from 'fs' | ||
import assert from 'assert' | ||
import CucumberExpression from '../src/cucumber_expression' | ||
import RegularExpression from '../src/regular_expression' | ||
import TransformLookup from '../src/transform_lookup' | ||
const fs = require('fs') | ||
const assert = require('assert') | ||
const CucumberExpression = require('../src/cucumber_expression') | ||
const RegularExpression = require('../src/regular_expression') | ||
const TransformLookup = require('../src/parameter_registry') | ||
@@ -8,0 +8,0 @@ describe('examples.txt', () => { |
/* eslint-env mocha */ | ||
import assert from 'assert' | ||
import RegularExpression from '../src/regular_expression' | ||
import TransformLookup from '../src/transform_lookup' | ||
const assert = require('assert') | ||
const RegularExpression = require('../src/regular_expression') | ||
const TransformLookup = require('../src/parameter_registry') | ||
describe(RegularExpression.name, () => { | ||
it("documents match arguments", () => { | ||
const transformLookup = new TransformLookup() | ||
const parameterRegistry = new TransformLookup() | ||
@@ -13,3 +13,3 @@ /// [capture-match-arguments] | ||
const types = ['int', null] | ||
const expression = new RegularExpression(expr, types, transformLookup) | ||
const expression = new RegularExpression(expr, types, parameterRegistry) | ||
const args = expression.match("I have 7 cukes in my belly now") | ||
@@ -21,35 +21,35 @@ assert.equal(7, args[0].transformedValue) | ||
it("transforms to string by default", () => { | ||
it("does no transform by default", () => { | ||
assert.deepEqual(match(/(\d\d)/, "22"), ['22']) | ||
}) | ||
it("transforms integer to double using explicit type", () => { | ||
it("transforms int to float by explicit type name", () => { | ||
assert.deepEqual(match(/(.*)/, "22", ['float']), [22.0]) | ||
}) | ||
it("transforms integer to double using explicit function", () => { | ||
it("transforms int to float by explicit function", () => { | ||
assert.deepEqual(match(/(.*)/, "22", [parseFloat]), [22.0]) | ||
}) | ||
it("transforms to int using capture group pattern", () => { | ||
it("transforms int by parameter pattern", () => { | ||
assert.deepEqual(match(/(-?\d+)/, "22"), [22]) | ||
}) | ||
it("transforms to int using alternate capture group pattern", () => { | ||
it("transforms int by alternate parameter pattern", () => { | ||
assert.deepEqual(match(/(\d+)/, "22"), [22]) | ||
}) | ||
it("transforms double without integer value", () => { | ||
it("transforms float without integer part", () => { | ||
assert.deepEqual(match(/(.*)/, ".22", ['float']), [0.22]) | ||
}) | ||
it("transforms double with sign", () => { | ||
it("transforms float with sign", () => { | ||
assert.deepEqual(match(/(.*)/, "-1.22", ['float']), [-1.22]) | ||
}) | ||
it("transforms double with sign using function", () => { | ||
it("transforms float with sign using function", () => { | ||
assert.deepEqual(match(/(.*)/, "-1.22", [parseFloat]), [-1.22]) | ||
}) | ||
it("transforms double with sign using anonymous function", () => { | ||
it("transforms float with sign using anonymous function", () => { | ||
assert.deepEqual(match(/(.*)/, "-1.22", [s => parseFloat(s)]), [-1.22]) | ||
@@ -71,3 +71,3 @@ }) | ||
const expr = /I have (\d+) cukes? in my (.+) now/ | ||
assert.deepEqual(new RegularExpression(expr, [], new TransformLookup()).getSource(), expr.toString()); | ||
assert.deepEqual(new RegularExpression(expr, [], new TransformLookup()).getSource(), expr.toString()) | ||
}) | ||
@@ -77,4 +77,4 @@ }) | ||
const match = (regexp, text, types) => { | ||
var transformLookup = new TransformLookup() | ||
const regularExpression = new RegularExpression(regexp, types || [], transformLookup) | ||
var parameterRegistry = new TransformLookup() | ||
const regularExpression = new RegularExpression(regexp, types || [], parameterRegistry) | ||
const args = regularExpression.match(text) | ||
@@ -81,0 +81,0 @@ if (!args) return null |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
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
5
43919
1097
2