Skip to content

Instantly share code, notes, and snippets.

@bezael
Created October 27, 2018 08:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bezael/5cdaa55e439cf1dcc6edfe51fb1af182 to your computer and use it in GitHub Desktop.
Save bezael/5cdaa55e439cf1dcc6edfe51fb1af182 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;
}
}
@ShubhamSahoo456
Copy link

ksbnjnklsmnlksdnksjnkjsnckjsnbkjsbskjdbksjbkfsj

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