Skip to content

Instantly share code, notes, and snippets.

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 juanAFernandez/0c82e870f11421586b7b7fe1ada79761 to your computer and use it in GitHub Desktop.
Save juanAFernandez/0c82e870f11421586b7b7fe1ada79761 to your computer and use it in GitHub Desktop.
Titlelize, a simple angular pipe to capitalize first letter of first word of a phrase.
import { Pipe, PipeTransform } from '@angular/core';
/**
* Used to capitalize the first word of a phrase. The rest of words keeps equal.
* Example: {{ 'hello world' | titlelize }} will render 'Hello world'
* Example: {{ 'HELLO world' | titlelize }} also will render 'Hello world'
*/
@Pipe({
name: 'titlelize'
})
export class TitlelizePipe implements PipeTransform {
constructor() {
}
transform(phrase: string): string {
let words =phrase.split(' ');
words[0] = words[0][0].toUpperCase() + words[0].substr(1).toLowerCase();
return words.join(' ');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment