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

unitformat

v1.0.1

Published

A simple and flexible unit formatter

Downloads

27

Readme

UnitFormat.js

NPM Package Build Status MIT license

UnitFormat.js is a number formatter for human readable unit numbers, like 10km, 5GB, 17kHz, 220MW, ... with known metric prefixes.

Usage

The interface of UnitFormat.js is a single function that basically takes the number to be formatted and optionally the base unit, like "m" for meters:

let a = unitFormat(1000, "m") // 1km
let b = unitFormat(20000, "Hz") // 20kHz
let c = unitFormat(1000) // 1k
let d = unitFormat(0.02, "m") // 2cm 

Installation

Installing UnitFormat.js is as easy as cloning this repo or use the following command:

npm install unitformat

Available Parameters

The whole package consists of a single function unitFormat with the following signature

unitFormat(num, baseUnit="", prefixes="kMGTPE", base=10)
  • num: the number to be formatted
  • baseUnit: the base unit, like meters, Hertz, Joule, ...
  • prefixes: which prefixes should be used
  • base: The number base, default is 10, but 2 is also possible for bytes

Prefixes

The prefix parameter is a string list of single-character metric prefixes, like kMGTPE. For base 10 the following prefixes can be used:

  • E: Exa
  • P: Peta
  • T: Tera
  • G: Giga
  • M: Mega
  • k: Kilo
  • h: Hecto
  • d: Deci
  • c: Centi
  • m: Milli
  • u: Micro
  • n: Nano
  • p: Pico
  • f: Femto
  • a: Atto

And for base 2 the following prefixes are possible:

  • k: Kilo
  • M: Mega
  • G: Giga
  • T: Tera
  • P: Peta
  • E: Exa

Using UnitFormat.js with the browser

<script src="unitformat.js"></script>
<script>
  var x = unitFormat(100);
</script>

Testing

If you plan to enhance the library, make sure you add test cases and all the previous tests are passing. You can test the library with

npm test

Copyright and licensing

Copyright (c) 2016, Robert Eisele Dual licensed under the MIT or GPL Version 2 licenses.