benjamincharity
6/26/2015 - 3:02 PM

A filter that turns an array of integers into weekday names.

A filter that turns an array of integers into weekday names.

angular.module('myModule')
.filter('weekdays', function() {
    'use strict';

    return function(day, length) {

        // If length exists but doesn't match our keywords
        if( length && length !== 'long' && length !== 'short' && length !== 'letter' ) {
            return 'Length must be \'long\', \'short\' or \'letter\'';
        }

        var weekdays = {};
        weekdays.long = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
        weekdays.short = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
        weekdays.letter = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];

        if( length === 'long' ) {
            return weekdays.long[day];
        }

        if( length === 'short' || !length ) {
            return weekdays.short[day];
        }

        if( length === 'letter' ) {
            return weekdays.letter[day];
        }

    };

});