mirror of
https://github.com/thelounge/thelounge
synced 2024-11-23 12:33:07 +00:00
dd05ee3a65
Co-authored-by: Eric Nemchik <eric@nemchik.com> Co-authored-by: Pavel Djundik <xPaw@users.noreply.github.com>
32 lines
605 B
TypeScript
32 lines
605 B
TypeScript
import {Part} from "./merge";
|
|
|
|
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],
|
|
});
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export default findNames;
|