hardlounge/client/js/helpers/ircmessageparser/findNames.ts

33 lines
605 B
TypeScript
Raw Normal View History

import {Part} from "./merge";
2017-11-14 22:36:45 +00:00
const nickRegExp = /([\w[\]\\`^{|}-]+)/g;
export type NamePart = Part & {
nick: string;
};
function findNames(text: string, nicks: string[]): NamePart[] {
const result: NamePart[] = [];
// Return early if we don't have any nicknames to find
if (nicks.length === 0) {
return result;
}
let match: RegExpExecArray | null;
while ((match = nickRegExp.exec(text))) {
if (nicks.indexOf(match[1]) > -1) {
result.push({
start: match.index,
end: match.index + match[1].length,
nick: match[1],
});
}
}
2017-11-14 22:36:45 +00:00
return result;
}
2019-11-16 17:24:03 +00:00
export default findNames;