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

@visulima/humanizer

v1.0.14

Published

Humanizer is a library for humanizing data in a human-readable form.

Downloads

2,997

Readme

typescript-image npm-image license-image



Install

npm install @visulima/humanizer
yarn add @visulima/humanizer
pnpm add @visulima/humanizer

Usage

Bytes

Convert bytes to human-readable strings and vice versa: 1024 → 1KB and 1KB → 1024

import { formatBytes, p } from "@visulima/humanizer";

console.log(formatBytes(123412341, { decimals: 2 })); // "117.70 MB"
console.log(parseBytes("117.70 MB")); // 123417395.2

// Localization support in both directions
console.log(formatBytes(123412341, { decimals: 2, locale: "de" })); // "117,70 MB"
console.log(parseBytes("117,70 MB", { locale: "de" })); // 123417395.2

// Use a specified unit
console.log(formatBytes(123412341, { decimals: 2, unit: "KB" })); // "120,519.86 KB"

// Use a long unit
console.log(formatBytes(123412341, { decimals: 2, unit: "GB", long: true })); // "0.11 Gigabytes"

// Use a differnet base
console.log(formatBytes(123412341, { decimals: 2, base: 10 })); // "123.41 MB"

Supported locales

Default: "en".

formatBytes and parseBytes supports the following locales:

Duration

duration use a modified version of HumanizeDuration.

I have the time in milliseconds and I want it to become "30 minutes" or "3 days, 1 hour".

import { duration } from "@visulima/humanizer";

duration(3000);
// => "3 seconds"

duration(2250);
// => "2.25 seconds"

duration(97320000);
// => "1 day, 3 hours, 2 minutes"

Options

You can change the settings by passing options as the second argument.

units

Array of possible units to use. Units are y, mo, w, d, h, m, s, and ms.

Units are skipped if their count is zero. For example, if you pass a duration of 1000 and units ["h", "m", "s"], the output will be "1 second".

Must be in descending order of unit size. For example, ["h", "m"] is valid but ["m", "h"] is not.

Default: ["y", "mo", "w", "d", "h", "m", "s"]

duration(69000, { units: ["h", "m", "s", "ms"] });
// => "1 minute, 9 seconds"

duration(3600000, { units: ["h"] });
// => "1 hour"

duration(3600000, { units: ["m"] });
// => "60 minutes"

duration(3600000, { units: ["d", "h"] });
// => "1 hour"

largest

Integer representing the maximum number of units to use.

Default: Infinity

duration(1000000000000);
// => "31 years, 8 months, 1 week, 19 hours, 46 minutes, 40 seconds"

duration(1000000000000, { largest: 2 });
// => "31 years, 8 months"

round

A boolean that, if true, rounds the smallest unit.

Default: false

duration(1200);
// => "1.2 seconds"

duration(1200, { round: true });
// => "1 second"

duration(1600, { round: true });
// => "2 seconds"

delimiter

String to display between units.

Default: ", " in most languages, " ﻭ " for Arabic

duration(22140000);
// => "6 hours, 9 minutes"

duration(22140000, { delimiter: " and " });
// => "6 hours and 9 minutes"

spacer

String to display between the count and the word.

Default: " "

duration(260040000);
// => "3 days, 14 minutes"

duration(260040000, { spacer: " whole " });
// => "3 whole days, 14 whole minutes"

decimal

String to display between the integer and decimal parts of a count, if relevant.

Default depends on the language.

duration(1200);
// => "1.2 seconds"

duration(1200, { decimal: " point " });
// => "1 point 2 seconds"

conjunction

String to include before the final unit.

You can also set serialComma to false to eliminate the final comma.

Default: ""

duration(22140000, { conjunction: " and " });
// => "6 hours and 9 minutes"

duration(22141000, { conjunction: " and " });
// => "6 hours, 9 minutes, and 1 second"

duration(22140000, { conjunction: " and ", serialComma: false });
// => "6 hours and 9 minutes"

duration(22141000, { conjunction: " and ", serialComma: false });
// => "6 hours, 9 minutes and 1 second"

maxDecimalPoints

Integer that defines the maximum number of decimal points to show, if relevant. If undefined, the count will be converted to a string using Number.prototype.toString().

This does not round any numbers. See the round option.

Default: undefined

duration(8123.456789);
// => "8.123456789 seconds"

duration(8123.456789, { maxDecimalPoints: 3 });
// => "8.123 seconds"

duration(8100, { maxDecimalPoints: 99 });
// => "8.1 seconds"

duration(8000, { maxDecimalPoints: 99 });
// => "8 seconds"

duration(7999, { maxDecimalPoints: 2 });
// => "7.99 seconds"

digitReplacements

Array of ten strings to which will replace the numerals 0-9. Useful if a language uses different numerals.

Default: undefined for most languages, ["۰", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"] for Arabic

duration(1234);
// => "1.234 seconds"

duration(1234, {
    digitReplacements: ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
});
// => "One.TwoThreeFour seconds"

unitMeasures

Use this option with care. It is an advanced feature.

Object used to customize the value used to calculate each unit of time. Most useful when you want to update the length of years or months, which have ambiguous lengths.

Default: { y: 31557600000, mo: 2629800000, w: 604800000, d: 86400000, h: 3600000, m: 60000, s: 1000, ms: 1 }

duration(2629800000);
// => "1 month"

duration(2629800000, {
    unitMeasures: {
        y: 31557600000,
        mo: 30 * 86400000,
        w: 604800000,
        d: 86400000,
        h: 3600000,
        m: 60000,
        s: 1000,
        ms: 1,
    },
});
// => "1 month, 10 hours, 30 minutes"

language

Language for unit display. Accepts an ISO 639-1 code from one of the supported languages.

Default: "en".

import { duration } from "@visulima/humanizer";
import { durationLanguage as es } from "@visulima/humanizer/language/es";
import { durationLanguage as ko } from "@visulima/humanizer/language/ko";

duration(3000, { language: es });
// => "3 segundos"

duration(5000, { language: ko });
// => "5 초"
Supported languages

duration supports the following languages:

| Language | Code | | -------------------- | -------- | | Afrikaans | af | | Albanian | sq | | Amharic | am | | Arabic | ar | | Basque | eu | | Bengali | bn | | Bulgarian | bg | | Catalan | ca | | Central Kurdish | ckb | | Chinese, simplified | zh_CN | | Chinese, traditional | zh_TW | | Croatian | hr | | Czech | cs | | Danish | da | | Dutch | nl | | English | en | | Esperanto | eo | | Estonian | et | | Faroese | fo | | Farsi/Persian | fa | | Finnish | fi | | French | fr | | German | de | | Greek | el | | Hebrew | he | | Hindi | hi | | Hungarian | hu | | Icelandic | is | | Indonesian | id | | Italian | it | | Japanese | ja | | Kannada | kn | | Khmer | km | | Korean | ko | | Kurdish | ku | | Lao | lo | | Latvian | lv | | Lithuanian | lt | | Macedonian | mk | | Mongolian | mn | | Malay | ms | | Marathi | mr | | Norwegian | no | | Polish | pl | | Portuguese | pt | | Romanian | ro | | Russian | ru | | Serbian | sr | | Slovak | sk | | Slovenian | sl | | Spanish | es | | Swahili | sw | | Swedish | sv | | Tamil | ta | | Telugu | te | | Thai | th | | Turkish | tr | | Ukrainian | uk | | Urdu | ur | | Uzbek | uz | | Uzbek (Cyrillic) | uz_CYR | | Vietnamese | vi | | Welsh | cy |

Related

Bytes

  • pretty-bytes - Convert bytes to a human readable string: 13371.34 kB

Duration

  • HumanizeDuration - 361000 becomes "6 minutes, 1 second"
  • pretty-ms - Convert milliseconds to a human readable string: 133700000015d 11h 23m 20s

Supported Node.js Versions

Libraries in this ecosystem make the best effort to track Node.js’ release schedule. Here’s a post on why we think this is important.

Contributing

If you would like to help take a look at the list of issues and check our Contributing guidelines.

Note: please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Credits

License

The visulima humanizer is open-sourced software licensed under the MIT