npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bytes-pro

v1.0.5

Published

Utility to parse a string bytes (ex: 1 ZiB) to bytes (1.1805916207174113e+21) and vice-versa.

Downloads

13

Readme

Bytes utility

Coverage Status

Utility to parse a string bytes (ex: 1 ZiB) to bytes (1.1805916207174113e+21) and vice-versa.

Installation

$ npm install bytes/pro | yarn install bytes/pro

Usage

import bytes from 'bytes/pro';

bytes.format(number value, [options]): string | string[] | null

Format the given value in bytes into a string. If the value is negative, it is kept as such. If it is a float, it is rounded. If options contain splitUnit, string array will be returned.

Arguments

| Name | Type | Description | | ------- | -------- | ------------------------ | | value | number | Number value to format or string value to parse | | options | Object | Conversion options for format |

Options

| Property | Type | Description | | -------- | ---- | ----------- | | decimalPlaces | numberundefined | Maximum number of decimal places to include in output. Default value to 2;Use with fixedDecimals: true | | fixedDecimals | booleanundefined | Whether to always display the maximum number of decimal places. Default value to false; Use with decimalPlaces | | thousandsSeparator | stringundefined | Example of values: ' ', ',' and '.'... Default value to ''. Use with toUnit | | toUnit | "B" | "KB" | "MB" | "GB" | "TB" | "PB" | "EB" | "ZB" | Convert to specified byte unit | | unitSeparator | stringundefined | Separator to use between number and unit. Default value to ''.Use with splitUnit: false | | withoutFloat | booleanundefined | Parse size without float:If it cannot be converted to integer byte value, the value displayed in B unit | | splitUnit | boolean | Whether to split byte values and units | | capacityBase | number | undefined | Conversion base ,It can be set to 1024 or 1000, Default value to 1024 |

Returns

| Name | Type | Description | | -------- | ---- | ----------- | | results | stringstring[]null | Return null upon error. string value ; or string array value. |

Example

// fixedDecimals: true
bytes.format(1024 * 1024, { fixedDecimals: true });
// output: '1.00MiB'

// fixedDecimals: true  + decimalPlaces: number
bytes.format(1024 * 1024, {
  fixedDecimals: true,
  decimalPlaces: 3,
});
// output: '1.000MiB'

// toUnit: "B" | "KB" | "MB" | "GB" | "TB" | "PB" | "EB" | "ZB"
bytes.format(1024 * 1024 * 1024, {
  toUnit: 'B',
});
// output: '1073741824B'

// thousandsSeparator: string + toUnit: "B" | "KB" | "MB" | "GB" | "TB" | "PB" | "EB" | "ZB"
bytes.format(1024 * 1024 * 1024, {
  thousandsSeparator: ',', // Thousands separator
  toUnit: 'B',
});
// output: '1,073,741,824B'

// unitSeparator: string
bytes.format(1024 * 1024, { unitSeparator: ' ' });
// output: '1 MiB'

// withoutFloat: true
bytes.format(1024 + 1, {
  withoutFloat: true,
});
// output: 1025B
bytes.format(1024, {
  withoutFloat: true,
});
// output: 1KiB

// splitUnit: false | true
bytes.format(1024 * 1024, { splitUnit: false });
// output: '1MiB'
bytes.format(1024 * 1024, { splitUnit: true });
// output: ['1', 'MiB']

// capacityBase: 1024 | 1000
bytes.format(1000 * 1000, { capacityBase: 1024 }) | format(1000 * 1000);
// output: '976.56KiB'
bytes.format(1000 * 1000, { capacityBase: 1000 });
// output: '1MiB'

// if value is not isFinite, return null;
// output: null

bytes.parse(string | number value): number | null

Parse the string value into an integer in bytes. If no unit is given, or value is a number, it is assumed the value is in bytes.

Supported units and abbreviations are as follows and are case-insensitive:

  • b for bytes
  • kb for kilobytes
  • mb for megabytes
  • gb for gigabytes
  • tb for terabytes
  • pb for petabytes
  • eb for exabytes
  • zb for zetbytes

The units are in powers of two, not ten. This means 1kib = 1024b according to this parser.

Arguments

| Name | Type | Description | | ----- | -------- | ---------------- | | value | string | String to parse. |

Options

| Name | Type | Description | | ----- | -------- | ---------------- | | convert | boolean | undefined | Convert decimal to binary, Use With capacityBase. | | capacityBase | number | undefined | Conversion base ,It can be set to 1024 or 1000, Default value to 1024 | | needUnit | boolean | undefined | When it is true, you can use all options whose value is number |

Returns

| Name | Type | Description | | ------- | ---------------- | ------------------------------------------------- | | results | numbernull | Return null upon error. Value in bytes otherwise. |

Example

bytes.parse('10MB', { capacityBase: 1000, convert: false });
// output: 10000000

bytes.parse('10Mb', { convert: true }) | bytes.parse('10Mb');
// output: 10485760

bytes.parse('10Mib');
// output: 10485760

// if parse value isNaN, return null
// output: null

bytes.parseToUnit(string value, toUnit, capacityBase): string | null

Convert byte string to specified unit string.

Arguments

| Name | Type | Description | | ----- | -------- | ---------------- | | value | string | string value to parse | | toUnit | "B" | "KB" | "MB" | "GB" | "TB" | "PB" | "EB" | "ZB" | Convert to specified byte unit | | capacityBase | 1024 | 1000 | undefined | Conversion base ,It can be set to 1024 or 1000, Default value to 1024 |

Returns

| Name | Type | Description | | results | string | null | Convert to byte unit string |

Example

xbytes.parseToUnit('10TB', 'GB', 1024) | xbytes.parseToUnit('10TB', 'GB');
// output: '10000GB'

xbytes.parseToUnit('10TB', 'GB', 1000);
// output: '10000GB'

License

MIT