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 FormatRawFileSize = (n: number) => string
-
- const formatRawFileSize: FormatRawFileSize = n => {
- if (typeof n as unknown !== 'number') {
- throw TypeError('Argument should be a number.')
- }
-
- if (isNaN(n)) {
- throw RangeError('Cannot format NaN.')
- }
-
- const absValue = Math.abs(n)
- const base = numeral(absValue < 1000 ? absValue : 999).format('0 b')
- const suffix = base.slice(base.indexOf(' ') + ' '.length)
- return `${n < 0 ? '-' : ''}${numeral(absValue).format('0,0')} ${suffix}`
- }
-
- export default formatRawFileSize
|