/* global angular */

angular.module("app").filter('zeroFillNumber', function () {
    return function (n, len) {
        var num = parseInt(n, 10);
        len = parseInt(len, 10);
        if (isNaN(num) || isNaN(len)) {
            return n;
        }
        num = '' + num;
        while (num.length < len) {
            num = '0' + num;
        }
        return num;
    };
});
angular.module("app").filter('phone', function () {
    return function (number) {
        if (isNaN(number) || (number.length !== 8 && number.length !== 10)) {
            return number;
        }
        var phone_number = "";
        var array_number;
        if (number.length === 10) {
            phone_number += "(" + number.substring(0, 2) + ") ";
            array_number = number.substring(2).split("");
        } else {
            array_number = number.split("");
        }
        for (var i = 0; i < array_number.length; i++) {
            phone_number += array_number[i];
            if (i === 3) {
                phone_number += " - ";
            }
        }
        return phone_number;
    };
});
angular.module("app").filter('capitalize', function () {
    return function (text) {
        if (text === undefined || text === null || !isNaN(text) || text === "") {
            return text;
        }
        const array_text = text.split(" ");
        for (var i = 0; i < array_text.length; i++) {
            array_text[i] = array_text[i].charAt(0).toUpperCase() + array_text[i].slice(1).toLowerCase();

        }
        return array_text.join(" ");
    };
});
angular.module("app").filter('reverse', function () {
    return function (items) {
        return items.slice().reverse();
    };
});