-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
370 lines (353 loc) · 9.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
const NEGATIVE = "ঋণাত্মক";
const DECIMAL = " দশমিক";
const HUNDRED = 100;
const THOUSAND = 1000;
const LAC = 100000;
const CORE = 10000000;
const BILLION = 1000000000;
const TRILLION = 1000000000000;
const QUADRILLION = 1000000000000000;
const MAX_SAFE_NUMBER = 9007199254740991;
// English To Bangla Number Map
const banglaNumberMap = {
1: "১",
2: "২",
3: "৩",
4: "৪",
5: "৫",
6: "৬",
7: "৭",
8: "৮",
9: "৯",
0: "০",
".": ".",
"-": "-",
"+": "+",
_: "_",
"/": "/",
};
// English To Bangla Days month Map
const banglaDateMap = {
1: "১",
2: "২",
3: "৩",
4: "৪",
5: "৫",
6: "৬",
7: "৭",
8: "৮",
9: "৯",
0: "০",
"-": "-",
_: "_",
"/": "/",
" ": " ",
":": ":",
january: "জানুয়ারি",
jan: "জানুয়ারি",
february: "ফেব্রুয়ারি",
feb: "ফেব্রুয়ারি",
march: "মার্চ",
mar: "মার্চ",
april: "এপ্রিল",
apr: "এপ্রিল",
may: "মে",
june: "জুন",
jun: "জুন",
july: "জুলাই",
jul: "জুলাই",
august: "আগস্ট",
aug: "আগস্ট",
september: "সেপ্টেম্বর",
sept: "সেপ্টেম্বর",
sep: "সেপ্টেম্বর",
october: "অক্টোবর",
oct: "অক্টোবর",
november: "নভেম্বর",
nove: "নভেম্বর",
nov: "নভেম্বর",
december: "ডিসেম্বর",
dec: "ডিসেম্বর",
saturday: "শনিবার",
sat: "শনি",
sunday: "রবিবার",
sun: "রবি",
monday: "সোমবার",
mon: "সোম",
tuesday: "মঙ্গলবার",
tues: "মঙ্গল",
wednesday: "বুধবার",
wed: "বুধ",
thursday: "বৃহস্পতিবার",
thu: "বৃহস্পতি",
thurs: "বৃহস্পতি",
friday: "শুক্রবার",
fri: "শুক্র",
am: "am",
pm: "pm",
};
// English to Bangla Word Map
const banglaWordMap = {
0: "শূন্য",
1: "এক",
2: "দুই",
3: "তিন",
4: "চার",
5: "পাঁচ",
6: "ছয়",
7: "সাত",
8: "আট",
9: "নয়",
10: "দশ",
11: "এগারো",
12: "বারো",
13: "তেরো",
14: "চৌদ্দ",
15: "পনের",
16: "ষোল",
17: "সতের",
18: "আঠার",
19: "উনিশ",
20: "বিশ",
21: "একুশ",
22: "বাইশ",
23: "তেইশ",
24: "চব্বিশ",
25: "পঁচিশ",
26: "ছাব্বিশ",
27: "সাতাশ",
28: "আটাশ",
29: "ঊনত্রিশ",
30: "ত্রিশ",
31: "একত্রিশ",
32: "বত্রিশ",
33: "তেত্রিশ",
34: "চৌত্রিশ",
35: "পঁয়ত্রিশ",
36: "ছত্রিশ",
37: "সাঁইত্রিশ",
38: "আটত্রিশ",
39: "ঊনচল্লিশ",
40: "চল্লিশ",
41: "একচল্লিশ",
42: "বিয়াল্লিশ",
43: "তেতাল্লিশ",
44: "চুয়াল্লিশ",
45: "পঁয়তাল্লিশ",
46: "ছিচল্লিশ",
47: "সাতচল্লিশ",
48: "আটচল্লিশ",
49: "ঊনপঞ্চাশ",
50: "পঞ্চাশ",
51: "একান্ন",
52: "বাহান্ন",
53: "তেপ্পান্ন",
54: "চুয়ান্ন",
55: "পঞ্চান্ন",
56: "ছাপ্পান্ন",
57: "সাতান্ন",
58: "আটান্ন",
59: "ঊনষাট",
60: "ষাট",
61: "একষট্টি",
62: "বাষট্টি",
63: "তেষট্টি",
64: "চৌষট্টি",
65: "পঁয়ষট্টি",
66: "ছেষট্টি",
67: "সাতষট্টি",
68: "আটষট্টি",
69: "উনসত্তুর",
70: "সত্তর",
71: "একাত্তর",
72: "বাহাত্তর",
73: "তেহাত্তুর",
74: "চুয়াত্তর",
75: "পঁচাত্তর",
76: "ছিয়াত্তর",
77: "সাতাত্তর",
78: "আটাত্তর",
79: "ঊনআশি",
80: "আশি",
81: "একাশি",
82: "বিরাশি",
83: "তিরাশি",
84: "চুরাশি",
85: "পঁচাশি",
86: "ছিয়াশি",
87: "সাতাশি",
88: "আটাশি",
89: "উননব্বই",
90: "নব্বই",
91: "একানব্বই",
92: "বিরানব্বই",
93: "তিরানব্বই",
94: "চুরানব্বই",
95: "পঁচানব্বই",
96: "ছিয়ানব্বই",
97: "সাতানব্বই",
98: "আটানব্বই",
99: "নিরানব্বই",
100: "শত",
1000: "হাজার",
100000: "লক্ষ",
10000000: "কোটি",
1000000000: "বিলিয়ন",
1000000000000: "ট্রিলিয়ন",
1000000000000000: "কোয়াড্রিলিয়ন",
};
// Utils Function
const isNumber = (str) => {
// Remove leading and trailing whitespace
let strTrim = str.toString().trim();
// Check if the trimmed string is not empty and is a valid number
return strTrim !== "" && !isNaN(strTrim);
};
// English to Bangla Word
function englishToBanglaWord(number) {
let remainder,
word,
words = arguments[1];
if (number === 0) {
return !words ? banglaWordMap[0] : words.join(" ").replace(/,$/, "");
}
if (!words) {
words = [];
}
if (number < 0) {
words.push(NEGATIVE);
number = Math.abs(number);
}
if (number < HUNDRED) {
remainder = 0;
word = banglaWordMap[number];
} else if (number < THOUSAND) {
remainder = number % HUNDRED;
word = banglaWordMap[Math.floor(number / HUNDRED)] + banglaWordMap[HUNDRED];
} else if (number < LAC) {
remainder = number % THOUSAND;
word =
banglaWordMap[Math.floor(number / THOUSAND)] +
" " +
banglaWordMap[THOUSAND];
} else if (number < CORE) {
remainder = number % LAC;
word = banglaWordMap[Math.floor(number / LAC)] + " " + banglaWordMap[LAC];
} else if (number < BILLION) {
remainder = number % CORE;
word =
englishToBanglaWord(Math.floor(number / CORE)) +
" " +
banglaWordMap[CORE];
} else if (number < TRILLION) {
remainder = number % BILLION;
word =
englishToBanglaWord(Math.floor(number / BILLION)) +
" " +
banglaWordMap[BILLION];
} else if (number < QUADRILLION) {
remainder = number % TRILLION;
word =
englishToBanglaWord(Math.floor(number / TRILLION)) +
" " +
banglaWordMap[TRILLION];
} else if (number <= MAX_SAFE_NUMBER) {
remainder = number % QUADRILLION;
word =
englishToBanglaWord(Math.floor(number / QUADRILLION)) +
" " +
banglaWordMap[QUADRILLION];
}
words.push(word);
return englishToBanglaWord(remainder, words);
}
// English to Bangla Number
const englishToBanglaNumber = (number) => {
{
if (!isNumber(number)) return "Not a valid number";
let toString = number.toString();
let toBanglaNumber = "";
for (let i = 0; i < toString.length; i++) {
const element = toString[i];
toBanglaNumber += banglaNumberMap[element];
}
return toBanglaNumber;
}
};
const singleNumberToWord = (number) => {
let result = "";
for (const char of number) {
result += `${banglaWordMap[char]} `;
}
return result;
};
// English Date Time to Bangla Date Time
const englishToBanglaDate = (string) => {
string.trim();
const lowerString = string.toLowerCase() + " ";
let result = "";
var regExp = /[a-z]/;
// Direct Match Check
if (banglaDateMap[lowerString]) {
return banglaDateMap[lowerString];
}
// If it contains words
else if (regExp.test(lowerString)) {
let charSet = "";
for (i = 0; i < lowerString.length; i++) {
const char = lowerString[i];
// Regx isChar Check
if (regExp.test(char)) {
charSet += char;
} else if (charSet) {
if (!banglaDateMap[charSet]) {
return "Invalid Date or Time";
}
result += banglaDateMap[charSet];
result += banglaDateMap[char];
charSet = "";
} else if (banglaDateMap[char]) {
result += banglaDateMap[char];
} else {
error.log("Invalid Date or Time");
return "Invalid Date or Time";
}
}
return result;
}
//Expected Regular Date Format
else {
for (const element of lowerString) {
result += banglaDateMap[element];
}
return result;
}
};
// Main Function Start
function toBangla() {
return {
// English to bangla Number
engToNumber: (number) => englishToBanglaNumber(number),
// English to bangla Word
engToWord: (number, currency) => {
if (!isNumber(number)) return "Not a valid number";
const numberToString = number.toString();
if (numberToString.includes(".")) {
const apart = numberToString.trim().split(".");
const firstPart = englishToBanglaWord(apart[0]);
const lastPart = currency
? englishToBanglaWord(apart[1].slice(0, 2))
: singleNumberToWord(apart[1]);
return currency
? `${firstPart} টাকা এবং ${lastPart} পয়সা`
: `${firstPart}${DECIMAL} ${lastPart}`;
}
return currency
? englishToBanglaWord(number) + " টাকা"
: englishToBanglaWord(number);
},
engToDate: (string) => englishToBanglaDate(string),
};
}
const bn = toBangla();
module.exports = bn;