Skip to content

Instantly share code, notes, and snippets.

@whusterj
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whusterj/a74e74ad7d7a16969f08 to your computer and use it in GitHub Desktop.
Save whusterj/a74e74ad7d7a16969f08 to your computer and use it in GitHub Desktop.
A custom AngularJS Filter for printing a human-readable list.
/**
* A custom AngularJS Filter for printing a human-readable list.
*
* Example: Use in a controller:
*
* $scope.daysOfTheWeek = ['Monday', 'Tuesday', 'Wednesday' ]
*
* Example: Use in a template:
*
* {{ daysOfTheWeek | prettyList }}
*
* Output: "Monday, Tuesday, and Wednesday"
*
*/
(function () {
angular
.module('customFilters', [])
.filter('prettyList', prettyListFilter);
function prettyListFilter () {
return function (inputList, finalJoin) {
if (inputList.length == 1) { return inputList[0]; }
finalJoin = finalJoin || ' and ';
if (inputList.length == 2) { return inputList[0] + finalJoin + inputList[1]; }
var join = ', ',
arr = inputList.slice(0),
last = arr.pop();
return arr.join(join) + ',' + finalJoin + last;
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment