Useful methods for file-related functions.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

20 linhas
441 B

  1. import numeral from 'numeral'
  2. type FormatFileSize = (n: number) => string
  3. const formatFileSize: FormatFileSize = 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 base = numeral(Math.abs(n)).format(Math.abs(n) < 1000 ? '0 b' : '0.00 b')
  11. return `${n < 0 ? '-' : ''}${base}`
  12. }
  13. export default formatFileSize