-
-
Save guibot17/6e1e85975a0060d8c29ceabfaebf0cfe to your computer and use it in GitHub Desktop.
Angular 2 pipe to transform string to Title Case
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 'angular2/core'; | |
/* | |
* Changes the case of the first letter of a given number of words in a string. | |
*/ | |
@Pipe({name: 'titleCase', pure: false}) | |
export class TitleCase implements PipeTransform { | |
transform(input:string, length: number): string{ | |
return input.length > 0 ? input.replace(/\w\S*/g, (txt => txt[0].toUpperCase() + txt.substr(1).toLowerCase() )) : ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment