<commit_before>/*
 * This file is part of Cockpit.
 *
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * Cockpit is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Cockpit is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
 */

var cockpit = cockpit || { };

// Used for escaping things in HTML elements and attributes
function cockpit_esc(str) {
    if (str === null || str === undefined)
        return "";
    var pre = document.createElement('pre');
    var text = document.createTextNode(str);
    pre.appendChild(text);
    return pre.innerHTML.replace(/"/g, "&quot;").replace(/'/g, "&#39;");
}

// Used for escaping things in HTML id attribute values
//
// http://www.w3.org/TR/html5/global-attributes.html#the-id-attribute
function cockpit_esc_id_attr(str) {
    return cockpit_esc(str).replace(/ /g, "&#20;").replace(/\x09/g, "&#09;").replace(/\x0a/g, "&#0a;").replace(/\x0c/g, "&#0c;").replace(/\x0d/g, "&#0d;");
}

function cockpit_debug(str) {
    console.debug("DEBUG: " + str);
}

/*
 * Byte formatting
 *
 * cockpit.format_bytes(number, [factor, separate])
 * @number: a normal number
 * @factor: optional, either 1000, 1024 or a string suffix
 * @separate: optional, when true return pieces in an array rather than string
 *
 * Formats bytes into a displayable string and suffix, such as
 * 'KB' or 'MB'. Returns an array of the formatted number and
 * the suffix if @separate is set.
 *
 * If specifying 1000 or 1024 these will be used as the factors
 * to choose an appropriate suffix. By default the factor is
 * 1024.
 *
 * You can pass the suffix into the second argument in which
 * case the number will be formatted with that specific suffix.
 *
 * If the number is less than the factor or an unknown suffix
 * was passed in, then the formatted number is returned without
 * a suffix.
 *
 * If @separate is true, returns an array of [formatted_number, suffix]
 * unless no suffix is returned.
 *
 * Examples:
 *    cockpit.format_bytes(1000000).join(" ");
 *    cockpit.format_bytes(1000000, 1024).join(" ");
 *    cockpit.format_bytes(1000000, "KB").join(" ");
 *
 * The current policy is to use KB, MB, GB, etc. for both factors
 * of 1000 and 1024.
 *
 *
 * cockpit.format_bytes_per_sec(number)
 * @number: the number to format
 *
 * Format bytes into a displayable speed string.
 *
 *
 * cockpit.format_delay(ms)
 * @ms: number of milli-seconds
 *
 * Format soconds into a string of "hours, minutes, seconds".
 */

(function(cockpit) {

var suffixes = {
    1024: [ null, "KB", "MB", "GB", "TB", "PB", "EB", "ZB" ],
    1000: [ null, "KB", "MB", "GB", "TB", "PB", "EB", "ZB" ]
    /* 1024: [ null, "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB" ] */
};

cockpit.format_bytes = function format_bytes(number, factor, separate) {
    var quotient;
    var suffix = null;

    if (factor === undefined)
        factor = 1024;

    /* Find that factor string */
    if (typeof (factor) === "string") {
        /* Prefer larger factors */
        var keys = [];
        for (var key in suffixes)
            keys.push(key);
        keys.sort().reverse();
        for (var y = 0; y < keys.length; y++) {
            for (var x = 0; x < suffixes[keys[y]].length; x++) {
                if (factor == suffixes[keys[y]][x]) {
                    number = number / Math.pow(keys[y], x);
                    suffix = factor;
                    break;
                }
            }
            if (suffix)
                break;
        }

    /* @factor is a number */
    } else if (factor in suffixes) {
        var divisor = 1;
        for (var i = 0; i < suffixes[factor].length; i++) {
            quotient = number / divisor;
            if (quotient < factor) {
                number = quotient;
                suffix = suffixes[factor][i];
                break;
            }
            divisor *= factor;
        }
    }

    var ret;

    if (!suffix) {
        ret = [number.toString()];
        if (!separate)
            ret = ret.join(" ");
        return ret;
    }

    /* non-zero values should never appear zero */
    if (number > 0 && number < 0.1)
        number = 0.1;

    /* TODO: Make the decimal separator translatable */
    if (number === 0)
        ret = [number.toString(), suffix];
    else
        ret = [number.toFixed(1), suffix];
    if (!separate)
        ret = ret.join(" ");
    return ret;
};

cockpit.format_bytes_per_sec = function format_bytes_per_sec(number) {
    return cockpit.format_bytes(number) + "/s";
};

cockpit.format_delay = function format_delay(d) {
    var seconds = Math.round(d/1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    seconds = seconds - minutes*60;
    minutes = minutes - hours*60;

    var s = seconds + " seconds";
    if (minutes > 0)
        s = minutes + " minutes, " + s;
    if (hours > 0)
        s = hours + " hours, " + s;
    return s;
};

})(cockpit);

function cockpit_add_thousands_separators(number)
{
    /* Translators: Thousands-separator ("," in en_US, "." in da_DK and so on) */
    var separator_char = C_("thousands-separator", ",");
    number += '';
    var x = number.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
	x1 = x1.replace(rgx, "$1" + separator_char + "$2");
    }
    return x1 + x2;
}

function cockpit_format_temperature(kelvin) {
    var celcius = kelvin - 273.15;
    var fahrenheit = 9.0 * celcius / 5.0 + 32.0;
    return celcius.toFixed(1) + "° C / " + fahrenheit.toFixed(1) + "° F";
}<commit_msg>shell: Remove unused cockpit_add_thousands_separater function<commit_after>/*
 * This file is part of Cockpit.
 *
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * Cockpit is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Cockpit is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
 */

var cockpit = cockpit || { };

// Used for escaping things in HTML elements and attributes
function cockpit_esc(str) {
    if (str === null || str === undefined)
        return "";
    var pre = document.createElement('pre');
    var text = document.createTextNode(str);
    pre.appendChild(text);
    return pre.innerHTML.replace(/"/g, "&quot;").replace(/'/g, "&#39;");
}

// Used for escaping things in HTML id attribute values
//
// http://www.w3.org/TR/html5/global-attributes.html#the-id-attribute
function cockpit_esc_id_attr(str) {
    return cockpit_esc(str).replace(/ /g, "&#20;").replace(/\x09/g, "&#09;").replace(/\x0a/g, "&#0a;").replace(/\x0c/g, "&#0c;").replace(/\x0d/g, "&#0d;");
}

function cockpit_debug(str) {
    console.debug("DEBUG: " + str);
}

/*
 * Byte formatting
 *
 * cockpit.format_bytes(number, [factor, separate])
 * @number: a normal number
 * @factor: optional, either 1000, 1024 or a string suffix
 * @separate: optional, when true return pieces in an array rather than string
 *
 * Formats bytes into a displayable string and suffix, such as
 * 'KB' or 'MB'. Returns an array of the formatted number and
 * the suffix if @separate is set.
 *
 * If specifying 1000 or 1024 these will be used as the factors
 * to choose an appropriate suffix. By default the factor is
 * 1024.
 *
 * You can pass the suffix into the second argument in which
 * case the number will be formatted with that specific suffix.
 *
 * If the number is less than the factor or an unknown suffix
 * was passed in, then the formatted number is returned without
 * a suffix.
 *
 * If @separate is true, returns an array of [formatted_number, suffix]
 * unless no suffix is returned.
 *
 * Examples:
 *    cockpit.format_bytes(1000000).join(" ");
 *    cockpit.format_bytes(1000000, 1024).join(" ");
 *    cockpit.format_bytes(1000000, "KB").join(" ");
 *
 * The current policy is to use KB, MB, GB, etc. for both factors
 * of 1000 and 1024.
 *
 *
 * cockpit.format_bytes_per_sec(number)
 * @number: the number to format
 *
 * Format bytes into a displayable speed string.
 *
 *
 * cockpit.format_delay(ms)
 * @ms: number of milli-seconds
 *
 * Format soconds into a string of "hours, minutes, seconds".
 */

(function(cockpit) {

var suffixes = {
    1024: [ null, "KB", "MB", "GB", "TB", "PB", "EB", "ZB" ],
    1000: [ null, "KB", "MB", "GB", "TB", "PB", "EB", "ZB" ]
    /* 1024: [ null, "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB" ] */
};

cockpit.format_bytes = function format_bytes(number, factor, separate) {
    var quotient;
    var suffix = null;

    if (factor === undefined)
        factor = 1024;

    /* Find that factor string */
    if (typeof (factor) === "string") {
        /* Prefer larger factors */
        var keys = [];
        for (var key in suffixes)
            keys.push(key);
        keys.sort().reverse();
        for (var y = 0; y < keys.length; y++) {
            for (var x = 0; x < suffixes[keys[y]].length; x++) {
                if (factor == suffixes[keys[y]][x]) {
                    number = number / Math.pow(keys[y], x);
                    suffix = factor;
                    break;
                }
            }
            if (suffix)
                break;
        }

    /* @factor is a number */
    } else if (factor in suffixes) {
        var divisor = 1;
        for (var i = 0; i < suffixes[factor].length; i++) {
            quotient = number / divisor;
            if (quotient < factor) {
                number = quotient;
                suffix = suffixes[factor][i];
                break;
            }
            divisor *= factor;
        }
    }

    var ret;

    if (!suffix) {
        ret = [number.toString()];
        if (!separate)
            ret = ret.join(" ");
        return ret;
    }

    /* non-zero values should never appear zero */
    if (number > 0 && number < 0.1)
        number = 0.1;

    /* TODO: Make the decimal separator translatable */
    if (number === 0)
        ret = [number.toString(), suffix];
    else
        ret = [number.toFixed(1), suffix];
    if (!separate)
        ret = ret.join(" ");
    return ret;
};

cockpit.format_bytes_per_sec = function format_bytes_per_sec(number) {
    return cockpit.format_bytes(number) + "/s";
};

cockpit.format_delay = function format_delay(d) {
    var seconds = Math.round(d/1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    seconds = seconds - minutes*60;
    minutes = minutes - hours*60;

    var s = seconds + " seconds";
    if (minutes > 0)
        s = minutes + " minutes, " + s;
    if (hours > 0)
        s = hours + " hours, " + s;
    return s;
};

})(cockpit);

function cockpit_format_temperature(kelvin) {
    var celcius = kelvin - 273.15;
    var fahrenheit = 9.0 * celcius / 5.0 + 32.0;
    return celcius.toFixed(1) + "° C / " + fahrenheit.toFixed(1) + "° F";
}
{'fraction_non_alphanumeric': 0.14022578728461083, 'fraction_numerical': 0.023172905525846704, 'mean_word_length': 2.501337295690936, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6117773', 'n_tokens_mistral': 3957, 'n_tokens_neox': 3622, 'n_words': 1231}