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

size-rate

v0.4.0

Published

Format current and maximum bytes into a human-readable expression

Downloads

117

Readme

size-rate

npm version GitHub Actions Coverage Status

Format current and maximum bytes into a human-readable expression

const SizeRate = require('size-rate');

const sizeRate = SizeRate({max: 100_000_000});

sizeRate.format();
//=> '  0.00 MB / 100.00 MB'

sizeRate.set(75_049_821);

sizeRate.format();
//=> ' 75.05 MB / 100.00 MB'

sizeRate.set(100_000_000);

sizeRate.format(100_000_000);
//=> '100.00 MB / 100.00 MB'

This module is useful for printing progress to the same line with process.stdout.clearLine().

The format is designed to keep initial string length: it adds whitespaces to short values, keeps using the same unit and number of decimal spaces regardless of the current value.

Usually, without string length unification, you ought to get more restless output instead.

Installation

Use npm.

npm install size-rate

API

const SizeRate = require('size-rate');

class SizeRate(options)

options: Object
Return: Object

options

Options are directly passed to filesize.js with these differences:

Also you need to set the following option:

max

Type: number (non-negative finite integer)

Required. The maximum size in byte displayed as a denominator.

Instance properties

max

Type: number

The maximum size. Same as the max property passed to the constructor.

bytes

Type: number

The current size.

formatLength

Type: number

Length of a string format() method returns.

Instance methods

format()

Return: string

Create a string in the form ${bytes} ${unit_of_max} / ${max} ${unit_of_max} using bytes and max properties.

new SizeRate({max: 1024}).format();
//=> '0.00 kB / 1.02 kB'

new SizeRate({max: 1024, base: 2}).format();
//=> '0.00 KiB / 1.00 KiB'

set(currentBytes)

currentBytes: integer (non-negative finite integer)

Set the current size in byte displayed as a numerator.

const sizeRate = new SizeRate({max: 300_000_000, round: 1});

sizeRate.format();
//=> '  0.0 MB / 300.0 MB'

sizeRate.set(123_456_789);

sizeRate.format();
//=> '123.5 MB / 300.0 MB'

Note that the argument must not be larger than max property.

const sizeRate = new SizeRate({max: 10});

sizeRate.set(11);
// RangeError: Expected a number no larger than the max bytes (10), but got 11.

format(currentBytes)

currentBytes: integer (non-negative finite integer)
Return: string

Call set() method with a given argument, then call format() method.

const sizeRate = new SizeRate({max: 7000, base: 2});

// Same as sizeRate.set(500); sizeRate.format()
sizeRate.format(500);
//=> '0.49 KiB / 6.84 KiB'

sizeRate.bytes;
//=> 500

License

ISC License © 2018 - 2019 Watanabe Shinnosuke