UNPKG

@nickbusey/thelounge

Version:

The self-hosted Web IRC client

47 lines (46 loc) 2.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // Escapes the RegExp special characters "^", "$", "", ".", "*", "+", "?", "(", // ")", "[", "]", "{", "}", and "|" in string. // See https://lodash.com/docs/#escapeRegExp const escapeRegExp_1 = __importDefault(require("lodash/escapeRegExp")); // escapes a regex in a way that's compatible to shove it in // a regex char set (meaning it also escapes -) function escapeRegExpCharSet(raw) { const escaped = (0, escapeRegExp_1.default)(raw); return escaped.replace("-", "\\-"); } // Given an array of channel prefixes (such as "#" and "&") and an array of user // modes (such as "@" and "+"), this function extracts channels and nicks from a // text. // It returns an array of objects for each channel found with their start index, // end index and channel name. function findChannels(text, channelPrefixes, userModes) { // `userModePattern` is necessary to ignore user modes in /whois responses. // For example, a voiced user in #thelounge will have a /whois response of: // > foo is on the following channels: +#thelounge // We need to explicitly ignore user modes to parse such channels correctly. const userModePattern = userModes.map(escapeRegExpCharSet).join(""); const channelPrefixPattern = channelPrefixes.map(escapeRegExpCharSet).join(""); const channelPattern = `(?:^|\\s)[${userModePattern}]*([${channelPrefixPattern}][^ \u0007]+)`; const channelRegExp = new RegExp(channelPattern, "g"); const result = []; let match; do { // With global ("g") regexes, calling `exec` multiple times will find // successive matches in the same string. match = channelRegExp.exec(text); if (match) { result.push({ start: match.index + match[0].length - match[1].length, end: match.index + match[0].length, channel: match[1], }); } } while (match); return result; } exports.default = findChannels;