starter.filter("format", function () {
    return function (input) {
        var args = arguments;
        return input.replace(/\{(\d+)\}/g, function (match, capture) {
            return args[1 * capture + 1];
        });
    };
})

.filter('getDateDiff', function () {
    return function (dateTimeStamp) {
        if (dateTimeStamp == null) {
            return null;
        }

        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var week = day * 7;
        var month = day * 30;
        var year = day * 365;
        var now = new Date().getTime();
        var datetime = new Date(dateTimeStamp).getTime()
        var diffValue = now - datetime;

        if (diffValue + 10000 < 0) {
            //非法操作
            return false;
        }

        var yearC = diffValue / year;
        var monthC = diffValue / month;
        var weekC = diffValue / week;
        var dayC = diffValue / day;
        var hourC = diffValue / hour;
        var minC = diffValue / minute;

        if (yearC >= 1) {
            result = parseInt(yearC) + "年前";
        } else if (monthC >= 1) {
            result = parseInt(monthC) + "个月前";
        } else if (weekC >= 1) {
            result = parseInt(weekC) + "周前";
        } else if (dayC >= 1) {
            result = parseInt(dayC) + "天前";
        } else if (hourC >= 1) {
            result = parseInt(hourC) + "小时前";
        } else if (minC >= 1) {
            result = parseInt(minC) + "分钟前";
        } else
            result = "刚刚";
        return result;
    }
})

.filter('SwitchHtmlContent', function () {
    return function (content) {
        if (content != null) {
            content = content.replace(/\n/g, '<br/>');
            if (typeof ioNull != 'undefined') {
                return ioNull.emoji.parse(content);
            }
        }
        return content;
    }
})

.filter('percentage', function ($window) {
    return function (input, decimals, suffix) {
        decimals = angular.isNumber(decimals) ? decimals : 2;
        suffix = suffix || '%';
        if ($window.isNaN(input)) {
            return '';
        }
        return Math.round(input * Math.pow(10, decimals + 2)) / Math.pow(10, decimals) + suffix
    };
})

.filter('strtimeformat', function ($filter) {
    var dateFilter = $filter('date');

    function formatter(value) {
        return dateFilter(value, 'HH:mm'); //format
    }

    return function (str) {
        if (str == null)
            return null;
        str = new Date('2016/10/1 ' + str);
        return formatter(str);
    };
})
