Created
September 30, 2013 17:41
-
-
Save blangenfeld/6767372 to your computer and use it in GitHub Desktop.
AngularJS directives for formatting arrays of dates.
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
angular.module('kf.filters', []). | |
/** | |
* @ngdoc filter | |
* @name kf.filters:dates | |
* @function | |
* | |
* @description | |
* Passed an Array of Dates, returns a String joining them together with a delimiter. The dates | |
* themselves may be customized with an Angular `date` filter specifier. | |
* | |
* @param {Array} input The source Array of Dates. | |
* @param {string=} format Formatting rules (see Angular's `date` filter). | |
* @param {string=} delimiter Delimiter separating the dates. If not specified, ` - ` is used. | |
*/ | |
filter('dates', function($filter) { | |
return function(input, format, delimiter) { | |
delimiter = delimiter || ', '; | |
return input.map(function(date) { return $filter('date')(date, format); }).join(delimiter); | |
}; | |
}). | |
/** | |
* @ngdoc filter | |
* @name kf.filters:dateRange | |
* @function | |
* | |
* @description | |
* Passed an Array of Dates, returns a String joining the first and last Dates with a delimiter. | |
* The dates themselves may be customized with an Angular `date` filter specifier. | |
* | |
* @param {Array} input The source Array of Dates. | |
* @param {string=} format Formatting rules (see Angular's `date` filter). | |
* @param {string=} delimiter Delimiter separating the two dates. If not specified, ` - ` is used. | |
*/ | |
filter('dateRange', function($filter) { | |
return function(input, format, delimiter) { | |
delimiter = delimiter || ' - '; | |
var ends = [], last = input.length - 1; | |
if(last >= 0) { ends.push(input[0]); } | |
if(last >= 1) { ends.push(input[last]); } | |
return $filter('dates')(ends, format, delimiter); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo fiddle: http://jsfiddle.net/blangenfeld/vdNz2/