DEV Community

Mhammed Talhaouy
Mhammed Talhaouy

Posted on

Simplify Numbers with Aveta: A Handy JavaScript Library

Have you ever wanted to convert large numbers into concise, human-readable formats in your JavaScript projects? Say hello to Aveta—a lightweight library that makes formatting numbers effortless!

Aveta Logo

Why Aveta?

Aveta helps you turn clunky numbers into simple, human-readable formats like 10K, 1.2M, or 3.45B. It's perfect for dashboards, charts, and anywhere clean data presentation is essential.

Input vs. Output

Input Output
6000 '6K'
10000 '10K'
42500 '42.5K'
1250000 '1.25M'

Installation

Add Aveta to your project using npm or Yarn:

# With npm
npm install aveta

# With Yarn
yarn add aveta
Enter fullscreen mode Exit fullscreen mode

How to Use Aveta

Aveta is flexible and supports various formatting options. Here's a quick overview:

Basic Usage

import aveta from 'aveta';

console.log(aveta(8700)); // '8.7K'
Enter fullscreen mode Exit fullscreen mode

Advanced Options

Aveta offers customization for precision, separator style, unit case, and more:

import aveta from 'aveta';

// Customize digits and lowercase units
console.log(
  aveta(123456, {
    digits: 3,
    lowercase: true,
  })
); // '123k'

// Adjust precision and add a separator
console.log(
  aveta(2048000, {
    precision: 2,
    separator: ',',
  })
); // '2,048M'

// Use custom units
console.log(
  aveta(1440000, {
    units: ['B', 'KB', 'MB', 'GB', 'TB'],
    space: true,
  })
); // '1.44 MB'
Enter fullscreen mode Exit fullscreen mode

Reverse Formatting with AvetaReverse

Need to convert a human-readable number back into its original form? Use avetaReverse:

import { avetaReverse } from 'aveta';

console.log(avetaReverse('8.7K')); // 8700
console.log(avetaReverse('4.57k')); // 4570
console.log(avetaReverse('2.48m')); // 2480000
Enter fullscreen mode Exit fullscreen mode

Command-Line Interface

Run Aveta directly in your terminal:

$ aveta 234000
234K

# Or use npx
$ npx aveta 1234567
1.23M
Enter fullscreen mode Exit fullscreen mode

For CLI options, type:

aveta --help
Enter fullscreen mode Exit fullscreen mode

Why Choose Aveta?

  1. Lightweight: Add minimal overhead to your project.
  2. Customizable: Tailor formats to your specific needs.
  3. User-Friendly: CLI support makes quick testing a breeze.
  4. Comprehensive: Both forward and reverse formatting options.

This library is crafted with ❤️ by Me.

Contributions are welcome—join us on GitHub to make Aveta even better!

Top comments (0)