|
| 1 | +import {formatNumber} from '..'; |
| 2 | +import {GIGABYTE, KILOBYTE, MEGABYTE, TERABYTE} from '../constants'; |
| 3 | +import {isNumeric} from '../utils'; |
| 4 | + |
| 5 | +import i18n from './i18n'; |
| 6 | + |
| 7 | +const sizes = { |
| 8 | + b: { |
| 9 | + value: 1, |
| 10 | + label: i18n('b'), |
| 11 | + }, |
| 12 | + kb: { |
| 13 | + value: KILOBYTE, |
| 14 | + label: i18n('kb'), |
| 15 | + }, |
| 16 | + mb: { |
| 17 | + value: MEGABYTE, |
| 18 | + label: i18n('mb'), |
| 19 | + }, |
| 20 | + gb: { |
| 21 | + value: GIGABYTE, |
| 22 | + label: i18n('gb'), |
| 23 | + }, |
| 24 | + tb: { |
| 25 | + value: TERABYTE, |
| 26 | + label: i18n('tb'), |
| 27 | + }, |
| 28 | +}; |
| 29 | + |
| 30 | +export type BytesSizes = keyof typeof sizes; |
| 31 | + |
| 32 | +/** |
| 33 | + * This function is needed to keep more than 3 digits of the same size. |
| 34 | + * |
| 35 | + * @param significantDigits - number of digits above 3 |
| 36 | + * @returns size to format value to get required number of digits |
| 37 | + * |
| 38 | + * By default value converted to the next size when it's above 1000, |
| 39 | + * so we have 900mb and 1gb. To extend it additional significantDigits could be set |
| 40 | + * |
| 41 | + * significantDigits value added above default 3 |
| 42 | + * |
| 43 | + * significantDigits = 1 - 9 000 mb and 10 gb |
| 44 | + * |
| 45 | + * significantDigits = 2 - 90 000 mb and 100 gb |
| 46 | + * |
| 47 | + * significantDigits = 3 - 900 000 mb and 1000 gb |
| 48 | + */ |
| 49 | +const getSizeWithSignificantDigits = (value: number, significantDigits: number) => { |
| 50 | + const multiplier = 10 ** significantDigits; |
| 51 | + |
| 52 | + const tbLevel = sizes.tb.value * multiplier; |
| 53 | + const gbLevel = sizes.gb.value * multiplier; |
| 54 | + const mbLevel = sizes.mb.value * multiplier; |
| 55 | + const kbLevel = sizes.kb.value * multiplier; |
| 56 | + |
| 57 | + let size: BytesSizes = 'b'; |
| 58 | + |
| 59 | + if (value >= kbLevel) { |
| 60 | + size = 'kb'; |
| 61 | + } |
| 62 | + if (value >= mbLevel) { |
| 63 | + size = 'mb'; |
| 64 | + } |
| 65 | + if (value >= gbLevel) { |
| 66 | + size = 'gb'; |
| 67 | + } |
| 68 | + if (value >= tbLevel) { |
| 69 | + size = 'tb'; |
| 70 | + } |
| 71 | + |
| 72 | + return size; |
| 73 | +}; |
| 74 | + |
| 75 | +interface FormatToSizeArgs { |
| 76 | + value: number; |
| 77 | + size?: BytesSizes; |
| 78 | + precision?: number; |
| 79 | +} |
| 80 | + |
| 81 | +const formatToSize = ({value, size = 'mb', precision = 0}: FormatToSizeArgs) => { |
| 82 | + const result = (Number(value) / sizes[size].value).toFixed(precision); |
| 83 | + |
| 84 | + return formatNumber(result); |
| 85 | +}; |
| 86 | + |
| 87 | +const addSizeLabel = (result: string, size: BytesSizes) => { |
| 88 | + return result + ` ${sizes[size].label}`; |
| 89 | +}; |
| 90 | + |
| 91 | +const addSpeedLabel = (result: string, size: BytesSizes) => { |
| 92 | + return addSizeLabel(result, size) + i18n('perSecond'); |
| 93 | +}; |
| 94 | + |
| 95 | +export type FormatBytesArgs = Omit<FormatToSizeArgs, 'value'> & { |
| 96 | + value: number | string | undefined | null; |
| 97 | + withSpeedLabel?: boolean; |
| 98 | + withSizeLabel?: boolean; |
| 99 | + significantDigits?: number; |
| 100 | +}; |
| 101 | + |
| 102 | +/** |
| 103 | + * @param significantDigits - number of digits above 3 |
| 104 | + */ |
| 105 | +export const formatBytes = ({ |
| 106 | + value, |
| 107 | + size, |
| 108 | + withSpeedLabel = false, |
| 109 | + withSizeLabel = true, |
| 110 | + significantDigits = 0, |
| 111 | + ...params |
| 112 | +}: FormatBytesArgs) => { |
| 113 | + if (!isNumeric(value)) { |
| 114 | + return ''; |
| 115 | + } |
| 116 | + |
| 117 | + const numValue = Number(value); |
| 118 | + |
| 119 | + const sizeToConvert = size ?? getSizeWithSignificantDigits(numValue, significantDigits); |
| 120 | + |
| 121 | + const result = formatToSize({value: numValue, size: sizeToConvert, ...params}); |
| 122 | + |
| 123 | + if (withSpeedLabel) { |
| 124 | + return addSpeedLabel(result, sizeToConvert); |
| 125 | + } |
| 126 | + |
| 127 | + if (withSizeLabel) { |
| 128 | + return addSizeLabel(result, sizeToConvert); |
| 129 | + } |
| 130 | + |
| 131 | + return result; |
| 132 | +}; |
0 commit comments