Last active
November 9, 2017 01:12
-
-
Save apkostka/a42b2f23df033872ae406549ab1a1c2e to your computer and use it in GitHub Desktop.
Angular 2 pipe to transform string to Title Case
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 '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