Add ellipsis pipe (#728)

* add ellipsis pipe

* run prettier

* Account for ellipsis length in returned string

* Fix complete words case

* Fix another complete words issue

* fix for if there are not spaces in long value

* extract length check to beginning of method

* condense if statements

* remove log
This commit is contained in:
Robyn MacCallum 2022-03-21 15:46:54 -04:00 committed by GitHub
parent 9950fb42a1
commit 5409525ea2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: "ellipsis",
})
export class EllipsisPipe implements PipeTransform {
transform(value: string, limit = 25, completeWords = false, ellipsis = "...") {
if (value.length <= limit) {
return value;
}
limit -= ellipsis.length;
if (completeWords && value.length > limit && value.indexOf(" ") > 0) {
limit = value.substring(0, limit).lastIndexOf(" ");
}
return value.substring(0, limit) + ellipsis;
}
}