File

app/shared/pipes/truncate-text.pipe.ts

Metadata

Name truncatetext

Methods

transform
transform(value: string, length: number)
Parameters :
Name Type Optional
value string No
length number No
Returns : string
import { Pipe, PipeTransform } from '@angular/core';


@Pipe({
	name: 'truncatetext'
})

export class TruncateTextPipe implements PipeTransform {

	transform(value: string, length: number): string {
		const biggestWord = 50;
		const elipses = '...';

		if (!value || value.length <= length) {
			return value;
		}

		let truncatedText = value.slice(0, length + biggestWord);

		while (truncatedText.length > length - elipses.length) {

			let lastSpace = truncatedText.lastIndexOf(' ');

			if (lastSpace === -1) {
				break;
			}

			truncatedText = truncatedText.slice(0, lastSpace).replace(/[!,.?;:]$/, '');

		}

		return truncatedText + elipses;
	}
}

result-matching ""

    No results matching ""