stylize.pipe.ts
1.2 KB
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
32
33
34
35
36
37
38
39
40
41
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeStyle } from '@angular/platform-browser';
interface Sentiment {
score: number,
magnitude: number
}
/*
* Styles a message based on its sentiment.
*/
@Pipe({name: 'stylize'})
export class StylizePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}
transform(sentiment: Sentiment): string|SafeStyle {
if (!sentiment) {
return '';
}
let style = '';
// Change font based on positive/negative score.
if (sentiment.score >= 0.9) {
style += `font-family: 'Bonbon', 'Roboto', 'Helvetica', sans-serif;`;
} else if (sentiment.score >= 0.5) {
style += `font-family: 'Crafty Girls', 'Roboto', 'Helvetica', sans-serif;`;
} else if (sentiment.score <= -0.9) {
style += `font-family: 'Creepster', 'Roboto', 'Helvetica', sans-serif;`;
} else if (sentiment.score <= -0.5) {
style += `font-family: 'Julee', 'Roboto', 'Helvetica', sans-serif;`;
}
// Make bold if the magnitude is greater than 1.
if (sentiment.magnitude >= 1) {
style += `font-weight: bold;`;
}
return style ? this.sanitizer.bypassSecurityTrustStyle(style) : '';
}
}