Gathering detailed insights and metrics for byte-size
Gathering detailed insights and metrics for byte-size
Gathering detailed insights and metrics for byte-size
Gathering detailed insights and metrics for byte-size
@types/byte-size
TypeScript definitions for byte-size
unicode-byte-truncate
Unicode aware string truncation that given a max byte size will truncate the string to or just below that size
randomhex
Will generate a random HEX string of a specifc byte size.
json-sizeof
Get the byte size of an object after JSON.stringify
Isomorphic function to convert a bytes value (e.g. 3456) to a human-readable string ('3.5 kB')
npm install byte-size
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
75 Stars
94 Commits
4 Forks
2 Watching
1 Branches
6 Contributors
Updated on 23 Sept 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-5.9%
354,136
Compared to previous day
Last week
2%
2,007,860
Compared to previous week
Last month
19.7%
8,102,509
Compared to previous month
Last year
-5.3%
83,573,991
Compared to previous year
3
Upgraders, please check the release notes.
An isomorphic, load-anywhere function to convert a bytes value (e.g. 3456
) to a human-readable string ('3.5 kB'
). Choose between metric or IEC units (summarised below) or specify your own custom units.
Value | Metric | Metric (octet) |
---|---|---|
1000 | kB kilobyte | ko kilooctet |
1000^2 | MB megabyte | Mo megaoctet |
1000^3 | GB gigabyte | Go gigaoctet |
1000^4 | TB terabyte | To teraoctet |
1000^5 | PB petabyte | Po petaoctet |
1000^6 | EB exabyte | Eo exaoctet |
1000^7 | ZB zettabyte | Zo zettaoctet |
1000^8 | YB yottabyte | Yo yottaoctet |
Value | IEC | IEC (octet) |
---|---|---|
1024 | KiB kibibyte | Kio kibioctet |
1024^2 | MiB mebibyte | Mio mebioctet |
1024^3 | GiB gibibyte | Gio gibioctet |
1024^4 | TiB tebibyte | Tio tebioctet |
1024^5 | PiB pebibyte | Pio pebioctet |
1024^6 | EiB exbibyte | Eio exbioctet |
1024^7 | ZiB zebibyte | Zio zebioctet |
1024^8 | YiB yobibyte | Yio yobioctet |
By default, byteSize
converts the input number to a human readable string with metric units and a precision of 1.
1> const byteSize = require('byte-size') 2 3> byteSize(1580) 4{ value: '1.6', unit: 'kB', long: 'kilobytes' }
The object returned by byteSize
defines a toString
method therefore can be used directly in string context.
1> `Filesize: ${byteSize(12400)}` 2'Filesize: 12.4 kB'
Override the default toString
behaviour by setting options.toStringFn
.
1> function toStringFn () { 2 return `**${this.value}${this.unit}**` 3} 4 5> `Filesize: ${byteSize(12400, { toStringFn })}` 6'Filesize: **12.4kB**'
Beside the default of metric
, there are three other built-in units available: metric_octet
, iec
and iec_octet
.
1> byteSize(1580, { units: 'iec' }) 2{ value: '1.5', unit: 'KiB', long: 'kibibytes' } 3 4> byteSize(1580, { units: 'iec_octet' }) 5{ value: '1.5', unit: 'Kio', long: 'kibioctets' } 6 7> byteSize(1580, { units: 'metric_octet' }) 8{ value: '1.6', unit: 'ko', long: 'kilooctets' }
You can adjust the precision
.
1> byteSize(1580, { units: 'iec', precision: 3 }) 2{ value: '1.543', unit: 'KiB', long: 'kibibytes' } 3 4> byteSize(1580, { units: 'iec', precision: 0 }) 5{ value: '2', unit: 'KiB', long: 'kibibytes' }
Define custom units by passing an object containing one or more additional conversion tables to options.customUnits
. In options.units
, specify the name of a property from the customUnits
object.
1> const customUnits = { 2 simple: [ 3 { from: 0 , to: 1e3 , unit: '' }, 4 { from: 1e3 , to: 1e6 , unit: 'K', long: 'thousand' }, 5 { from: 1e6 , to: 1e9 , unit: 'Mn', long: 'million' }, 6 { from: 1e9 , to: 1e12, unit: 'Bn', long: 'billion' } 7 ] 8} 9 10> const { value, unit } = byteSize(10000, { customUnits, units: 'simple' }) 11 12> `${value}${unit}` 13'10.0K'
Override the built-in defaults for the duration of the process by passing an options object to byteSize.defaultOptions()
. This results in cleaner code in cases where byteSize
is used often with the same options.
1> byteSize.defaultOptions({ 2 units: 'simple', 3 precision: 2, 4 customUnits: { 5 simple: [ 6 { from: 0, to: 1e3, unit: '' }, 7 { from: 1e3, to: 1e6, unit: 'k' }, 8 { from: 1e6, to: 1e9, unit: 'm' }, 9 { from: 1e9, to: 1e12, unit: 'bn' }, 10 ] 11 }, 12 toStringFn: function () { 13 return this.value + this.unit 14 } 15}) 16 17> [2400, 16400, 3991200].map(byteSize).join(', ') 18'2.40k, 16.40k, 3.99m'
object
⏏Returns an object with the spec { value: string, unit: string, long: string }
. The returned object defines a toString
method meaning it can be used in any string context.
Kind: Exported function
Param | Type | Description |
---|---|---|
bytes | number | The bytes value to convert. |
[options] | object | Optional config. |
[options.precision] | number | Number of decimal places. Defaults to 1 . |
[options.units] | string | Specify 'metric' , 'iec' , 'metric_octet' , 'iec_octet' or the name of a property from the custom units table in options.customUnits . Defaults to metric . |
[options.customUnits] | object | An object containing one or more custom unit lookup tables. |
[options.toStringFn] | function | A toString function to override the default. |
[options.locale] | string | Array.<string> | Node >=13 or modern browser only - on earlier platforms this option is ignored. The locale to use for number formatting (e.g. 'de-DE' ). Defaults to your system locale. Passed directed into Intl.NumberFormat(). |
Set the default byteSize
options for the duration of the process.
Kind: static method of byteSize
Param | Type | Description |
---|---|---|
options | object | A byteSize options object. |
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
Node.js (CommonJS):
1const byteSize = require('byte-size')
Node.js (ECMAScript Module):
1import byteSize from 'byte-size'
Within a modern browser ECMAScript Module:
1import byteSize from './node_modules/byte-size/index.js'
Browser global (adds window.byteSize
):
1<script src="./node_modules/byte-size/dist/index.js"></script>
© 2014-24 Lloyd Brookes <75pound@gmail.com>.
Tested by test-runner. Documented by jsdoc-to-markdown.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More