Useful methods for file-related functions.
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

21 righe
593 B

  1. import numeral from 'numeral'
  2. type FormatRawFileSize = (n: number) => string
  3. const formatRawFileSize: FormatRawFileSize = n => {
  4. if (typeof n as unknown !== 'number') {
  5. throw TypeError('Argument should be a number.')
  6. }
  7. if (isNaN(n)) {
  8. throw RangeError('Cannot format NaN.')
  9. }
  10. const absValue = Math.abs(n)
  11. const base = numeral(absValue < 1000 ? absValue : 999).format('0 b')
  12. const suffix = base.slice(base.indexOf(' ') + ' '.length)
  13. return `${n < 0 ? '-' : ''}${numeral(absValue).format('0,0')} ${suffix}`
  14. }
  15. export default formatRawFileSize