]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/search-highlight.pipe.ts
import ceph quincy 17.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / pipes / search-highlight.pipe.ts
1 import { Pipe, PipeTransform } from '@angular/core';
2
3 @Pipe({
4 name: 'searchHighlight'
5 })
6 export class SearchHighlightPipe implements PipeTransform {
7 transform(value: string, args: string): string {
8 if (!args) {
9 return value;
10 }
11 args = this.escapeRegExp(args);
12 const regex = new RegExp(args, 'gi');
13 const match = value.match(regex);
14
15 if (!match) {
16 return value;
17 }
18
19 return value.replace(regex, '<mark>$&</mark>');
20 }
21
22 private escapeRegExp(str: string) {
23 // $& means the whole matched string
24 return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
25 }
26 }