Skip to content

Instantly share code, notes, and snippets.

@marufsiddiqui
Created May 22, 2013 07:34
Show Gist options
  • Save marufsiddiqui/5625869 to your computer and use it in GitHub Desktop.
Save marufsiddiqui/5625869 to your computer and use it in GitHub Desktop.
A Sample AngularJS filter
var homeModule = angular.module('HomeModule', []);
homeModule.filter('titleCase', function () {
return function (input) {
var words = input.split(' ');
for (var i = 0; i < words.length; i++) {
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
return words.join(' ');
}
});
@i8ramin
Copy link

i8ramin commented Oct 31, 2013

This is great .. but doesn't work when all the words are in UPPERCASE or have weird casing. Made a slight adjustment to handle those cases.

app.filter('titlecase', function () {
  return function (input) {
    var words = input.split(' ');
    for (var i = 0; i < words.length; i++) {
      words[i] = words[i].toLowerCase(); // lowercase everything to get rid of weird casing issues
      words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
    }
    return words.join(' ');
  }
});

@Tg7z
Copy link

Tg7z commented Dec 3, 2013

And if you want true titlecase you need to keep short articles, conjunctions, and prepositions lowercase.
Credit where it's due I pinched the code from https://github.com/gouch/to-title-case and wrapped it up for Angular

app.filter('titlecase', function () {
    return function (input) {
      var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;

      return input.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
        if (index > 0 && index + match.length !== title.length &&
          match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
          (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
          title.charAt(index - 1).search(/[^\s-]/) < 0) {
          return match.toLowerCase();
        }

        if (match.substr(1).search(/[A-Z]|\../) > -1) {
          return match;
        }

        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    }
  })

@anton000
Copy link

@Autodidaktosaur doesnt seem to work on All caps

@benhoIIand
Copy link

Nice simple filter that works with all caps:

angular.module('app.filters')
    .filter('titleCase', function() {
        return function(str) {
            return (str == undefined || str === null) ? '' : str.replace(/_|-/, ' ').replace(/\w\S*/g, function(txt){
                return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
            });
        }
    });

Tests to go with it if you want them:

describe('titleCase', function() {

    var fixtures = [
        'AWAITING DISPATCH',
        'AWAITING_DISPATCH',
        'AWaiTing-DISPatcH',
        'Awaiting DISPATCH'
    ];

    beforeEach(module('app.filters'));

    it('should convert strings correctly', inject(function(titleCaseFilter) {

        fixtures.forEach(function(fixture) {
            expect(titleCaseFilter(fixture)).toEqual('Awaiting Dispatch');
        });

    }));

    it('should return an empty string when a value is not passed', inject(function(titleCaseFilter) {
        expect(titleCaseFilter()).toEqual('');
        expect(titleCaseFilter(null)).toEqual('');
    }));
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment