cucumber-expressions
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -7,5 +7,2 @@ 'use strict'; | ||
var Regex = require('becke-ch--regex--s0-0-v1--base--pl--lib'); | ||
var Group = require('./group'); | ||
var _require = require('./errors'), | ||
@@ -17,11 +14,10 @@ CucumberExpressionError = _require.CucumberExpressionError; | ||
key: 'build', | ||
value: function build(regexp, text, parameterTypes) { | ||
var m = new Regex(regexp).exec(text); | ||
if (!m) return null; | ||
value: function build(treeRegexp, text, parameterTypes) { | ||
var group = treeRegexp.match(text); | ||
if (!group) return null; | ||
var matchGroup = new Group(m); | ||
var argGroups = matchGroup.children; | ||
var argGroups = group.children; | ||
if (argGroups.length !== parameterTypes.length) { | ||
throw new CucumberExpressionError('Expression ' + regexp + ' has ' + argGroups.length + ' arguments (' + argGroups.map(function (g) { | ||
throw new CucumberExpressionError('Expression ' + treeRegexp.regexp + ' has ' + argGroups.length + ' capture groups (' + argGroups.map(function (g) { | ||
return g.value; | ||
@@ -28,0 +24,0 @@ }) + '), but there were ' + parameterTypes.length + ' parameter types (' + parameterTypes.map(function (p) { |
@@ -8,2 +8,3 @@ 'use strict'; | ||
var Argument = require('./argument'); | ||
var TreeRegexp = require('./tree_regexp'); | ||
@@ -49,3 +50,3 @@ var _require = require('./errors'), | ||
var text = expression.slice(matchOffset, match.index); | ||
var captureRegexp = getCaptureRegexp(parameterType.regexps); | ||
var captureRegexp = buildCaptureRegexp(parameterType.regexps); | ||
matchOffset = PARAMETER_REGEXP.lastIndex; | ||
@@ -57,3 +58,3 @@ regexp += text; | ||
regexp += '$'; | ||
this._regexp = new RegExp(regexp); | ||
this._treeRegexp = new TreeRegexp(regexp); | ||
} | ||
@@ -64,5 +65,10 @@ | ||
value: function match(text) { | ||
return Argument.build(this._regexp, text, this._parameterTypes); | ||
return Argument.build(this._treeRegexp, text, this._parameterTypes); | ||
} | ||
}, { | ||
key: 'regexp', | ||
get: function get() { | ||
return this._treeRegexp.regexp; | ||
} | ||
}, { | ||
key: 'source', | ||
@@ -77,3 +83,3 @@ get: function get() { | ||
function getCaptureRegexp(regexps) { | ||
function buildCaptureRegexp(regexps) { | ||
if (regexps.length === 1) { | ||
@@ -80,0 +86,0 @@ return '(' + regexps[0] + ')'; |
@@ -12,6 +12,6 @@ 'use strict'; | ||
var GeneratedExpression = function () { | ||
function GeneratedExpression(expression, parameterTypes) { | ||
function GeneratedExpression(expressionTemplate, parameterTypes) { | ||
_classCallCheck(this, GeneratedExpression); | ||
this._expressionTemplate = expression; | ||
this._expressionTemplate = expressionTemplate; | ||
this._parameterTypes = parameterTypes; | ||
@@ -18,0 +18,0 @@ } |
@@ -8,59 +8,30 @@ "use strict"; | ||
var Group = function () { | ||
function Group(a, b, c) { | ||
function Group(value, start, end, children) { | ||
_classCallCheck(this, Group); | ||
this.children = []; | ||
if (Array.isArray(a)) { | ||
this._parse(a); | ||
} else { | ||
this.start = a; | ||
this.end = b; | ||
this.value = c; | ||
} | ||
this._value = value; | ||
this._start = start; | ||
this._end = end; | ||
this._children = children; | ||
} | ||
_createClass(Group, [{ | ||
key: "contains", | ||
value: function contains(group) { | ||
return group.isNull() || group.start >= this.start && group.end <= this.end; | ||
key: "value", | ||
get: function get() { | ||
return this._value; | ||
} | ||
}, { | ||
key: "add", | ||
value: function add(group) { | ||
this.children.push(group); | ||
key: "start", | ||
get: function get() { | ||
return this._value; | ||
} | ||
}, { | ||
key: "isNull", | ||
value: function isNull() { | ||
return this.value === null; | ||
key: "end", | ||
get: function get() { | ||
return this._value; | ||
} | ||
}, { | ||
key: "_parse", | ||
value: function _parse(matches) { | ||
if (matches.length === 0) { | ||
this.start = this.end = -1; | ||
this.value = null; | ||
return; | ||
} | ||
this.start = matches.index[0]; | ||
this.end = matches.index[0] + matches[0].length; | ||
this.value = matches[0]; | ||
var stack = []; | ||
stack.push(this); | ||
for (var groupIndex = 1; groupIndex < matches.length; groupIndex++) { | ||
var value = matches[groupIndex] || null; | ||
var start = matches.index[groupIndex]; | ||
var end = value !== null && start >= 0 ? start + value.length : -1; | ||
var group = new Group(start, end, value); | ||
while (!stack[stack.length - 1].contains(group)) { | ||
stack.pop(); | ||
} | ||
stack[stack.length - 1].add(group); | ||
stack.push(group); | ||
} | ||
key: "children", | ||
get: function get() { | ||
return this._children; | ||
} | ||
@@ -67,0 +38,0 @@ }, { |
@@ -12,3 +12,3 @@ "use strict"; | ||
this._parameterType = parameter; | ||
this._regexp = regexp; | ||
this._treeRegexp = regexp; | ||
this._text = text; | ||
@@ -24,3 +24,3 @@ this._matchPosition = matchPosition || 0; | ||
value: function advanceTo(newMatchPosition) { | ||
return new ParameterTypeMatcher(this._parameterType, this._regexp, this._text, newMatchPosition); | ||
return new ParameterTypeMatcher(this._parameterType, this._treeRegexp, this._text, newMatchPosition); | ||
} | ||
@@ -27,0 +27,0 @@ }, { |
@@ -15,3 +15,3 @@ 'use strict'; | ||
var INTEGER_REGEXPS = [/-?\d+/, /\d+/]; | ||
var FLOAT_REGEXP = /-?\d*\.?\d+/; | ||
var FLOAT_REGEXP = /-?\d*\.\d+/; | ||
var WORD_REGEXP = /\w+/; | ||
@@ -18,0 +18,0 @@ var STRING_REGEXP = /"([^"\\]*(\\.[^"\\]*)*)"|'([^'\\]*(\\.[^'\\]*)*)'/; |
@@ -8,10 +8,12 @@ 'use strict'; | ||
var Argument = require('./argument'); | ||
var TreeRegexp = require('./tree_regexp'); | ||
var ParameterType = require('./parameter_type'); | ||
var RegularExpression = function () { | ||
function RegularExpression(regexp, parameterTypeRegistry) { | ||
function RegularExpression(expressionRegexp, parameterTypeRegistry) { | ||
_classCallCheck(this, RegularExpression); | ||
this._regexp = regexp; | ||
this._expressionRegexp = expressionRegexp; | ||
this._parameterTypeRegistry = parameterTypeRegistry; | ||
this._treeRegexp = new TreeRegexp(expressionRegexp); | ||
} | ||
@@ -27,8 +29,8 @@ | ||
var match = void 0; | ||
while ((match = CAPTURE_GROUP_PATTERN.exec(this._regexp.source)) !== null) { | ||
while ((match = CAPTURE_GROUP_PATTERN.exec(this._treeRegexp.regexp.source)) !== null) { | ||
var parameterTypeRegexp = match[1]; | ||
var parameterType = this._parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, this._regexp, text); | ||
var parameterType = this._parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, this._treeRegexp, text); | ||
if (!parameterType) { | ||
parameterType = new ParameterType('*', parameterTypeRegexp, String, function (s) { | ||
parameterType = new ParameterType(parameterTypeRegexp, parameterTypeRegexp, String, function (s) { | ||
return s; | ||
@@ -40,9 +42,14 @@ }, false, false); | ||
return Argument.build(this._regexp, text, parameterTypes); | ||
return Argument.build(this._treeRegexp, text, parameterTypes); | ||
} | ||
}, { | ||
key: 'getSource', | ||
value: function getSource() { | ||
return this._regexp.toString(); | ||
key: 'regexp', | ||
get: function get() { | ||
return this._expressionRegexp; | ||
} | ||
}, { | ||
key: 'source', | ||
get: function get() { | ||
return this._expressionRegexp.source; | ||
} | ||
}]); | ||
@@ -49,0 +56,0 @@ |
{ | ||
"name": "cucumber-expressions", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "Cucumber Expressions - a simpler alternative to Regular Expressions", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,16 +0,13 @@ | ||
const Regex = require('becke-ch--regex--s0-0-v1--base--pl--lib') | ||
const Group = require('./group') | ||
const { CucumberExpressionError } = require('./errors') | ||
class Argument { | ||
static build(regexp, text, parameterTypes) { | ||
const m = new Regex(regexp).exec(text) | ||
if (!m) return null | ||
static build(treeRegexp, text, parameterTypes) { | ||
const group = treeRegexp.match(text) | ||
if (!group) return null | ||
const matchGroup = new Group(m) | ||
const argGroups = matchGroup.children | ||
const argGroups = group.children | ||
if (argGroups.length !== parameterTypes.length) { | ||
throw new CucumberExpressionError( | ||
`Expression ${regexp} has ${argGroups.length} arguments (${argGroups.map( | ||
`Expression ${treeRegexp.regexp} has ${argGroups.length} capture groups (${argGroups.map( | ||
g => g.value | ||
@@ -17,0 +14,0 @@ )}), but there were ${parameterTypes.length} parameter types (${parameterTypes.map( |
const Argument = require('./argument') | ||
const TreeRegexp = require('./tree_regexp') | ||
const { UndefinedParameterTypeError } = require('./errors') | ||
@@ -39,3 +40,3 @@ | ||
const text = expression.slice(matchOffset, match.index) | ||
const captureRegexp = getCaptureRegexp(parameterType.regexps) | ||
const captureRegexp = buildCaptureRegexp(parameterType.regexps) | ||
matchOffset = PARAMETER_REGEXP.lastIndex | ||
@@ -47,9 +48,13 @@ regexp += text | ||
regexp += '$' | ||
this._regexp = new RegExp(regexp) | ||
this._treeRegexp = new TreeRegexp(regexp) | ||
} | ||
match(text) { | ||
return Argument.build(this._regexp, text, this._parameterTypes) | ||
return Argument.build(this._treeRegexp, text, this._parameterTypes) | ||
} | ||
get regexp() { | ||
return this._treeRegexp.regexp | ||
} | ||
get source() { | ||
@@ -60,3 +65,3 @@ return this._expression | ||
function getCaptureRegexp(regexps) { | ||
function buildCaptureRegexp(regexps) { | ||
if (regexps.length === 1) { | ||
@@ -63,0 +68,0 @@ return `(${regexps[0]})` |
const util = require('util') | ||
class GeneratedExpression { | ||
constructor(expression, parameterTypes) { | ||
this._expressionTemplate = expression | ||
constructor(expressionTemplate, parameterTypes) { | ||
this._expressionTemplate = expressionTemplate | ||
this._parameterTypes = parameterTypes | ||
@@ -7,0 +7,0 @@ } |
class Group { | ||
constructor(a, b, c) { | ||
this.children = [] | ||
constructor(value, start, end, children) { | ||
this._value = value | ||
this._start = start | ||
this._end = end | ||
this._children = children | ||
} | ||
if (Array.isArray(a)) { | ||
this._parse(a) | ||
} else { | ||
this.start = a | ||
this.end = b | ||
this.value = c | ||
} | ||
get value() { | ||
return this._value | ||
} | ||
contains(group) { | ||
return ( | ||
group.isNull() || (group.start >= this.start && group.end <= this.end) | ||
) | ||
get start() { | ||
return this._value | ||
} | ||
add(group) { | ||
this.children.push(group) | ||
get end() { | ||
return this._value | ||
} | ||
isNull() { | ||
return this.value === null | ||
get children() { | ||
return this._children | ||
} | ||
@@ -33,32 +30,4 @@ | ||
} | ||
_parse(matches) { | ||
if (matches.length === 0) { | ||
this.start = this.end = -1 | ||
this.value = null | ||
return | ||
} | ||
this.start = matches.index[0] | ||
this.end = matches.index[0] + matches[0].length | ||
this.value = matches[0] | ||
const stack = [] | ||
stack.push(this) | ||
for (let groupIndex = 1; groupIndex < matches.length; groupIndex++) { | ||
const value = matches[groupIndex] || null | ||
const start = matches.index[groupIndex] | ||
const end = value !== null && start >= 0 ? start + value.length : -1 | ||
const group = new Group(start, end, value) | ||
while (!stack[stack.length - 1].contains(group)) { | ||
stack.pop() | ||
} | ||
stack[stack.length - 1].add(group) | ||
stack.push(group) | ||
} | ||
} | ||
} | ||
module.exports = Group |
class ParameterTypeMatcher { | ||
constructor(parameter, regexp, text, matchPosition) { | ||
this._parameterType = parameter | ||
this._regexp = regexp | ||
this._treeRegexp = regexp | ||
this._text = text | ||
@@ -19,3 +19,3 @@ this._matchPosition = matchPosition || 0 | ||
this._parameterType, | ||
this._regexp, | ||
this._treeRegexp, | ||
this._text, | ||
@@ -22,0 +22,0 @@ newMatchPosition |
@@ -9,3 +9,3 @@ const ParameterType = require('./parameter_type') | ||
const INTEGER_REGEXPS = [/-?\d+/, /\d+/] | ||
const FLOAT_REGEXP = /-?\d*\.?\d+/ | ||
const FLOAT_REGEXP = /-?\d*\.\d+/ | ||
const WORD_REGEXP = /\w+/ | ||
@@ -12,0 +12,0 @@ const STRING_REGEXP = /"([^"\\]*(\\.[^"\\]*)*)"|'([^'\\]*(\\.[^'\\]*)*)'/ |
const Argument = require('./argument') | ||
const TreeRegexp = require('./tree_regexp') | ||
const ParameterType = require('./parameter_type') | ||
class RegularExpression { | ||
constructor(regexp, parameterTypeRegistry) { | ||
this._regexp = regexp | ||
constructor(expressionRegexp, parameterTypeRegistry) { | ||
this._expressionRegexp = expressionRegexp | ||
this._parameterTypeRegistry = parameterTypeRegistry | ||
this._treeRegexp = new TreeRegexp(expressionRegexp) | ||
} | ||
@@ -16,3 +18,6 @@ | ||
let match | ||
while ((match = CAPTURE_GROUP_PATTERN.exec(this._regexp.source)) !== null) { | ||
while ( | ||
(match = CAPTURE_GROUP_PATTERN.exec(this._treeRegexp.regexp.source)) !== | ||
null | ||
) { | ||
const parameterTypeRegexp = match[1] | ||
@@ -22,3 +27,3 @@ | ||
parameterTypeRegexp, | ||
this._regexp, | ||
this._treeRegexp, | ||
text | ||
@@ -28,4 +33,4 @@ ) | ||
parameterType = new ParameterType( | ||
'*', | ||
parameterTypeRegexp, | ||
parameterTypeRegexp, | ||
String, | ||
@@ -40,10 +45,14 @@ s => s, | ||
return Argument.build(this._regexp, text, parameterTypes) | ||
return Argument.build(this._treeRegexp, text, parameterTypes) | ||
} | ||
getSource() { | ||
return this._regexp.toString() | ||
get regexp() { | ||
return this._expressionRegexp | ||
} | ||
get source() { | ||
return this._expressionRegexp.source | ||
} | ||
} | ||
module.exports = RegularExpression |
@@ -25,3 +25,3 @@ /* eslint-env mocha */ | ||
"I have {float} cukes at {int} o'clock", | ||
/^I have (-?\d*\.?\d+) cukes at ((?:-?\d+)|(?:\d+)) o'clock$/ | ||
/^I have (-?\d*\.\d+) cukes at ((?:-?\d+)|(?:\d+)) o'clock$/ | ||
) | ||
@@ -44,3 +44,3 @@ }) | ||
) | ||
assert.deepEqual(cucumberExpression._regexp, expectedRegexp) | ||
assert.deepEqual(cucumberExpression.regexp, expectedRegexp) | ||
} |
@@ -53,8 +53,7 @@ /* eslint-env mocha */ | ||
it('exposes source', () => { | ||
const expr = /I have (\d+) cukes? in my (.+) now/ | ||
assert.deepEqual( | ||
new RegularExpression(expr, new ParameterTypeRegistry()).getSource(), | ||
expr.toString() | ||
) | ||
it('exposes regexp and source', () => { | ||
const regexp = /I have (\d+) cukes? in my (.+) now/ | ||
let expression = new RegularExpression(regexp, new ParameterTypeRegistry()) | ||
assert.deepEqual(expression.regexp, regexp) | ||
assert.deepEqual(expression.source, regexp.source) | ||
}) | ||
@@ -61,0 +60,0 @@ }) |
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
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
94114
53
2335
1