UNPKG

markdown-proofing

Version:

A markdown proofing platform for individuals, teams, and organizations.

105 lines (81 loc) 3.88 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _validator = require('./validator'); var _validator2 = _interopRequireDefault(_validator); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Rule = function () { function Rule(messageType, condition) { _classCallCheck(this, Rule); this.supportedConditionPrefixes = ['error', 'info', 'warning']; this.supportedOperators = ['<', '<=', '>', '>=', '==']; _validator2.default.ensureValidMessageType(messageType); if (!this.supportedConditionPrefixes.some(function (x) { return condition.startsWith(x); })) { throw new Error('Unsupported configuration condition: \'' + condition + '\''); } this.messageType = messageType; this.condition = condition; } _createClass(Rule, [{ key: '_getOperator', value: function _getOperator() { var _this = this; // Order comparisons by length of text strings descending var orderedSupportedOperators = this.supportedOperators.slice().sort(function (a, b) { return b.length - a.length; }); var output = void 0; orderedSupportedOperators.forEach(function (x) { if (!output && _this.condition.includes(x)) { output = x; } }); return output; } }, { key: '_getComparisonValue', value: function _getComparisonValue() { var matches = this.condition.match(/\d+$/gi); var comparisonValue = Number(matches[matches.length - 1]); return comparisonValue; } }, { key: 'matchesCondition', value: function matchesCondition(analyzerMessage) { var _this2 = this; // If this rule is a numeric rule // use the operator and comparison value // during the matchesCondition check. if (this.supportedOperators.some(function (x) { return _this2.condition.includes(x); })) { var operator = this._getOperator(); if (!operator) { throw new Error('Encountered an unexpected problem while parsing numeric condition operator from: ' + this.condition); } var comparisonValue = this._getComparisonValue(); // `0` is a valid `comparisonValue` value if (comparisonValue === undefined || comparisonValue === null) { // eslint-disable-line no-undefined throw new Error('Encountered an unexpected problem while parsing numeric condition value from: ' + this.condition); } return analyzerMessage.type === this.messageType && eval(analyzerMessage.text + ' ' + operator + ' ' + comparisonValue); // eslint-disable-line no-eval } else if (!this.supportedConditionPrefixes.some(function (x) { return x === _this2.condition; })) { // If non-numeric comparison, ensure matches // one of the valid message types exactly. throw new Error('Invalid condition specified: ' + this.condition); } return analyzerMessage.type === this.messageType; } }]); return Rule; }(); exports.default = Rule; module.exports = exports['default'];