Skip to content

Instantly share code, notes, and snippets.

@touilfarouk
Forked from bezael/Truncate-text-pipe.ts
Created April 11, 2020 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save touilfarouk/fe6340b75c5f78bd4d2d2bed50c546e5 to your computer and use it in GitHub Desktop.
Save touilfarouk/fe6340b75c5f78bd4d2d2bed50c546e5 to your computer and use it in GitHub Desktop.
Truncate text by Custom pipe. Angular 6
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'truncateText'
})
export class TruncateTextPipe implements PipeTransform {
transform(value: string, limit: number = 40, trail: String = '…'): string {
let result = value || '';
if (value) {
const words = value.split(/\s+/);
if (words.length > Math.abs(limit)) {
if (limit < 0) {
limit *= -1;
result =
trail + words.slice(words.length - limit, words.length).join(' ');
} else {
result = words.slice(0, limit).join(' ') + trail;
}
}
}
return result;
}
}
@jdmasoft
Copy link

jdmasoft commented Sep 2, 2020

Thanks but i can use Slice Pipe!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment