utility.filter.js 4.06 KB
(function(angular){
    var module = angular.module('framework.filters.utility',[]);

    module.filter('RemoveSpaces', removeSpaces);
    module.filter('Truncate', truncate);
    module.filter('AbbrValue', abbrValue);
    module.filter('Countdown', countdown);


    function removeSpaces() {
        return function (text) {
            return text.replace(/\s/g, '');
        }
    }

    function truncate () {
        return function (text, length, end) {
            if (isNaN(length))
                length = 10;
            if (end === undefined)
                end = "...";
            if (text.length <= length || text.length - end.length <= length)
            { return text; }
            else
            { return String(text).substring(0, length - end.length) + end; }
        }
    }

    function abbrValue() {
        return function (val) {

        return Math.abs(Number(val)) >= 1.0e+9

           ? Math.round(Math.abs((Number(val)) / 1.0e+9)*10)/10 + "b"
           // Six Zeroes for Millions 
           : Math.abs(Number(val)) >= 1.0e+6

           ? Math.round(Math.abs((Number(val)) / 1.0e+6)*10)/10 + "m"
           // Three Zeroes for Thousands
           : Math.abs(Number(val)) >= 1.0e+3

           ? Math.round(Math.abs((Number(val)) / 1.0e+3)*10)/10 + "k"

           : Math.abs(Number(val));
        }
    }

    function countdown() {
        return function (input) {
            var substitute = function (stringOrFunction, number, strings) {
                var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, dateDifference) : stringOrFunction;
                var value = (strings.numbers && strings.numbers[number]) || number;
                return string.replace(/%d/i, value);
            },
                nowTime = (new Date()).getTime(),
                date = (new Date(input)).getTime(),
                allowFuture = true,
                strings = {
                    prefixAgo: null,
                    prefixFromNow: null,
                    suffixAgo: "ago",
                    suffixFromNow: "",//"from now"
                    seconds: "less than a minute",
                    minute: "a minute",
                    minutes: "%d minutes",
                    hour: "an hour",
                    hours: "%d hours",
                    day: "a day",
                    days: "%d days",
                    month: "a month",
                    months: "%d months",
                    year: "a year",
                    years: "%d years"
                },
                dateDifference = nowTime - date,
                words,
                seconds = Math.abs(dateDifference) / 1000,
                minutes = seconds / 60,
                hours = minutes / 60,
                days = hours / 24,
                years = days / 365,
                separator = strings.wordSeparator === undefined ? " " : strings.wordSeparator,
                prefix = strings.prefixAgo,
                suffix = strings.suffixAgo;

            if (allowFuture) {
                if (dateDifference < 0) {
                    prefix = strings.prefixFromNow;
                    suffix = strings.suffixFromNow;
                }
            }

            words = seconds < 45 && substitute(strings.seconds, Math.round(seconds), strings) ||
            seconds < 90 && substitute(strings.minute, 1, strings) ||
            minutes < 45 && substitute(strings.minutes, Math.round(minutes), strings) ||
            minutes < 90 && substitute(strings.hour, 1, strings) ||
            hours < 24 && substitute(strings.hours, Math.round(hours), strings) ||
            hours < 42 && substitute(strings.day, 1, strings) ||
            days < 30 && substitute(strings.days, Math.round(days), strings) ||
            days < 45 && substitute(strings.month, 1, strings) ||
            days < 365 && substitute(strings.months, Math.round(days / 30), strings) ||
            years < 1.5 && substitute(strings.year, 1, strings) ||
            substitute(strings.years, Math.round(years), strings);

            return $.trim([prefix, words, suffix].join(separator));
        }
    }


})(angular);