Created
October 27, 2018 08:30
-
-
Save bezael/5cdaa55e439cf1dcc6edfe51fb1af182 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
ksbnjnklsmnlksdnksjnkjsnckjsnbkjsbskjdbksjbkfsj