1(function (global, factory) { 2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : 3 typeof define === 'function' && define.amd ? define(factory) : 4 (global = global || self, global.byteSize = factory()); 5}(this, function () { 'use strict'; 6 7 /** 8 * An isomorphic, load-anywhere function to convert a bytes value into a more human-readable format. Choose between [metric or IEC units](https://en.wikipedia.org/wiki/Gigabyte), summarised below. 9 * 10 * Value | Metric 11 * ----- | ------------- 12 * 1000 | kB kilobyte 13 * 1000^2 | MB megabyte 14 * 1000^3 | GB gigabyte 15 * 1000^4 | TB terabyte 16 * 1000^5 | PB petabyte 17 * 1000^6 | EB exabyte 18 * 1000^7 | ZB zettabyte 19 * 1000^8 | YB yottabyte 20 * 21 * Value | IEC 22 * ----- | ------------ 23 * 1024 | KiB kibibyte 24 * 1024^2 | MiB mebibyte 25 * 1024^3 | GiB gibibyte 26 * 1024^4 | TiB tebibyte 27 * 1024^5 | PiB pebibyte 28 * 1024^6 | EiB exbibyte 29 * 1024^7 | ZiB zebibyte 30 * 1024^8 | YiB yobibyte 31 * 32 * Value | Metric (octet) 33 * ----- | ------------- 34 * 1000 | ko kilooctet 35 * 1000^2 | Mo megaoctet 36 * 1000^3 | Go gigaoctet 37 * 1000^4 | To teraoctet 38 * 1000^5 | Po petaoctet 39 * 1000^6 | Eo exaoctet 40 * 1000^7 | Zo zettaoctet 41 * 1000^8 | Yo yottaoctet 42 * 43 * Value | IEC (octet) 44 * ----- | ------------ 45 * 1024 | Kio kilooctet 46 * 1024^2 | Mio mebioctet 47 * 1024^3 | Gio gibioctet 48 * 1024^4 | Tio tebioctet 49 * 1024^5 | Pio pebioctet 50 * 1024^6 | Eio exbioctet 51 * 1024^7 | Zio zebioctet 52 * 1024^8 | Yio yobioctet 53 * 54 * @module byte-size 55 * @example 56 * ```js 57 * const byteSize = require('byte-size') 58 * ``` 59 */ 60 61 class ByteSize { 62 constructor (bytes, options) { 63 options = options || {}; 64 options.units = options.units || 'metric'; 65 options.precision = typeof options.precision === 'undefined' ? 1 : options.precision; 66 67 const table = [ 68 { expFrom: 0, expTo: 1, metric: 'B', iec: 'B', metric_octet: 'o', iec_octet: 'o' }, 69 { expFrom: 1, expTo: 2, metric: 'kB', iec: 'KiB', metric_octet: 'ko', iec_octet: 'Kio' }, 70 { expFrom: 2, expTo: 3, metric: 'MB', iec: 'MiB', metric_octet: 'Mo', iec_octet: 'Mio' }, 71 { expFrom: 3, expTo: 4, metric: 'GB', iec: 'GiB', metric_octet: 'Go', iec_octet: 'Gio' }, 72 { expFrom: 4, expTo: 5, metric: 'TB', iec: 'TiB', metric_octet: 'To', iec_octet: 'Tio' }, 73 { expFrom: 5, expTo: 6, metric: 'PB', iec: 'PiB', metric_octet: 'Po', iec_octet: 'Pio' }, 74 { expFrom: 6, expTo: 7, metric: 'EB', iec: 'EiB', metric_octet: 'Eo', iec_octet: 'Eio' }, 75 { expFrom: 7, expTo: 8, metric: 'ZB', iec: 'ZiB', metric_octet: 'Zo', iec_octet: 'Zio' }, 76 { expFrom: 8, expTo: 9, metric: 'YB', iec: 'YiB', metric_octet: 'Yo', iec_octet: 'Yio' } 77 ]; 78 79 const base = options.units === 'metric' || options.units === 'metric_octet' ? 1000 : 1024; 80 const prefix = bytes < 0 ? '-' : ''; 81 bytes = Math.abs(bytes); 82 83 for (let i = 0; i < table.length; i++) { 84 const lower = Math.pow(base, table[i].expFrom); 85 const upper = Math.pow(base, table[i].expTo); 86 if (bytes >= lower && bytes < upper) { 87 const units = table[i][options.units]; 88 if (i === 0) { 89 this.value = prefix + bytes; 90 this.unit = units; 91 return 92 } else { 93 this.value = prefix + (bytes / lower).toFixed(options.precision); 94 this.unit = units; 95 return 96 } 97 } 98 } 99 100 this.value = prefix + bytes; 101 this.unit = ''; 102 } 103 104 toString () { 105 return `${this.value} ${this.unit}`.trim() 106 } 107 } 108 109 /** 110 * @param {number} - the bytes value to convert. 111 * @param [options] {object} - optional config. 112 * @param [options.precision=1] {number} - number of decimal places. 113 * @param [options.units=metric] {string} - select `'metric'`, `'iec'`, `'metric_octet'` or `'iec_octet'` units. 114 * @returns {{ value: string, unit: string}} 115 * @alias module:byte-size 116 * @example 117 * ```js 118 * > const byteSize = require('byte-size') 119 * 120 * > byteSize(1580) 121 * { value: '1.6', unit: 'kB' } 122 * 123 * > byteSize(1580, { units: 'iec' }) 124 * { value: '1.5', unit: 'KiB' } 125 * 126 * > byteSize(1580, { units: 'iec', precision: 3 }) 127 * { value: '1.543', unit: 'KiB' } 128 * 129 * > byteSize(1580, { units: 'iec', precision: 0 }) 130 * { value: '2', unit: 'KiB' } 131 * 132 * > byteSize(1580, { units: 'metric_octet' }) 133 * { value: '1.6', unit: 'ko' } 134 * 135 * > byteSize(1580, { units: 'iec_octet' }) 136 * { value: '1.5', unit: 'Kio' } 137 * 138 * > byteSize(1580, { units: 'iec_octet' }).toString() 139 * '1.5 Kio' 140 * 141 * > const { value, unit } = byteSize(1580, { units: 'iec_octet' }) 142 * > `${value} ${unit}` 143 * '1.5 Kio' 144 * ``` 145 */ 146 function byteSize (bytes, options) { 147 return new ByteSize(bytes, options) 148 } 149 150 return byteSize; 151 152})); 153