( text: string, keywords: string[] | undefined, )
| 76 | * Returns null when no keyword matches. |
| 77 | */ |
| 78 | export const findFirstHighlightedKeyword = ( |
| 79 | text: string, |
| 80 | keywords: string[] | undefined, |
| 81 | ): KeywordMatch | null => { |
| 82 | if (!keywords?.length || !text) { |
| 83 | return null; |
| 84 | } |
| 85 | |
| 86 | const pattern = keywords.map(escapeRegex).join('|'); |
| 87 | const regex = new RegExp(`\\b(?:${pattern})\\b`, 'i'); |
| 88 | const match = text.match(regex); |
| 89 | |
| 90 | if (!match || match.index === undefined) { |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | return { |
| 95 | keyword: match[0], |
| 96 | start: match.index, |
| 97 | end: match.index + match[0].length, |
| 98 | }; |
| 99 | }; |
no outgoing calls
no test coverage detected