DEV Community

Cover image for india-formatter: A Lightweight JavaScript Toolkit for Indian Developers ๐Ÿ‡ฎ๐Ÿ‡ณ
AJIT DEV
AJIT DEV

Posted on Fully Autonomous

india-formatter: A Lightweight JavaScript Toolkit for Indian Developers ๐Ÿ‡ฎ๐Ÿ‡ณ

๐Ÿ‡ฎ๐Ÿ‡ณ india-formatter: A Lightweight JavaScript Toolkit for Indian Developers

Working with Indian financial data in JavaScript applications often means handling different formatting conventions and validation requirements.

For example:

  • Formatting โ‚น12,34,567 instead of 1,234,567.
  • Converting monetary amounts into Indian English words.
  • Formatting amounts in Lakhs and Crores.
  • Validating PAN and GSTIN formats.
  • Handling Indian mobile number formats.

Developers often end up implementing these utilities separately across projects.

india-formatter brings these commonly needed Indian-specific utilities together in one lightweight, zero-dependency JavaScript and TypeScript package.

๐Ÿš€ Introducing india-formatter

india-formatter is designed to make Indian number formatting, INR currency conversion, and common Indian data validations easier to integrate into modern applications.

The package supports:

  • Node.js
  • React
  • Next.js
  • Browser applications
  • Edge runtimes
  • Cloudflare Workers

It also provides ESM and CommonJS builds with TypeScript definitions.

โœจ Features at a glance

Feature Description
INR Formatting Indian currency grouping
Compact Currency K, Lakh and Crore notation
Number to Words Indian English currency words
Number Formatting Indian comma separation
PAN Validation PAN format validation
GSTIN Validation GSTIN structure validation
Phone Validation Indian mobile number validation
Zero Dependencies No runtime dependencies

The package targets a gzipped bundle size below 3 KB.

๐Ÿ“ฆ Installation

Install the package using npm:

npm install india-formatter
Enter fullscreen mode Exit fullscreen mode

Other package managers are supported too:

yarn add india-formatter
Enter fullscreen mode Exit fullscreen mode
pnpm add india-formatter
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ฐ 1. Indian Currency Formatting

One of the main features is formatting currency according to the Indian numbering system.

import { formatINR } from 'india-formatter';

formatINR(1234567.89);
// โ‚น12,34,567.89

formatINR(150000, { decimals: 0 });
// โ‚น1,50,000

formatINR(500, { showSymbol: false });
// 500.00

formatINR(150000, { symbol: 'Rs. ' });
// Rs. 1,50,000.00
Enter fullscreen mode Exit fullscreen mode

Compact Currency Formatting

For dashboards, reports, and analytics interfaces, compact currency notation can be useful.

import { formatINRCompact } from 'india-formatter';

formatINRCompact(1500);
// โ‚น1.5 K

formatINRCompact(1500000);
// โ‚น15 Lakh

formatINRCompact(25000000);
// โ‚น2.5 Crore
Enter fullscreen mode Exit fullscreen mode

๐Ÿ—ฃ๏ธ 2. Convert Numbers into Indian English Words

Converting monetary values into words is useful when generating invoices, receipts, and financial documents.

import { toWordsINR } from 'india-formatter';

toWordsINR(125000);
// One Lakh Twenty Five Thousand Rupees Only

toWordsINR(10500.50);
// Ten Thousand Five Hundred Rupees and Fifty Paise Only

toWordsINR(0);
// Zero Rupees Only
Enter fullscreen mode Exit fullscreen mode

You can also customize the output:

toWordsINR(500, { showOnly: false });
// Five Hundred Rupees
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ข 3. Indian Number Formatting

Indian and international number grouping conventions are different.

import { formatIndianNumber } from 'india-formatter';

formatIndianNumber(1234567);
// 12,34,567

formatIndianNumber(1234567.89);
// 12,34,567.89

formatIndianNumber(1234567, 2);
// 12,34,567.00
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›ก๏ธ 4. PAN Validation

The package provides PAN format validation, including an optional strict mode for checking the fourth-character category.

import { isValidPAN } from 'india-formatter';

isValidPAN('ABCDE1234F');
// true

isValidPAN('12345ABCDE');
// false

isValidPAN('ABCPE1234F', { strict: true });
// true
Enter fullscreen mode Exit fullscreen mode

๐Ÿงพ 5. GSTIN Validation

Validate the structure of a 15-character GSTIN.

import { isValidGSTIN } from 'india-formatter';

isValidGSTIN('27ABCDE1234F1Z5');
// true

isValidGSTIN('invalid-gst');
// false
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฑ 6. Indian Mobile Number Validation

Indian mobile numbers can arrive in different formats, such as with country codes, prefixes, and separators.

import { isValidIndianPhone } from 'india-formatter';

isValidIndianPhone('9876543210');
// true

isValidIndianPhone('+91 98765 43210');
// true

isValidIndianPhone('+919876543210');
// true

isValidIndianPhone('09876543210');
// true

isValidIndianPhone('1234567890');
// false
Enter fullscreen mode Exit fullscreen mode

Important: These validators check formatting and structural rules. They do not establish the authenticity of government records or verify whether a phone number is active.

๐ŸŒ Built for Modern JavaScript Applications

The package provides:

  • Dual ESM and CommonJS support.
  • Full TypeScript definitions.
  • Native JavaScript and TypeScript implementation.
  • Zero runtime dependencies.
  • Support for browser and server-side environments.

This makes it suitable for applications built with Node.js, React, Next.js, and supported edge runtimes.

๐Ÿงช Development

Developers interested in exploring or contributing to the package can get started with:

# Clone the repository
git clone https://github.com/rahman4ktr/india-formatter.git

# Enter the project
cd india-formatter

# Install dependencies
npm install

# Run tests
npm test

# Build the package
npm run build
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Why Indian-Specific Utilities Matter

JavaScript provides many general-purpose formatting and validation capabilities, but application developers still need to account for country-specific conventions.

Indian currency grouping, Lakhs and Crores, monetary amounts in words, and common identification-number formats are everyday requirements in many Indian applications.

Having these utilities in one reusable package can reduce repeated implementation and make application code easier to maintain.

๐Ÿ”— Explore the Project

Want to see it in action or explore the source code?

๐Ÿค Open Source & Community

The project is maintained by M. Rahman.

Developers are welcome to explore the implementation, try the package, share feedback, and contribute improvements through the GitHub repository.

If you work with Indian financial data or build applications for Indian users, give india-formatter a try.

Happy coding! ๐Ÿš€


india-formatter โ€” Indian formatting utilities for modern JavaScript and TypeScript applications.

Top comments (0)