All files / app/assets/javascripts/lib/utils/unit_format index.js

100% Statements 60/60
100% Branches 39/39
100% Functions 1/1
100% Lines 60/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386                                  46x                                                                           46x   1117x 59x   1058x 59x   999x 67x       932x 46x   886x 59x   827x 59x       768x 59x   709x 59x   650x 59x   591x 59x   532x 59x   473x 59x       414x 59x   355x 59x   296x 59x   237x 59x   178x 59x   119x 59x       60x 59x       1x                           46x                         46x                         46x                         46x                         46x                         46x                           46x                           46x                           46x                           46x                           46x                           46x                           46x                           46x                           46x                           46x                           46x                           46x                         46x  
import { engineeringNotation } from '@gitlab/ui/src/utils/number_utils';
import { s__ } from '~/locale';
 
import {
  suffixFormatter,
  scaledSIFormatter,
  scaledBinaryFormatter,
  numberFormatter,
} from './formatter_factory';
 
/**
 * Supported formats
 *
 * Based on:
 *
 * https://tc39.es/proposal-unified-intl-numberformat/section6/locales-currencies-tz_proposed_out.html#sec-issanctionedsimpleunitidentifier
 */
export const SUPPORTED_FORMATS = {
  // Number
  number: 'number',
  percent: 'percent',
  percentHundred: 'percentHundred',
 
  // Duration
  days: 'days',
  seconds: 'seconds',
  milliseconds: 'milliseconds',
 
  // Digital (Metric)
  decimalBytes: 'decimalBytes',
  kilobytes: 'kilobytes',
  megabytes: 'megabytes',
  gigabytes: 'gigabytes',
  terabytes: 'terabytes',
  petabytes: 'petabytes',
 
  // Digital (IEC)
  bytes: 'bytes',
  kibibytes: 'kibibytes',
  mebibytes: 'mebibytes',
  gibibytes: 'gibibytes',
  tebibytes: 'tebibytes',
  pebibytes: 'pebibytes',
 
  // Engineering Notation
  engineering: 'engineering',
};
 
/**
 * Returns a function that formats number to different units.
 *
 * Used for dynamic formatting, for more convenience, use the functions below.
 *
 * @param {String} format - Format to use, must be one of the SUPPORTED_FORMATS. Defaults to engineering notation.
 */
export const getFormatter = (format = SUPPORTED_FORMATS.engineering) => {
  // Number
  if (format === SUPPORTED_FORMATS.number) {
    return numberFormatter();
  }
  if (format === SUPPORTED_FORMATS.percent) {
    return numberFormatter('percent');
  }
  if (format === SUPPORTED_FORMATS.percentHundred) {
    return numberFormatter('percent', 1 / 100);
  }
 
  // Durations
  if (format === SUPPORTED_FORMATS.days) {
    return suffixFormatter(s__('Units|d'));
  }
  if (format === SUPPORTED_FORMATS.seconds) {
    return suffixFormatter(s__('Units|s'));
  }
  if (format === SUPPORTED_FORMATS.milliseconds) {
    return suffixFormatter(s__('Units|ms'));
  }
 
  // Digital (Metric)
  if (format === SUPPORTED_FORMATS.decimalBytes) {
    return scaledSIFormatter('B');
  }
  if (format === SUPPORTED_FORMATS.kilobytes) {
    return scaledSIFormatter('B', 1);
  }
  if (format === SUPPORTED_FORMATS.megabytes) {
    return scaledSIFormatter('B', 2);
  }
  if (format === SUPPORTED_FORMATS.gigabytes) {
    return scaledSIFormatter('B', 3);
  }
  if (format === SUPPORTED_FORMATS.terabytes) {
    return scaledSIFormatter('B', 4);
  }
  if (format === SUPPORTED_FORMATS.petabytes) {
    return scaledSIFormatter('B', 5);
  }
 
  // Digital (IEC)
  if (format === SUPPORTED_FORMATS.bytes) {
    return scaledBinaryFormatter('B');
  }
  if (format === SUPPORTED_FORMATS.kibibytes) {
    return scaledBinaryFormatter('B', 1);
  }
  if (format === SUPPORTED_FORMATS.mebibytes) {
    return scaledBinaryFormatter('B', 2);
  }
  if (format === SUPPORTED_FORMATS.gibibytes) {
    return scaledBinaryFormatter('B', 3);
  }
  if (format === SUPPORTED_FORMATS.tebibytes) {
    return scaledBinaryFormatter('B', 4);
  }
  if (format === SUPPORTED_FORMATS.pebibytes) {
    return scaledBinaryFormatter('B', 5);
  }
 
  // Default
  if (format === SUPPORTED_FORMATS.engineering) {
    return engineeringNotation;
  }
 
  // Fail so client library addresses issue
  throw TypeError(`${format} is not a valid number format`);
};
 
/**
 * Formats a number
 *
 * @function
 * @param {Number} value - Number to format
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const number = getFormatter(SUPPORTED_FORMATS.number);
 
/**
 * Formats a percentage (0 - 1)
 *
 * @function
 * @param {Number} value - Number to format, `1` is rendered as `100%`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const percent = getFormatter(SUPPORTED_FORMATS.percent);
 
/**
 * Formats a percentage (0 to 100)
 *
 * @function
 * @param {Number} value - Number to format, `100` is rendered as `100%`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const percentHundred = getFormatter(SUPPORTED_FORMATS.percentHundred);
 
/**
 * Formats a number of days
 *
 * @function
 * @param {Number} value - Number to format, `1` is rendered as `1d`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const days = getFormatter(SUPPORTED_FORMATS.days);
 
/**
 * Formats a number of seconds
 *
 * @function
 * @param {Number} value - Number to format, `1` is rendered as `1s`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const seconds = getFormatter(SUPPORTED_FORMATS.seconds);
 
/**
 * Formats a number of milliseconds with ms as units
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1ms`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const milliseconds = getFormatter(SUPPORTED_FORMATS.milliseconds);
 
/**
 * Formats a number of bytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1B`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const decimalBytes = getFormatter(SUPPORTED_FORMATS.decimalBytes);
 
/**
 * Formats a number of kilobytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1kB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const kilobytes = getFormatter(SUPPORTED_FORMATS.kilobytes);
 
/**
 * Formats a number of megabytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1MB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const megabytes = getFormatter(SUPPORTED_FORMATS.megabytes);
 
/**
 * Formats a number of gigabytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1GB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const gigabytes = getFormatter(SUPPORTED_FORMATS.gigabytes);
 
/**
 * Formats a number of terabytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1GB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const terabytes = getFormatter(SUPPORTED_FORMATS.terabytes);
 
/**
 * Formats a number of petabytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1PB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const petabytes = getFormatter(SUPPORTED_FORMATS.petabytes);
 
/**
 * Formats a number of bytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1B`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const bytes = getFormatter(SUPPORTED_FORMATS.bytes);
 
/**
 * Formats a number of kilobytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1kB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const kibibytes = getFormatter(SUPPORTED_FORMATS.kibibytes);
 
/**
 * Formats a number of megabytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1MB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const mebibytes = getFormatter(SUPPORTED_FORMATS.mebibytes);
 
/**
 * Formats a number of gigabytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1GB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const gibibytes = getFormatter(SUPPORTED_FORMATS.gibibytes);
 
/**
 * Formats a number of terabytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1GB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const tebibytes = getFormatter(SUPPORTED_FORMATS.tebibytes);
 
/**
 * Formats a number of petabytes scaled up to larger digital
 * units for larger numbers.
 *
 * @function
 * @param {Number} value - Number to format, `1` is formatted as `1PB`
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - number of precision decimals
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const pebibytes = getFormatter(SUPPORTED_FORMATS.pebibytes);
 
/**
 * Formats via engineering notation
 *
 * @function
 * @param {Number} value - Value to format
 * @param {Object} options - Formatting options
 * @param {Number} options.fractionDigits - precision decimals, defaults to 2
 * @param {Number} options.maxLength - Max length of formatted number
 * if length is exceeded, exponential format is used.
 * @param {String} options.unitSeparator - Separator between value and unit
 */
export const engineering = getFormatter();