Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Pipe, PipeTransform } from '@angular/core';
- @Pipe({
- name: 'titlecase'
- })
- export class TitlecasePipe implements PipeTransform {
- transform(value: any): any {
- if (!value) {
- return null;
- }
- // short list
- const prepositions = [
- 'a', 'an', 'and', 'as', 'at', 'but', 'by', 'en', 'for', 'if', 'in', 'nor', 'of', 'on', 'or', 'per', 'the', 'to', 'via'
- ];
- const words = value.split(' ');
- for (let i = 0; i < words.length; i++) {
- if (i > 0 && prepositions.includes(words[i].toLowerCase())) {
- words[i] = words[i].toLowerCase();
- } else {
- words[i] = words[i].substr(0, 1).toUpperCase() + words[i].substr(1).toLowerCase();
- }
- }
- return words.join(' ');
- }
- }
Add Comment
Please, Sign In to add comment