diffusion
Version:
Diffusion JavaScript client
41 lines (40 loc) • 1.42 kB
JavaScript
;
/**
* @module Util.RegEx
*
* @brief Utility functions for regular expressions
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.regex = void 0;
var errors_1 = require("./../../errors/errors");
/**
* Create a regex matcher function for a given regex expression, with an
* optional context for RegEx construction error messages.
*
* The returned function will evaluate the generated regex across the entire
* region of the given input. This is equivalent to Matcher.matches in Java.
*
* @param s the regular expression
* @param context the optional context message
* @returns the tester function
* @throws an {@link IllegalArgumentError} if the regex couldn't be created
*/
function regex(s, context) {
if (context === void 0) { context = ''; }
if (s === '') {
throw new errors_1.IllegalArgumentError('Empty regular expression [' + context + ']');
}
try {
var r_1 = new RegExp(s);
return function (test) {
// because JS regex will match any part of the test string,
// we have to verify the results to ensure the entire part matches
var m = r_1.exec(test);
return m && m[0] === test;
};
}
catch (e) {
throw new errors_1.IllegalArgumentError('Bad regular expression [' + e.message + ', ' + context + ']');
}
}
exports.regex = regex;