Created
July 11, 2017 04:05
-
-
Save tnqsoft/8eec973a2fa03c1a88443c8d4d7b6d96 to your computer and use it in GitHub Desktop.
Angular 4 Long file name
This file contains 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'; | |
/* | |
* Format long file name | |
* Max length, default is 100 | |
* Usage: | |
* filename | longFileName:maxLength | |
* Example: | |
* {{ 'demo file name long at here, this is long file name omg please try input now.txt' | longFileName:40}} | |
* formats to: demo file name long ...se try input now.txt | |
*/ | |
@Pipe({ | |
name: 'longFileName' | |
}) | |
export class LongFileNamePipe implements PipeTransform { | |
public transform(filename: string, maxLength: number = 100): any { | |
if (filename === null) { | |
return filename; | |
} | |
let midLength = (maxLength / 2).toString(); | |
let pattern = '^([a-zA-Z\\s,\\-\\_\\.]{' + midLength + '})(.*)([a-zA-Z\\s,\\-\\_\\.]{' + midLength + '})$'; | |
let regex = new RegExp(pattern); | |
return filename.replace(regex, '$1...$3'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment