From 3ba8ee003327c474fbb11613dec950d278c27253 Mon Sep 17 00:00:00 2001 From: kaw67872 <kawtar.laariche@iais.fraunhofer.de> Date: Mon, 13 May 2024 09:46:51 +0200 Subject: [PATCH] #19: add truncate pipe --- src/app/shared/pipes/truncate.pipe.spec.ts | 8 ++++++++ src/app/shared/pipes/truncate.pipe.ts | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/app/shared/pipes/truncate.pipe.spec.ts create mode 100644 src/app/shared/pipes/truncate.pipe.ts diff --git a/src/app/shared/pipes/truncate.pipe.spec.ts b/src/app/shared/pipes/truncate.pipe.spec.ts new file mode 100644 index 0000000..b16f3ef --- /dev/null +++ b/src/app/shared/pipes/truncate.pipe.spec.ts @@ -0,0 +1,8 @@ +import { TruncatePipe } from './truncate.pipe'; + +describe('TruncatePipe', () => { + it('create an instance', () => { + const pipe = new TruncatePipe(); + expect(pipe).toBeTruthy(); + }); +}); diff --git a/src/app/shared/pipes/truncate.pipe.ts b/src/app/shared/pipes/truncate.pipe.ts new file mode 100644 index 0000000..b03e5e6 --- /dev/null +++ b/src/app/shared/pipes/truncate.pipe.ts @@ -0,0 +1,19 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'truncate', + standalone: true, +}) +export class TruncatePipe implements PipeTransform { + transform( + value: string, + limit = 25, + completeWords = false, + ellipsis = '...', + ) { + if (completeWords) { + limit = value.slice(0, limit).lastIndexOf(' '); + } + return value.length > limit ? value.slice(0, limit) + ellipsis : value; + } +} -- GitLab