1.0.3 • Published 7 months ago

bangla-num-converter v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

Bangla Number Converter

A TypeScript library that converts English numbers to Bengali (Bangla) numerals with support for formatting options.

Features

  • Convert numbers to Bengali numerals
  • Support for both number and string inputs
  • Preserve or remove commas in formatted numbers
  • Handle decimal numbers and negative values
  • Type-safe with full TypeScript support
  • Zero dependencies

Installation

npm install bangla-num-converter

Usage

Basic Usage

import enToBn from "bangla-num-converter";

// Converting numbers
console.log(enToBn(123)); // Returns: ১২৩
console.log(enToBn(1000.5)); // Returns: ১০০০.৫০
console.log(enToBn(-42)); // Returns: -৪২

// Converting strings with commas
console.log(enToBn("1,234,567")); // Returns: ১,২৩৪,৫৬৭

Formatting Options

You can control comma preservation in the output:

// With commas (default)
console.log(enToBn("1,234,567")); // Returns: ১,২৩৪,৫৬৭

// Without commas
console.log(enToBn("1,234,567", false)); // Returns: ১২৩৪৫৬৭

Return Types

The function returns different types based on the input:

// Numbers return as numbers
const result1 = enToBn(123); // Type: number, Value: 123

// Strings return as strings
const result2 = enToBn("1,234"); // Type: string, Value: "১,২৩৪"

Utility Functions

The package also exports utility functions for working with Bengali numerals:

import { isBengaliDigit, BENGALI_DIGITS } from "bangla-num-converter";

// Check if a character is a Bengali digit
console.log(isBengaliDigit("১")); // true
console.log(isBengaliDigit("1")); // false

// Access all Bengali digits
console.log(BENGALI_DIGITS); // ['০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯']

Input Validation

The library includes strict input validation:

  • Only accepts valid numbers, commas, decimal points, and negative signs
  • Throws error for multiple decimal points
  • Throws error for multiple negative signs
  • Throws error for invalid characters
// These will throw errors
enToBn("abc"); // Error: Invalid characters
enToBn("1.2.3"); // Error: Multiple decimal points
enToBn("--123"); // Error: Multiple negative signs
enToBn("123abc"); // Error: Invalid characters

API Reference

Main Function

function enToBn(
  number: number | string,
  preserveCommas: boolean = true
): number | string;

Parameters

  • number: The number or string to convert
  • preserveCommas (optional): Whether to preserve commas in the output (default: true)

Returns

  • Returns number if the input is a number
  • Returns string if the input is a string or contains formatting

Utility Functions

function isBengaliDigit(char: string): boolean;
  • Checks if a character is a Bengali numeral
const BENGALI_DIGITS: string[];
  • Array of all Bengali numerals (০-৯)

Error Handling

The library throws descriptive errors for invalid inputs:

try {
  enToBn("123@456");
} catch (error) {
  console.error(error.message);
  // "Input must only contain numbers, commas, periods, or hyphens"
}

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Chowdhury Tafsir Ahmed Siddiki

Support

If you find any bugs or have feature requests, please create an issue on GitHub.

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago