Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

1.6 KB
Raw Blame
/**
 * Shared byte-size formatter.
 *
 * Single source of truth for "1536 -> 1.5 KB" style formatting, previously
 * duplicated as formatBytes in deletion/delete_manager.js and formatFileSize
 * in pdf_upload_handler.js. Loaded globally from base.html so both consumers
 * (and any future ones) can call window.formatBytes.
 */
(function() {
    'use strict';

    /**
     * Format a byte count as a human-readable size.
     * @param {number} bytes - Non-negative byte count.
     * @returns {string} e.g. "0 Bytes", "1.5 KB", "1 MB".
     */
    function formatBytes(bytes) {
        // Covers 0, negatives, NaN, undefined, and null. Relational
        // comparison coerces null to 0 and undefined to NaN.
        // Without it, Math.log(-1) is NaN, which propagates through the
        // clamp and yields "NaN undefined". Number.isFinite additionally
        // rejects Infinity, which the relational check lets through and
        // the unit clamp would render as "Infinity EB".
        if (!(bytes > 0) || !Number.isFinite(bytes)) return '0 Bytes';
        const k = 1024;
        const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'];
        // Clamp both ends: the upper bound stops indexing past the unit list
        // for very large values, and the lower bound keeps sub-1-byte inputs
        // (log < 0) from producing a negative index -> undefined unit.
        const i = Math.max(0, Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1));
        return parseFloat((bytes / k ** i).toFixed(2)) + ' ' + sizes[i];
    }

    window.formatBytes = formatBytes;
})();