UNPKG

offensive

Version:

Fast and boilerplate-free precondition checks for javascript.

43 lines (42 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MatchesAssertion = void 0; var model_1 = require("../../model"); var NoDsl_1 = require("../../NoDsl"); /** * @author Maciej Chałapuk (maciej@chalapuk.pl) */ var MatchesAssertion = /** @class */ (function () { function MatchesAssertion(regexp) { this.regexp = regexp; } MatchesAssertion.prototype.assert = function (varName, testedValue) { var regexp = this.regexp; function flags() { return "".concat(regexp.global ? 'g' : '').concat(regexp.ignoreCase ? 'i' : '').concat(regexp.multiline ? 'm' : ''); } return { get success() { return typeof testedValue === 'string' && testedValue.match(regexp) !== null; }, get message() { return new model_1.StandardMessage(varName, "match /".concat(regexp.source, "/").concat(flags()), testedValue); }, }; }; return MatchesAssertion; }()); exports.MatchesAssertion = MatchesAssertion; (function (MatchesAssertion) { /** * @author Maciej Chałapuk (maciej@chalapuk.pl) */ function factory(args) { NoDsl_1.nodslArguments.check(args.length === 1, '.matches requires 1 argument (got ', args.length, ')'); NoDsl_1.nodslArguments.check(args[0] instanceof RegExp, 'regexp must be isntance of RegExp (got ', (typeof args[0]), ')'); return new MatchesAssertion(args[0]); } MatchesAssertion.factory = factory; })(MatchesAssertion = exports.MatchesAssertion || (exports.MatchesAssertion = {})); exports.MatchesAssertion = MatchesAssertion; exports.default = MatchesAssertion;