From 5409525ea2ed69c15998724015d5e1247d3a4a48 Mon Sep 17 00:00:00 2001 From: Robyn MacCallum Date: Mon, 21 Mar 2022 15:46:54 -0400 Subject: [PATCH] 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 --- angular/src/pipes/ellipsis.pipe.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 angular/src/pipes/ellipsis.pipe.ts diff --git a/angular/src/pipes/ellipsis.pipe.ts b/angular/src/pipes/ellipsis.pipe.ts new file mode 100644 index 0000000000..081dba11ab --- /dev/null +++ b/angular/src/pipes/ellipsis.pipe.ts @@ -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; + } +}