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