Created
April 7, 2021 15:22
-
-
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.
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 '@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