Skip to content

Instantly share code, notes, and snippets.

@MMohan1
Last active September 19, 2017 05:07
Show Gist options
  • Save MMohan1/7fe4bc7299cd6de843cf56033e8ca022 to your computer and use it in GitHub Desktop.
Save MMohan1/7fe4bc7299cd6de843cf56033e8ca022 to your computer and use it in GitHub Desktop.
Title case with filtering Prepositions
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