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.
|
- import numeral from 'numeral'
-
- type FormatFileSize = (maybeNumber: unknown) => string
-
- const formatFileSize: FormatFileSize = maybeNumber => {
- if (typeof maybeNumber! !== 'number') {
- throw TypeError('Argument should be a number.')
- }
-
- const n = maybeNumber as number
- if (isNaN(n)) {
- throw RangeError('Cannot format NaN.')
- }
-
- const base = numeral(Math.abs(n)).format(Math.abs(n) < 1000 ? '0 b' : '0.00 b')
-
- return `${n < 0 ? '-' : ''}${base}`
- }
-
- export default formatFileSize
|