parent
b2c05f1440
commit
8e49b63a99
@ -0,0 +1,8 @@
|
||||
import { TruncatePipe } from './truncate.pipe';
|
||||
|
||||
describe('TruncatePipe', () => {
|
||||
it('create an instance', () => {
|
||||
const pipe = new TruncatePipe();
|
||||
expect(pipe).toBeTruthy();
|
||||
});
|
||||
});
|
@ -0,0 +1,20 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({
|
||||
name: 'truncate',
|
||||
standalone: true,
|
||||
})
|
||||
export class TruncatePipe implements PipeTransform {
|
||||
|
||||
transform(value: string, limit: number = 50, completeWords: boolean = false, ellipsis: string = '...'): string {
|
||||
if (!value) return '';
|
||||
if (value.length <= limit) return value;
|
||||
|
||||
if (completeWords) {
|
||||
limit = value.substring(0, limit).lastIndexOf(' ');
|
||||
}
|
||||
return `${value.substring(0, limit)}${ellipsis}`;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in new issue