Guest User

Untitled

a guest
Oct 19th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import { Pipe, PipeTransform } from '@angular/core';
  2.  
  3. @Pipe({
  4. name: 'titlecase'
  5. })
  6. export class TitlecasePipe implements PipeTransform {
  7.  
  8. transform(value: any): any {
  9. if (!value) {
  10. return null;
  11. }
  12. // short list
  13. const prepositions = [
  14. 'a', 'an', 'and', 'as', 'at', 'but', 'by', 'en', 'for', 'if', 'in', 'nor', 'of', 'on', 'or', 'per', 'the', 'to', 'via'
  15. ];
  16.  
  17. const words = value.split(' ');
  18.  
  19. for (let i = 0; i < words.length; i++) {
  20. if (i > 0 && prepositions.includes(words[i].toLowerCase())) {
  21. words[i] = words[i].toLowerCase();
  22. } else {
  23. words[i] = words[i].substr(0, 1).toUpperCase() + words[i].substr(1).toLowerCase();
  24. }
  25. }
  26. return words.join(' ');
  27. }
  28. }
Add Comment
Please, Sign In to add comment