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