Installations
npm install xbytes
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Min. Node Version
>=1
Node Version
21.6.0
NPM Version
10.4.0
Score
95.9
Supply Chain
100
Quality
77.8
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
miraclx
Download Statistics
Total Downloads
1,227,821
Last Day
1,993
Last Week
12,497
Last Month
72,264
Last Year
617,826
GitHub Statistics
25 Stars
124 Commits
9 Forks
4 Watching
2 Branches
6 Contributors
Bundle Size
9.01 kB
Minified
3.28 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.9.1
Package Id
xbytes@1.9.1
Unpacked Size
74.45 kB
Size
20.50 kB
File Count
11
NPM Version
10.4.0
Node Version
21.6.0
Publised On
01 Apr 2024
Total Downloads
Cumulative downloads
Total Downloads
1,227,821
Last day
-27.6%
1,993
Compared to previous day
Last week
-32%
12,497
Compared to previous week
Last month
-18.9%
72,264
Compared to previous month
Last year
171.7%
617,826
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
ByteParser (xbytes)
NodeJS Byte Parser Parse bytes to human readable sizes (4747) → ('4.75 KB') and vice versa.
Installing
Via NPM:
1npm install xbytes
Usage
1// Node CommonJS 2const xbytes = require('xbytes'); 3// Or Node ES6 Modules 4import xbytes from 'xbytes'; 5// Or TypeScript 6import * as xbytes from 'xbytes';
1<!-- Or in the Browser --> 2<script src="xbytes/dist/index.js"></script>
Examples
1xbytes(4747); // '4.75 KB' 2xbytes(-3946); // '-3.95 KB' 3xbytes(34.724e+4); // '347.24 KB' 4xbytes(32000000000); // '32.00 GB' 5xbytes(21474836480, {iec: true}); // '20.00 GiB' 6xbytes.parseSize('10 GB'); // 10000000 7xbytes.parseSize('-50 GiB'); // -53687091200
IEC Specification*
1xbytes(5242880, {iec: true}); // '5 MiB' 2 3xbytes.parseSize('10 GiB'); // 10485760
API
xbytes(byte[, options])
byte
: <number>options
: <ByteOptions>- Returns: <ByteString>
Parse byte
to human readable size. If byte
is not a number or an number-like string, return null
appropriately.
1xbytes(2472946) 2// << '2.47 MB' 3xbytes(49392123904, {iec: true}) 4// << '46.00 GiB'
xbytes.parseSize(str[, config])
str
: <ByteString>config
: <ParseOptions>- Returns: <number>
Parse human readable size to bytes
1xbytes.parseSize('15.967 MB') 2// << 15967000 3xbytes.parseSize('5.97 PiB') 4// << 6721622443850465
xbytes.parse(size)
str
: <HybridByte>- Returns: <ByteUnitObject>
Create a ByteUnitObject around the specified HybridByte
1xbytes.parse('10 MiB') 2// << ByteUnitObject { bytes: 10485760 } 3xbytes.parse('10 MiB').add('20 MiB') 4// << ByteUnitObject { bytes: 31457280 } 5xbytes.parse('10 MiB').add('20 MiB').toIECBytes() 6// << '30.00 MiB'
xbytes.isBytes(str)
Check if the provided string is a ByteString
1xbytes.isBytes('10 MiB') 2// << true 3xbytes.isBytes('Hello') 4// << false 5xbytes.isBytes('10 iB') 6// << false 7xbytes.isBytes('10b') 8// << true
xbytes.isUnit(str)
str
: <UnitString>- Returns: <boolean>
Check if the provided string is an UnitString
1xbytes.isUnit('GB') 2// << true 3xbytes.isUnit('giB') 4// << true 5xbytes.isUnit('iB') 6// << false 7xbytes.isUnit('BB') 8// << false
xbytes.isParsable(input)
input
: <HybridByte>- Returns: <boolean>
Check if the provided argument is parsable i.e raw_bytes (number) or ByteString.
xbytes.relative(size[, options])
size
: <HybridByte>options
: <ByteOptions>- Returns: <HybridByteRelations>
xbytes.relative.bits(size[, options])
size
: <HybridByte>options
: <ByteOptions>- Returns: <ByteString>
Show the input size in relation to its bit
format
xbytes.relative.bytes(size[, options])
size
: <HybridByte>options
: <ByteOptions>- Returns: <ByteString>
Show the input size in relation to its byte
format
xbytes.relative.iecBits(size[, options])
size
: <HybridByte>options
: <ByteOptions>- Returns: <ByteString>
Show the input size in relation to its bit
format under IEC Standards
xbytes.relative.iecBytes(size[, options])
size
: <HybridByte>options
: <ByteOptions>- Returns: <ByteString>
Show the input size in relation to its bytes
format under IEC Standards
xbytes.relative.size(size[, unit[, options]])
size
: <HybridByte>unit
: <UnitString>options
: <ByteOptions>- Returns: <ByteString>
xbytes.parseString(str)
str
: <ByteString>- Returns: <ParsedByteString>
Parse a human readable byte into its components
xbytes.parseBytes(str, options)
str
: <HybridByte>options
: <ByteOptions>- Returns: <ParsedBytes>
Parse a human readable byte into its components.
Extended from parseString(), but with a few extra properties.
And flexibility to use parse either an integer byte value or a ByteString
resulting in the same object.
xbytes.extractBytes(str)
str
: <string>- Returns: <ByteString[]>
Extract all ByteStrings within a string into an array, alternative to str.match(xbytes.byteFilter)
xbytes.createByteParser(config)
config
: <ByteOptions>- Returns: <ByteParser>
Construct a static ByteParser with predefined configurations
xbytes.createSizeParser(config)
config
: <ParseOptions>- Returns: <SizeParser>
Construct a static SizeParser with predefined configurations
xbytes.createRelativeSizer(unit[, config])
unit
: <UnitString>config
: <ByteOptions>- Returns: <RelativeSizer>
Create a RelativeSizer for converting a hybrid byte into any set unit under predefined configuration
xbytes.unitMatcher: RegExp
The raw Regular expression used in scanning all string byte units.
xbytes.genericMatcher: RegExp
The raw regular expression used in scanning all byte containing strings.
xbytes.byteFilter: RegExp
An regular expression extension of genericMatcher
with the 'i' flag.
xbytes.globalByteFilter: RegExp
An regular expression extension of genericMatcher
with the 'gi' flags.
UnitString: String
Supported Unit Strings
Index | Prefix | Decimal Bits | Binary Bits (IEC) | Decimal Bytes | Binary Bytes (IEC) |
---|---|---|---|---|---|
0 | - | b (Bits) | b (Bits) | b (Bits) | b (Bits) |
0 | - | B (Bytes) | B (Bytes) | B (Bytes) | B (Bytes) |
1 | K | Kb (KiloBits) | Kib (KiloBits) | KB (KiloBytes) | KiB (KibiBytes) |
2 | M | Mb (MegaBits) | Mib (MebiBits) | MB (MegaBytes) | MiB (MebiBytes) |
3 | G | Gb (GigaBits) | Gib (GibiBits) | GB (GigaBytes) | GiB (GibiBytes) |
4 | T | Tb (TeraBits) | Tib (TebiBits) | TB (TeraBytes) | TiB (TebiBytes) |
5 | P | Pb (PetaBits) | Pib (PebiBits) | PB (PetaBytes) | PiB (PebiBytes) |
6 | E | Eb (ExaBits) | Eib (ExbiBits) | EB (ExaBytes) | EiB (ExbiBytes) |
7 | Z | Zb (ZettaBits) | Zib (ZebiBits) | ZB (ZettaBytes) | ZiB (ZebiBytes) |
8 | Y | Yb (YottaBits) | Yib (YobiBits) | YB (YottaBytes) | YiB (YobiBytes) |
ByteString: String
A stringed byte representation. The result of a parsed byte value.
- '5 MB'
- '10 GiB'
- '0.67 Tb'
- '-50 KB'
- '2e+15 KB'
- '-9e-4 GB'
HybridByte: Number
|ByteString
Used to identify a variable thats either a ByteString or a number
- '47 MiB'
- '50kb'
- 74753
- '105'
ByteOptions: Object
iec
: <boolean> Whether or not to parse under the IEC standard i.e in terms of 1024. Default:true
bits
: <boolean> Whether or not to convert inputed bytes to bits and parse in terms of bits [1 byte = 8 bits]. Default:false
.fixed
: <number> Number of digits to include after decimal point. Default:2
.short
: <boolean> Whether or not to shorten unit String [short: 'MB', long: 'MegaBytes']. Default:true
.space
: <boolean> Whether or not to include a white space inbetween value and unit. Default:true
.sticky
: <boolean> Whether or not to retain unit on max unit values. e.g'1024.00 GiB'
instead of'1.00 TiB'
. Default:false
.prefixIndex
: <number> The index of unit relativity [See UnitString].
ParseOptions: Object
iec
: <boolean> Whether or not to enforce compliance to IEC standards. Default:true
.bits
: <boolean> Whether or not to parse a lower case 'b' in bit format. Default:true
.
ByteParser: Function
size
: <number>- Returns: <ByteString>
Byte parser with predefined configuration. Result of createByteParser
.
SizeParser: Function
str
: <ByteString[]>- Returns: <number>
ByteString parser with predefined configuration. Result of createSizeParser
.
RelativeSizer: Function
size
: <HybridByte>- Returns: <ByteString>
HybridByte parser with predefined configuration. Result of createRelativeSizer
.
ParsedUnit: Object
iec
: <boolean> Whether or not the byte is represented under the IEC standard i.e in terms of 1024.true
in'7 TiB'
false
in'3 TB'
type
: <string> Whether the size is represented as bits(b) or bytes(B).'b'
in'499Yb'
'B'
in'7 MB'
bits
: <boolean> Whether or not the size is specifically represented as abit
.true
in84 Yb
false
in278.58 KB
bytes
: <boolean> Whether or not the size is specifically represented as abyte
.true
in92 EB
false
in28 Mb
unit
: <UnitString> Re-parsed UnitString ofinputUnit
, fixing formatting.'TB'
if'TB'
'Gib'
if'gib'
inputUnit
: <UnitString> The unparsed String as was provided.'47TB'
in'47TB'
prefix
: <string> The prefix of the size string.'K'
in'KB'
prefixIndex
: <number> The index of the size unit [See UnitString].3
in'GB'
ParsedByteString extends
ParsedUnit: Object
input
: <ByteString> The unparsed String as was provided.'47TB'
in'47TB'
value
: <number> The value for the size.83
in'83MB'
ParsedBytes extends
ParsedByteString: Object
input
: <HybridByte> The unparsed String as was provided.1024
in1024
'47TB'
in'47TB'
size
: <ByteString> The value for the size.83
in'83MB'
bytes
: <number> The value for the size.10485760
from'10 MiB'
class ByteUnitObject
bytes
: <number> Internal byte value.
Wrap a HybridByte in a chainable, transformative object.
1new ByteUnitObject('10 MiB') 2// << ByteUnitObject { bytes: 10485760 } 3new ByteUnitObject('10 MiB').add('20 MiB') 4// << ByteUnitObject { bytes: 31457280 } 5new ByteUnitObject('10 MiB').add('20 MiB').toIECBytes() 6// << '30.00 MiB'
ByteUnitObject().add(bytes)
bytes
: <HybridByte|HybridByte[]> Byte(s) to subract from the root byte.- Returns: <ByteUnitObject>
Add byte(s) to the internal bytes, resulting in a new ByteUnitObject
object with the value
ByteUnitObject().subtract(bytes)
bytes
: <HybridByte|HybridByte[]> Subtract byte(s) from the internal bytes, resulting in a new ByteUnitObject object with the value.- Returns: <ByteUnitObject>
Subtract byte(s) from the internal bytes, resulting in a new ByteUnitObject
object with the value
ByteUnitObject().multiply(bytes)
bytes
: <HybridByte|HybridByte[]> Multiply byte(s) with the internal bytes, resulting in a new ByteUnitObject object with the value.- Returns: <ByteUnitObject>
Multiply byte(s) with the internal bytes, resulting in a new ByteUnitObject
object with the value
ByteUnitObject().divide(bytes)
bytes
: <HybridByte|HybridByte[]> Byte(s) to divide with.- Returns: <ByteUnitObject>
Divide internal bytes by byte(s) specified, resulting in a new ByteUnitObject
object with the value
ByteUnitObject().convertTo(unit)
unit
: <UnitString>- Returns: <ByteString>
Parse the internal byte into any unit, following the relativity.
1new ByteUnitObject('10 MiB') 2// >> ByteUnitObject { bytes: 10485760 } 3new ByteUnitObject('10 MiB').convertTo('MB') 4// >> '10.49 MB' 5new ByteUnitObject('10 MiB').add('50 MB').convertTo('KB') 6 '60485.76 KB'
ByteUnitObject().objectify(opts)
opts
: <ByteOptions>
Parse the internal bytes property to a byte object.
ByteUnitObject().checkInternalByteVal(bytes)
Method to check integrity of internal bytes. Throw if there's an error somewhere.
HybridByteRelations: Object
raw
: <HybridByte> The unparsed databits
: <ByteString> A relative bit parsing of the input HybridBytesize
: <number> The numeric byte format of the input HybridBytebytes
: <ByteString> A relative byte parsing of the input HybridByteiecBits
: <ByteString> A relative bit parsing of the input HybridByte under the IEC Specificationparsed
: <ByteString> A relative bit parsing of the input HybridByte under the IEC SpecificationiecBytes
: <ByteString> A relative byte parsing of the input HybridByte under the IEC Specification
More Examples
Check out some examples in the examples
folder
1$ node examples/index.js 'Hey, its 6GB, but my 8 TB flash drive is better' 2┌─────────┬──────────┬───────────┬────────┬───────────┬────────────┬────────────┬─────────────┐ 3│ (index) │ parsed │ size │ raw │ bytes │ iecBytes │ bits │ iecBits │ 4├─────────┼──────────┼───────────┼────────┼───────────┼────────────┼────────────┼─────────────┤ 5│ 0 │ [Object] │ '6.00 GB' │ '6GB' │ '6.00 GB' │ '5.59 GiB' │ '48.00 Gb' │ '44.70 Gib' │ 6│ 1 │ [Object] │ '8.00 TB' │ '8 TB' │ '8.00 TB' │ '7.28 TiB' │ '64.00 Tb' │ '58.21 Tib' │ 7└─────────┴──────────┴───────────┴────────┴───────────┴────────────┴────────────┴─────────────┘ 8$ node examples/parse.js 'The 10GB file was downloaded in 50MB/s' 9The 10737418240 file was downloaded in 52428800/s 10$ node examples/relative.js mb '10GiB, 1mb 6 gb' 1185899.35 Mb, 1.00 Mb 6000.00 Mb 12$ node examples/random.js 10 // Parse 10 random bytes 13[tabular data] 14$ node examples/extract.js 'Hey, its 6GB, but my 8 TB flash drive is better' 15┌─────────┬────────┬───────────────┐ 16│ (index) │ size │ bytes │ 17├─────────┼────────┼───────────────┤ 18│ 0 │ '6GB' │ 6000000000 │ 19│ 1 │ '8 TB' │ 8000000000000 │ 20└─────────┴────────┴───────────────┘
Features
Compatible with all versions of NodeJS (tested from v1.8.4)
1$ nvm exec v1.8.4 node -pe 'require(".")(3748587)' 2"3.75 MB" 3$ nvm exec v5.12.0 node -pe 'require(".").parseSize("476 TiB")' 4523367534821376 5$ nvm exec v11.10.0 node -e ' 6 let xbytes = require("./dist"); 7 let str = "My 10GB drive transmits at 250MiB/sec" 8 console.log(str.replace(xbytes.globalByteFilter, xbytes.relative.bits)) 9' 10"My 80.00 Gb drive transmits at 2.10 Gb/sec"
Decimal parsing
1xbytes(524334545.847775856); // 524.33 MB 2xbytes.parseSize('665.284 TiB'); // 731487493773328.4 3 4xbytes(.24283884748955); // 0.24 B 5xbytes.parseSize('.295 MB'); // 295000
Parse human readable sizes in binary (IEC) format to bytes
1xbytes.parseSize('1 MiB'); // 1048576
Parse byte values to extended human readable sizes
1xbytes(50000000, {short: false}); // '50.00 MegaBytes'
Match or extract ByteStrings in a string
1let data = 'My 16GB flash drive has a 4GB Zip Archive and a 5MB JavaScript file'; 2xbytes.extractBytes(data); 3 //> [ '16GB', '4GB', '5MB' ] 4data.match(xbytes.globalByteFilter) 5 //> [ '16GB', '4GB', '5MB' ] 6data.replace(xbytes.globalByteFilter, xbytes.parseSize) 7 //> 'My 16000000000 flash drive has a 4000000000 Zip Archive and a 5000000 JavaScript file' 8data.replace(xbytes.globalByteFilter, xbytes.createSizeParser({ iec: false })) 9 //> 'My 17179869184 flash drive has a 4294967296 Zip Archive and a 5242880 JavaScript file'
Decimal and Binary Unit Relativity
1import { parseSize, relative } from 'xbytes'; 2 3relative('35 TiB').bits // '307.86 Tb' 4relative('35 TiB').bytes // '38.48 TB' 5relative('35 TiB').iecBits // '280.00 Tib' 6relative('35 TiB').iecBytes // '35.00 TiB' 7 8parseSize(relative('35 TiB', {fixed: 20}).bits); 9 //> 38482906972160 10parseSize(relative('35 TiB', {fixed: 20}).bytes); 11 //> 38482906972160 12parseSize(relative('35 TiB', {fixed: 20}).iecBits); 13 //> 38482906972160 14parseSize(relative('35 TiB', {fixed: 20}).iecBytes); 15 //> 38482906972160
Development
Building
Feel free to clone, use in adherance to the license and perhaps send pull requests
1git clone https://github.com/miraclx/xbytes.git 2cd xbytes 3npm install 4# hack on code 5npm run build 6npm test
Testing
Tests are executed with Jest. To use it, simple run npm install
, it will install
Jest and its dependencies in your project's node_modules
directory followed by npm run build
and finally npm test
.
To run the tests:
1npm install 2npm run build 3npm test
License
Apache 2.0 © Miraculous Owonubi (@miraclx) <omiraculous@gmail.com>
No vulnerabilities found.
Reason
no vulnerabilities detected
Reason
license file detected
Details
- Info: : LICENSE:1
Reason
tokens are read-only in GitHub workflows
Reason
no dangerous workflow patterns detected
Reason
all dependencies are pinned
Details
- Info: GitHub-owned GitHubActions are pinned
- Info: Third-party GitHubActions are pinned
- Info: Dockerfile dependencies are pinned
- Info: no insecure (not pinned by hash) dependency downloads found in Dockerfiles
- Info: no insecure (not pinned by hash) dependency downloads found in shell scripts
Reason
no binaries found in the repo
Reason
5 commit(s) out of 30 and 1 issue activity out of 2 found in the last 90 days -- score normalized to 5
Reason
GitHub code reviews found for 5 commits out of the last 30 -- score normalized to 1
Details
- Warn: no reviews found for commit: 1257c8fd4c59d8ca3631e46885198090fb08bc1f
- Warn: no reviews found for commit: 3e94e7ce7413628fd44609e42e25ec58d190cf73
- Warn: no reviews found for commit: b6172a6c0e4d4329eca7ce580fcf9c26ddda2e0f
- Warn: no reviews found for commit: 885800afc9d8c5ea8b853b2ecb01b391cac5ce44
- Warn: no reviews found for commit: 5f1b1af646467b18d3c02c6d1d80f3c782b56cac
- Warn: no reviews found for commit: dea4404874faabe694993abe14f988e52dc90045
- Warn: no reviews found for commit: eb839b715429b0a7435dfd84388dd516c52ad4c0
- Warn: no reviews found for commit: 2a48ae4558a36005ee9dc18ea908211d0d58174c
- Warn: no reviews found for commit: b506485a292b9443ac42b98a496ff74aa9e116ee
- Warn: no reviews found for commit: af800988f5f9a0f563beaf07acf245bc2dfa2e6a
- Warn: no reviews found for commit: 738c9c48f940f1ec700777ab54038fe762835719
- Warn: no reviews found for commit: 982a4b48421adbf4a35a6d56bcbddc672e42cfc6
- Warn: no reviews found for commit: 40caaeb497d374a492b090852b2dfd9b1cda8304
- Warn: no reviews found for commit: 862e4e3d62224dc5f5d2088daaa7e54ca99593e3
- Warn: no reviews found for commit: 745e0cfee5984b8c253e7a50b920940bd0f82991
- Warn: no reviews found for commit: e99e2b658250cee48fd1183764495fd8686c224e
- Warn: no reviews found for commit: 5384a0566e9aef73c90ca6fded15f9910fffb6b6
- Warn: no reviews found for commit: 5202ad79af81645db8dcb3f99a7e9c7567412a2c
- Warn: no reviews found for commit: 93358c99aa1be781e374a8c470019af0cbe462a8
- Warn: no reviews found for commit: 716c9b91e24d5e45f932b0e4d53a92ad83f2568f
- Warn: no reviews found for commit: 04041870847cc7c677b4c6d99e8c624b218ecc59
- Warn: no reviews found for commit: 94a5ef78afad89bed97226273991cfd05b13b951
- Warn: no reviews found for commit: ec1197ac9db4d986ff992dffe040c6f554c674e2
- Warn: no reviews found for commit: e4b2888fdde056c11e36ab2194debf7d45870343
- Warn: no reviews found for commit: 524467d813e435e4388339c05593922a1e6e2d6d
Reason
no badge detected
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
no update tool detected
Details
- Warn: dependabot config file not detected in source location. We recommend setting this configuration in code so it can be easily verified by others.
- Warn: renovatebot config file not detected in source location. We recommend setting this configuration in code so it can be easily verified by others.
Reason
project is not fuzzed
Reason
security policy file not detected
Score
5.4
/10
Last Scanned on 2022-08-15
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