-
-
Save touilfarouk/fe6340b75c5f78bd4d2d2bed50c546e5 to your computer and use it in GitHub Desktop.
Truncate text by Custom pipe. Angular 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks but i can use Slice Pipe!