You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
602 B
JavaScript
28 lines
602 B
JavaScript
"use strict";
|
|
|
|
const execall = require("execall");
|
|
const { ParseError } = require("../text-parser");
|
|
|
|
module.exports = function createRegexParser(regex, resultMapper) {
|
|
return {
|
|
supportsStreams: false,
|
|
parse: function (text) {
|
|
let matches = execall(regex, text);
|
|
|
|
if (matches.length > 0) {
|
|
let matchResult = (regex.global === true)
|
|
? matches
|
|
: matches[0];
|
|
|
|
if (resultMapper != null) {
|
|
return resultMapper(matchResult);
|
|
} else {
|
|
return matchResult;
|
|
}
|
|
} else {
|
|
throw new ParseError(`Input did not match regular expression ${regex}`);
|
|
}
|
|
}
|
|
};
|
|
};
|