pattern-rule-matcher.js 479 Bytes
"use strict";

function getPatternMatchFromMessage(msg, rules) {
    if (!rules || !Array.isArray(rules)) {
        return;
    }

    for (let rule of rules) {
        if (!rule.pattern) return;

        const re = new RegExp(rule.pattern, rule.flags);
        const matches = msg.match(re);

        if (matches && matches.length >= 2) {
            return (rule.prefix || '') + matches[1] + (rule.suffix || '');
        }
    }
}

module.exports = getPatternMatchFromMessage;