"use strict"; const { validateArguments, required } = require("@validatem/core"); const isString = require("@validatem/is-string"); const isRegularExpression = require("@validatem/is-regular-expression"); module.exports = function matchOrError(regex, string) { validateArguments(arguments, [ [ "regex", required, isRegularExpression ], [ "string", required, isString ] ]); let match = regex.exec(string); if (match == null) { throw new Error(`Regular expression ${regex.toString()} failed to match on string: ${string}`); } else { // NOTE: Follows `execall` format: https://www.npmjs.com/package/execall return { match: match[0], subMatches: match.slice(1), index: match.index }; } };