Last active
August 29, 2015 14:07
-
-
Save whusterj/a74e74ad7d7a16969f08 to your computer and use it in GitHub Desktop.
A custom AngularJS Filter for printing a human-readable list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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