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.
|
- import numeral from 'numeral'
-
- type FormatFileSize = (n: number) => string
-
- const formatFileSize: FormatFileSize = n => {
- if (typeof n as unknown !== 'number') {
- throw TypeError('Argument should be a 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
|