regops
Version:
A small javascript library for performing operations on regular expressions
161 lines (160 loc) • 6.04 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
/** Convert a list of convert regexs to their source strings. */
function sourcify(list) {
return list
.filter(function (item) { return item; }) // remove null items
.map(function (item) { return item instanceof RegExp ? item.source : item; });
}
/** Put non-capturing brackets around a regex source string. */
function bracket(str) {
return "(?:" + str + ")";
}
/** If necessary, put non-capturing brackets around a regex source string. */
function autoBracket(str) {
if (/^[\w, ]*$/.test(str))
return str;
else
return bracket(str);
}
exports.autoBracket = autoBracket;
/** Concatenate a list a of regular expressions. */
function concat() {
var operands = [];
for (var _i = 0; _i < arguments.length; _i++) {
operands[_i] = arguments[_i];
}
return new RegExp(sourcify(operands)
.map(autoBracket)
.join(""));
}
exports.concat = concat;
/** Concatenate a list of regular expressions with a single-space (' ') delimiter.*/
function concatSpaced() {
var operands = [];
for (var _i = 0; _i < arguments.length; _i++) {
operands[_i] = arguments[_i];
}
return new RegExp(sourcify(operands)
.map(autoBracket)
.join(" "));
}
exports.concatSpaced = concatSpaced;
/** Combine regular expressions with OR (|) operator. */
function or() {
var operands = [];
for (var _i = 0; _i < arguments.length; _i++) {
operands[_i] = arguments[_i];
}
return new RegExp(sourcify(operands)
.map(autoBracket)
.join("|"));
}
exports.or = or;
/** Apply OPTIONAL (?) operator to a regular expressions. */
function optional(operand) {
operand = new RegExp(operand).source;
operand = bracket(operand);
return operand + "?";
}
exports.optional = optional;
/** Apply Kleene closure (*) operator to a regular expression. */
function kleene(operand) {
operand = new RegExp(operand).source;
operand = bracket(operand);
return operand + "*";
}
exports.kleene = kleene;
/** Apply Kleene closure (*) operator to a regular expression, delimiting repetitions with a single-space (' ') */
function kleeneSpaced(operand) {
return kleeneJoin(operand, ' ');
}
exports.kleeneSpaced = kleeneSpaced;
/** Apply Kleene repetitions of a regular expression with some specified delimiter. */
function kleeneJoin(operand, delimiter) {
operand = new RegExp(operand).source;
delimiter = new RegExp(delimiter).source;
return concat(operand, kleene(concat(delimiter, operand)));
}
exports.kleeneJoin = kleeneJoin;
/** Create a "polite list" (form: X, X, X and X) using Kleene closure to allow any number of items. */
function kleenePoliteList() {
var operands = [];
for (var _i = 0; _i < arguments.length; _i++) {
operands[_i] = arguments[_i];
}
var operand = or.apply(void 0, __spread(operands));
return concat(optional(concat(kleeneJoin(operand, ', '), ',? and ')), operand);
}
exports.kleenePoliteList = kleenePoliteList;
/** Concatenate a list of regular expressions with optional (?) modifiers. */
function optionalConcatSpaced(stem) {
var optionalAppendages = [];
for (var _i = 1; _i < arguments.length; _i++) {
optionalAppendages[_i - 1] = arguments[_i];
}
stem = autoBracket(new RegExp(stem).source);
optionalAppendages = sourcify(optionalAppendages)
.map(function (a) { return autoBracket(a); })
.map(function (a) { return optional(" " + a); });
return concat.apply(void 0, __spread([stem], optionalAppendages));
}
exports.optionalConcatSpaced = optionalConcatSpaced;
/** Concatenate an item with itself any number of times (using kleene closure *) using a single-space (' ') as a delimiter. */
function kleeneConcatSpaced(stem) {
var optionalAppendages = [];
for (var _i = 1; _i < arguments.length; _i++) {
optionalAppendages[_i - 1] = arguments[_i];
}
stem = autoBracket(new RegExp(stem).source);
optionalAppendages = sourcify(optionalAppendages);
var toConcat = kleene(concat(' ', or.apply(void 0, __spread(optionalAppendages)).source));
return concat(stem, toConcat);
}
exports.kleeneConcatSpaced = kleeneConcatSpaced;
/** Add ^ and $ markers either side of a regular expression so that it must match an entire string. */
function whole(operand) {
operand = autoBracket(new RegExp(operand).source);
return new RegExp('^' + operand + '$');
}
exports.whole = whole;
/** Add a ^ marker at the beginning of a regular expression, so that it must match the beginning of a string. */
function initial(operand) {
operand = autoBracket(new RegExp(operand).source);
return new RegExp('^' + operand);
}
exports.initial = initial;
/** Add a $ marker at the end of a regular expression, so that it matches the end of a string. */
function terminal(operand) {
operand = autoBracket(new RegExp(operand).source);
return new RegExp(operand + '$');
}
exports.terminal = terminal;
/** Surround a regular expression with capturing parentheses, optionally specifying a group name. */
function capture(operand, groupName) {
if (operand instanceof RegExp)
operand = operand.source;
var name = groupName ? '?<' + groupName + '>' : '';
return new RegExp('(' + name + operand + ')');
}
exports.capture = capture;