forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtruncate-breadcrumb-item-characters.pipe.ts
More file actions
32 lines (29 loc) · 1.09 KB
/
truncate-breadcrumb-item-characters.pipe.ts
File metadata and controls
32 lines (29 loc) · 1.09 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
import { Pipe, PipeTransform } from '@angular/core';
import { environment } from '../../../environments/environment';
import { hasValue } from '../../shared/empty.util';
@Pipe({
name: 'dsTruncateText',
})
export class TruncateBreadcrumbItemCharactersPipe implements PipeTransform {
/**
* The maximum number of characters to display in a breadcrumb item
* @type {number}
*/
readonly charLimit: number = environment.layout.breadcrumbs.charLimit;
/**
* Truncates the text based on the configured char number allowed per breadcrumb element.
* If text is shorter than the number of chars allowed, it will return the text as it is.
* If text is longer than the number of chars allowed, it will return the text truncated with an ellipsis at the end.
* @param text Traslated text to be truncated
*/
transform(text: string): string {
if (this.isTruncatable(text)) {
return text.substring(0, this.charLimit).concat('...');
} else {
return text;
}
}
protected isTruncatable(text: string): boolean {
return hasValue(text) && text.length > this.charLimit;
}
}