tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

format-utils.js (3590B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const {
      8  L10N,
      9 } = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
     10 
     11 // Constants for formatting bytes.
     12 const BYTES_IN_KB = 1000;
     13 const BYTES_IN_MB = Math.pow(BYTES_IN_KB, 2);
     14 const BYTES_IN_GB = Math.pow(BYTES_IN_KB, 3);
     15 const MAX_BYTES_SIZE = 1000;
     16 const MAX_KB_SIZE = 1000 * BYTES_IN_KB;
     17 const MAX_MB_SIZE = 1000 * BYTES_IN_MB;
     18 
     19 // Constants for formatting time.
     20 const MAX_MILLISECOND = 1000;
     21 const MAX_SECOND = 60 * MAX_MILLISECOND;
     22 
     23 const REQUEST_DECIMALS = 2;
     24 
     25 // Constants for formatting the priority, derived from nsISupportsPriority.idl
     26 const PRIORITY_HIGH = -10;
     27 const PRIORITY_NORMAL = 0;
     28 const PRIORITY_LOW = 10;
     29 
     30 function getSizeWithDecimals(size, decimals = REQUEST_DECIMALS) {
     31  return L10N.numberWithDecimals(size, decimals);
     32 }
     33 
     34 function getTimeWithDecimals(time) {
     35  return L10N.numberWithDecimals(time, REQUEST_DECIMALS);
     36 }
     37 
     38 function formatDecimals(size, decimals) {
     39  return size % 1 > 0 ? decimals : 0;
     40 }
     41 
     42 /**
     43 * Get a human-readable string from a number of bytes, with the B, kB, MB, or
     44 * GB value.
     45 */
     46 function getFormattedSize(bytes, decimals = REQUEST_DECIMALS) {
     47  if (bytes < MAX_BYTES_SIZE) {
     48    return L10N.getFormatStr("networkMenu.sizeB", bytes);
     49  }
     50  if (bytes < MAX_KB_SIZE) {
     51    const kb = bytes / BYTES_IN_KB;
     52    const formattedDecimals = formatDecimals(kb, decimals);
     53 
     54    return L10N.getFormatStr(
     55      "networkMenu.size.kB",
     56      getSizeWithDecimals(kb, formattedDecimals)
     57    );
     58  }
     59  if (bytes < MAX_MB_SIZE) {
     60    const mb = bytes / BYTES_IN_MB;
     61    const formattedDecimals = formatDecimals(mb, decimals);
     62    return L10N.getFormatStr(
     63      "networkMenu.sizeMB",
     64      getSizeWithDecimals(mb, formattedDecimals)
     65    );
     66  }
     67  const gb = bytes / BYTES_IN_GB;
     68  const formattedDecimals = formatDecimals(gb, decimals);
     69  return L10N.getFormatStr(
     70    "networkMenu.sizeGB",
     71    getSizeWithDecimals(gb, formattedDecimals)
     72  );
     73 }
     74 
     75 /**
     76 * Get a human-readable string from a number of time, with the ms, s, or min
     77 * value.
     78 */
     79 function getFormattedTime(ms) {
     80  if (ms < MAX_MILLISECOND) {
     81    return L10N.getFormatStr("networkMenu.millisecond", ms | 0);
     82  }
     83  if (ms < MAX_SECOND) {
     84    const sec = ms / MAX_MILLISECOND;
     85    return L10N.getFormatStr("networkMenu.second", getTimeWithDecimals(sec));
     86  }
     87  const min = ms / MAX_SECOND;
     88  return L10N.getFormatStr("networkMenu.minute", getTimeWithDecimals(min));
     89 }
     90 
     91 /**
     92 * Formats IP (v4 and v6) and port
     93 *
     94 * @param {string} ip - IP address
     95 * @param {string} port
     96 * @return {string} the formatted IP + port
     97 */
     98 function getFormattedIPAndPort(ip, port) {
     99  if (!port) {
    100    return ip;
    101  }
    102  return ip.match(/:+/) ? `[${ip}]:${port}` : `${ip}:${port}`;
    103 }
    104 
    105 /**
    106 * Formats the priority of a request
    107 * Based on unix conventions
    108 * See xpcom/threads/nsISupportsPriority.idl
    109 *
    110 * @param {number} priority - request priority
    111 */
    112 function getRequestPriorityAsText(priority) {
    113  if (priority < PRIORITY_HIGH) {
    114    return "Highest";
    115  } else if (priority >= PRIORITY_HIGH && priority < PRIORITY_NORMAL) {
    116    return "High";
    117  } else if (priority === PRIORITY_NORMAL) {
    118    return "Normal";
    119  } else if (priority > PRIORITY_NORMAL && priority <= PRIORITY_LOW) {
    120    return "Low";
    121  }
    122  return "Lowest";
    123 }
    124 
    125 module.exports = {
    126  getFormattedIPAndPort,
    127  getFormattedSize,
    128  getFormattedTime,
    129  getSizeWithDecimals,
    130  getTimeWithDecimals,
    131  getRequestPriorityAsText,
    132 };