Useful methods for file-related functions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
496 B

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