github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/utils/units.js (about)

     1  export const BYTES_UNITS = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
     2  export const HERTZ_UNITS = ['Hz', 'KHz', 'MHz', 'GHz', 'THz', 'PHz'];
     3  
     4  const locale = window.navigator.locale || 'en';
     5  const decimalFormatter = new Intl.NumberFormat(locale, {
     6    maximumFractionDigits: 2,
     7  });
     8  const roundFormatter = new Intl.NumberFormat(locale, {
     9    maximumFractionDigits: 0,
    10  });
    11  
    12  const unitReducer = (number = 0, interval, units, maxUnit, startingUnit) => {
    13    if (startingUnit && units.indexOf(startingUnit) !== -1) {
    14      units = units.slice(units.indexOf(startingUnit));
    15    }
    16    if (maxUnit && units.indexOf(maxUnit) !== -1) {
    17      units = units.slice(0, units.indexOf(maxUnit) + 1);
    18    }
    19  
    20    // Reduce negative numbers by temporarily flipping them positive.
    21    const negative = number < 0;
    22    if (negative) {
    23      number *= -1;
    24    }
    25  
    26    let unitIndex = 0;
    27    while (number >= interval && unitIndex < units.length - 1) {
    28      number /= interval;
    29      unitIndex++;
    30    }
    31  
    32    if (negative) {
    33      number *= -1;
    34    }
    35    return [number, units[unitIndex]];
    36  };
    37  
    38  export function reduceBytes(bytes = 0, maxUnitSize, startingUnitSize) {
    39    return unitReducer(bytes, 1024, BYTES_UNITS, maxUnitSize, startingUnitSize);
    40  }
    41  
    42  export function reduceHertz(hertz = 0, maxUnitSize, startingUnitSize) {
    43    return unitReducer(hertz, 1000, HERTZ_UNITS, maxUnitSize, startingUnitSize);
    44  }
    45  
    46  // General purpose formatters meant to reduce units as much
    47  // as possible.
    48  export function formatBytes(bytes, startingUnitSize) {
    49    const [number, unit] = reduceBytes(bytes, null, startingUnitSize);
    50    return `${decimalFormatter.format(number)} ${unit}`;
    51  }
    52  
    53  export function formatHertz(hertz, startingUnitSize) {
    54    const [number, unit] = reduceHertz(hertz, null, startingUnitSize);
    55    return `${decimalFormatter.format(number)} ${unit}`;
    56  }
    57  
    58  // Specialized formatters meant to reduce units to the resolution
    59  // the scheduler and job specs operate at.
    60  export function formatScheduledBytes(bytes, startingUnitSize) {
    61    const [number, unit] = reduceBytes(bytes, 'MiB', startingUnitSize);
    62    return `${roundFormatter.format(number)} ${unit}`;
    63  }
    64  
    65  export function formatScheduledHertz(hertz, startingUnitSize) {
    66    const [number, unit] = reduceHertz(hertz, 'MHz', startingUnitSize);
    67    return `${roundFormatter.format(number)} ${unit}`;
    68  }
    69  
    70  // Replaces the minus sign (−) with a hyphen (-).
    71  // https://github.com/d3/d3-format/releases/tag/v2.0.0
    72  export function replaceMinus(input) {
    73    return input.replace('−', '-');
    74  }