Skip to content

Instantly share code, notes, and snippets.

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