forked from jebaGem/AngularRedux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter.bold.ts
31 lines (29 loc) · 919 Bytes
/
letter.bold.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'letterBold' })
export class LetterBoldPipe implements PipeTransform {
transform(value: any, search: any): any {
if (!search) { return value; }
const searchLength = search.length;
const holder = value.split('');
let indexAdder = 0;
let indexs = this.searchSubString(search.toLowerCase(), value.toLowerCase());
indexs = indexs.map((x) => {
const solution = x + indexAdder;
indexAdder += 2;
return solution;
});
indexs.forEach((i) => {
holder.splice(i, 0, '<span>');
holder.splice(i + searchLength + 1, 0, '</span>');
});
return holder.join('');
}
searchSubString(substring, searchValue) {
const indexs = [];
let i = -1;
while ((i = searchValue.indexOf(substring, i + 1)) >= 0) {
indexs.push(i);
}
return indexs;
}
}