Last active
September 19, 2017 05:07
-
-
Save MMohan1/7fe4bc7299cd6de843cf56033e8ca022 to your computer and use it in GitHub Desktop.
Title case with filtering Prepositions
This file contains hidden or 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'; | |
/* | |
* Changes the case of the first letter of a world by avoiding the prepositions. | |
*/ | |
@Pipe({name: 'titlecase'}) | |
export class TitleCase implements PipeTransform { | |
transform(input:string):string { | |
let words = input.split(" "); | |
for (var i=0 ; i<words.length; i++){ | |
let word = words[i]; | |
if (this.isPrepositions(word) && i !=0 ){ | |
words[i] = word.toLowerCase() + " " | |
} | |
else { | |
words[i] = this.makeCamelCase(word) | |
} | |
} | |
return words.join(" ") | |
} | |
private isPrepositions(word:string):boolean { | |
let prepositions = ["of", | |
"the", | |
"is" | |
]; | |
return prepositions.includes(word.toLowerCase()); | |
} | |
private makeCamelCase(word:string):string{ | |
return word[0].toUpperCase() + word.substr(1).toLowerCase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment