import { Pipe, PipeTransform } from '@angular/core'; import { eStringPlaceholder } from '../../../enums'; // interfaces import { IComment } from '../models'; @Pipe({ name: 'caHighlightComment', standalone: true, }) export class CaHighlightCommentPipe implements PipeTransform { transform( comments: IComment[], searchValue: string, isDarkmode?: boolean ): IComment[] { if (!searchValue?.trim()) return comments; const regex = new RegExp( searchValue.trim(), eStringPlaceholder.GI_LOWERCASE ); return comments .filter( (item: IComment) => regex.test(item.commentContent) || regex.test( item?.companyUser?.fullName ?? eStringPlaceholder.EMPTY ) ) .map((item: IComment) => { if (!item.commentContent?.trim()) return item; const highlightedContent = item.commentContent.replace( regex, (match) => { const textColor: string = isDarkmode ? '#e9effd' : '#3074d3'; return `${match}`; } ); return { ...item, commentContent: highlightedContent, }; }); } }