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

@tsmx/human-readable

v2.0.3

Published

Easily create human-readable strings from byte sizes, e.g. 17238 --> 17.24 kB. Supports decimal (MB,GB,..) and binary (MiB, GiB,..) units as well as user-defined conversion from/to other sizes.

Downloads

5,319

Readme

@tsmx/human-readable

License: MIT npm (scoped) node-current (scoped) Build Status Coverage Status

Easily create human-readable strings from byte sizes, e.g. 1723817.24 kB. Supports decimal (MB,GB,..) and binary (MiB, GiB,..) units as well as user-defined conversion from/to other sizes.

For details about the differences of decimal (SI) and binary (IEC) units please refer to Wikipedia.

Also check out the full documentation. Type declarations for seamless usage in TypeScript projects are included. If you need to use this package on the client-side in the browser using plain JavaScript, refer to this article.

Usage

const hr = require('@tsmx/human-readable');

hr.fromBytes(17238);
// '17.24 kB'

hr.fromBytes(17238, { mode: 'IEC' });
// '16.83 KiB'

hr.fromBytes(17238, { numberOnly: true });
// '17.24'

hr.fromBytes(17238, { fixedPrecision: 1 });
// '17.2 kB'

hr.fromBytes(17238, { fullPrecision: true });
// '17.238 kB'

hr.fromTo(17, 'GBYTE', 'KBYTE');
// '17000000 kB'

hr.fromTo(17, 'GBYTE', 'KBYTE', { mode: 'IEC' });
// '17825792 KiB'

hr.availableSizes();
// [ 'BYTE', 'KBYTE', 'MBYTE', 'GBYTE', 'TBYTE', 'PBYTE' ]

API

fromBytes(bytes, options)

Automatically creates a human-readable string out of a given number of bytes. E.g. 7125571.26 kB

bytes

Type: Number

Amount of bytes.

options

Type: Object

Optional.

mode

Type: String Default: none (use decimal mode)

Can be set to IEC to use binary conversion (factor 1.024) and units (KiB,MiB,...). If not set or to any other value, decimal conversion (factor 1.000) and units (kB, MB,...) are used.

numberOnly

Type: Boolean Default: false

If set to true, conversion only returns the number and omits the unit. Overrides noWhitespace.

fixedPrecision

Type: Number

If set the returned number string is formatted to the given fixed decimal places. If not set, the default behaviour of the conversion is to use a dynamic number of decimal places from zero up to two.

fullPrecision

Type: Boolean Default: false

If set to true, the returned number value will be presented with full available decimal places. Overrides fixedPrecision.

noWhitespace

Type: Boolean Default: false

If set to true, the whitespace between the number and unit string is omitted. E.g. 10MB instead of 10 MB.

fromTo(value, fromSize, toSize, options)

Converts a value from a given size unit to a human-readable string of the target size. E.g. converting Gigabytes to Megabytes.

value

Type: Number

The value to be converted.

fromSize

Type: String

The size value has. Must be one out of availableSizes.

toSize

Type: String

The size value should be converted to. Must be one out of availableSizes.

options

Type: Object

See options description under fromBytes.

availableSizes()

Returns an array of strings of all available sizes.