Skip to content

Instantly share code, notes, and snippets.

@guibot17
Forked from apkostka/titlecase.pipe.ts
Created July 11, 2017 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guibot17/6e1e85975a0060d8c29ceabfaebf0cfe to your computer and use it in GitHub Desktop.
Save guibot17/6e1e85975a0060d8c29ceabfaebf0cfe 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