Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
remonhasanapu committed Nov 16, 2023
0 parents commit a1975d3
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# React En to Bn ( English to Bangla) Convertion

A utility package providing functions to convert English numerals and date-time to Bengali numerals and date-time representations in React applications.

## Installation

You can install this package via npm or yarn:

```bash
npm install react-en-bn
# or
yarn add react-en-bn
```

## Usage

### `toBN`
Converts English numerals to Bengali numerals.

```
import { toBN } from 'react-en-bn';
const currentDate = '07-10-2023'
const bnNumber = toBN(currentDate); // Output: '০৭-১০-২০২৩'
```

### `toBnDateTime`
Converts English date-time to Bengali date-time representation.

```
import { toBnDateTime } from 'react-en-bn';
const enDateTime = '2023-11-14 12:45:00';
const bnDateTime = toBnDateTime(enDateTime); // Output: '২০২৩-১১-১৪, ১২:৪৫:০০ অপরাহ্ণ'
```

### `toBnMonthDateTime`

Converts English date-time to Bengali date-time with the month name.

```
import { toBnMonthDateTime } from 'react-en-bn';
const enDateTime = '2023-11-14 12:45:00';
const bnDateTimeMonth = toBnDateTimeMonth(enDateTime); // Output: '১৪ নভেম্বর ২০২৩, ১২ টা ৪৫ মিনিট অপরাহ্ণ'
```




20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "react-en-bn",
"version": "1.0.1",
"type": "module",
"source": "src/index.js",
"main": "dist/index.js",
"module": "dist/index.module.js",
"unpkg": "dist/index.umd.js",
"scripts": {
"build": "microbundle",
"dev": "microbundle watch"
},
"devDependencies": {
"microbundle": "^0.15.1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Remonhasan/react-en-bn.git"
}
}
71 changes: 71 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// General en to Bn

export const toBN = (n) => {
return n?.toString().replace(/\d/g, d => "০১২৩৪৫৬৭৮৯"[d]);
}

// En to bn date time
export const toBnDateTime = (dateTimeString) => {
const numbersToBn = {
'0': '০', '1': '১', '2': '২', '3': '৩', '4': '৪', '5': '৫', '6': '৬', '7': '৭', '8': '৮', '9': '৯'
};

const [datePart, timePart] = dateTimeString.split(' ');
const [year, month, day] = datePart.split('-');
const [hour, minute, second] = timePart.split(':');

const bnDate = `${day.replace(/\d/g, d => numbersToBn[d] || d)}-${month.replace(/\d/g, d => numbersToBn[d] || d)}-${year.replace(/\d/g, d => numbersToBn[d] || d)}`;

let bnTime = '';
if (hour === '00') {
bnTime = '১২';
} else if (parseInt(hour, 10) <= 12) {
bnTime = parseInt(hour, 10).toString().replace(/\d/g, d => numbersToBn[d] || d);
} else {
bnTime = (parseInt(hour, 10) - 12).toString().replace(/\d/g, d => numbersToBn[d] || d);
}

bnTime += `:${minute.replace(/\d/g, d => numbersToBn[d] || d)}:${second.replace(/\d/g, d => numbersToBn[d] || d)}`;

// Add AM or PM in Bengali
const amPM = parseInt(hour, 10) >= 12 ? 'অপরাহ্ণ' : 'পূর্বাহ্ণ';
bnTime += ` ${amPM}`;

return `${bnDate} ${bnTime}`;
}

// En to bn date time with month
export const toBnMonthDateTime = (n) => {
const numbersToBn = {
'0': '০', '1': '১', '2': '২', '3': '৩', '4': '৪', '5': '৫', '6': '৬', '7': '৭', '8': '৮', '9': '৯'
};

const monthsInBengali = {
'01': 'জানুয়ারী', '02': 'ফেব্রুয়ারী', '03': 'মার্চ', '04': 'এপ্রিল',
'05': 'মে', '06': 'জুন', '07': 'জুলাই', '08': 'আগস্ট',
'09': 'সেপ্টেম্বর', '10': 'অক্টোবর', '11': 'নভেম্বর', '12': 'ডিসেম্বর'
};

const [datePart, timePart] = dateTimeString.split(' ');
const [year, month, day] = datePart.split('-');
const [hour, minute] = timePart.split(':');

const bnDate = `${parseInt(day, 10).toString().replace(/\d/g, d => numbersToBn[d] || d)} ${monthsInBengali[month]} ${parseInt(year, 10).toString().replace(/\d/g, d => numbersToBn[d] || d)}`;

let bnTime = '';
if (hour === '00') {
bnTime = '১২';
} else if (parseInt(hour, 10) <= 12) {
bnTime = parseInt(hour, 10).toString().replace(/\d/g, d => numbersToBn[d] || d);
} else {
bnTime = (parseInt(hour, 10) - 12).toString().replace(/\d/g, d => numbersToBn[d] || d);
}

bnTime += ` টা ${parseInt(minute, 10).toString().replace(/\d/g, d => numbersToBn[d] || d)} মিনিট`;

// Add AM or PM in Bengali
const amPM = parseInt(hour, 10) >= 12 ? 'অপরাহ্ণ' : 'পূর্বাহ্ণ';
bnTime += ` ${amPM}`;

return `${bnDate}, ${bnTime}`;
}

0 comments on commit a1975d3

Please sign in to comment.