developer033
developer033

Reputation: 24864

Capitalize initial letter with AngularJS

I know there's a filter in AngularJS to uppercase, but I want to make a function to capitalize only the initial letters of a full name, but with some restrictions. Prepositions ("da, de, di, do, du, das, des, dis, dos, dus") should be lower case.

Ex:

Is there a efficient way to do this?

Upvotes: 2

Views: 1796

Answers (1)

tymeJV
tymeJV

Reputation: 104775

You can make a custom filter for this:

.filter("name", function() {
    var ignoreWords = ["da", "de", "di", "do", "du", "das", "des", "dis", "dos", "dus"]
    return function(name) {
      return name.split(" ").map(function(word) {
        if (ignoreWords.indexOf(word.toLowerCase()) > -1) {
          return word[0].toLowerCase() + word.substr(1);
        } else {
          return word[0].toUpperCase() + word.substr(1);
        }
    }).join(" ")
  }
});

Demo: http://plnkr.co/edit/Xw1vrASJEP4VU53HFs8v?p=preview

Upvotes: 3

Related Questions