angularjs - Angular filter to initialise first name -


i need make angular filter take full names, , initialise first name, leaving last name, except in cases (e.g. van aanholt).

so following:

yohan cabaye  oscar  jordi alba ramos  patrick van aanholt hatem ben arfa 

will shown as:

y. cabaye oscar j. ramos p. van aanholt h. ben arfa 

any appreciated.


update: i've tried...

    angular.module('eurofilters', [])                .filter('initialisename', function() {                          return function(name) {  var namearr = name.split(' ');  var firstname = namearr[0]; var firstnameinitial = namearr[0][0] + ". ";  var lastname = namearr[namearr.length - 1];  var secondarylastname = namearr[namearr.length - 2];  // if 1 name (standard)...  if (namearr.length <= 1) {   return firstname; }  // if more 1 name, , contains "van" or "ben" (exception)... else if (secondarylastname === "van" || secondarylastname === "ben") {   return firstnameinitial + " " + secondarylastname + " " + lastname; }  // if more 1 name (standard)... else if (namearr.length > 1) {   return firstnameinitial + lastname; }  };  }); 

is there more elegant way write this? how convert switch statement, example?

you can use split , join that. string array objects, can use index on them.

angular.module('app', []);    angular.module('app').filter('initialisefirstname', function () {    return function (name) {      var parts = name.split(/\s+/);            if (parts.length <= 1) {        return name;      }            var first = parts[0][0] + '.';            return first + ' ' + parts.slice(1).join(' ');    }  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  <div ng-app="app">    {{'john ket'|initialisefirstname}}  </div>


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -