-
Notifications
You must be signed in to change notification settings - Fork 7
/
engine.js
342 lines (292 loc) · 7.55 KB
/
engine.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
const balanceKeywords = [
'avbl bal',
'available balance',
'a/c bal',
'available bal',
'avl bal',
];
const trnKeywords = ['debited', 'credited', 'payment', 'spent'];
function getTypeOfTransaction(message) {
const creditPattern = /(?:credited|credit|deposited)/gi;
const debitPattern = /(?:debited|debit|deducted)/gi;
const miscPattern = /(?:payment|spent)/gi;
if (typeof message !== 'string') {
message = message.join(' ');
}
if (debitPattern.test(message)) {
return 'debited';
} else if (creditPattern.test(message)) {
return 'credited';
} else if (miscPattern.test(message)) {
return 'debited';
}
return '';
}
function getCard(message) {
if (typeof message === 'string') {
message = processMessage(message);
}
const cardIndex = message.indexOf('card');
let card = { type: '', no: '' };
// Search for "card" and if not found return empty obj
if (cardIndex !== -1) {
card.no = message[cardIndex + 1];
card.type = 'card';
// If the data is false positive
// return empty obj
// Else return the card info
if (isNaN(Number(card.no))) {
return {
type: '',
no: '',
};
} else {
return card;
}
} else {
return { type: '', no: '' };
}
}
function extractBondedAccountNo(accountNo) {
const strippedAccountNo = accountNo.replace('ac', '');
if (isNaN(Number(strippedAccountNo))) {
return '';
} else {
return strippedAccountNo;
}
}
function getAccount(message) {
if (typeof message === 'string') {
message = processMessage(message);
}
let accountIndex = -1;
let account = {
type: '',
no: '',
};
for (const [index, word] of message.entries()) {
if (word === 'ac') {
if (index + 1 < message.length) {
const accountNo = trimLeadingAndTrailingChars(message[index + 1]);
if (isNaN(Number(accountNo))) {
// continue searching for a valid account number
continue;
} else {
accountIndex = index;
account.type = 'account';
account.no = accountNo;
return account;
}
} else {
// continue searching for a valid account number
continue;
}
} else if (word.includes('ac')) {
const extractedAccountNo = extractBondedAccountNo(word);
if (extractedAccountNo === '') {
continue;
} else {
accountIndex = index;
account.type = 'account';
account.no = extractedAccountNo;
return account;
}
}
}
// No occurence of the word "ac". Check for "card"
if (accountIndex === -1) {
return getCard(message);
}
}
function trimLeadingAndTrailingChars(str) {
const [first, last] = [str[0], str[str.length - 1]];
if (isNaN(Number(last))) {
str = str.slice(0, -1);
}
if (isNaN(Number(first))) {
str = str.slice(1);
}
return str;
}
function extractBalance(index, message, length) {
let balance = '';
let saw_number = false;
let invalid_char_count = 0;
let char = '';
while (index < length) {
char = message[index];
if ('0' <= char && char <= '9') {
saw_number = true;
// is_start = false;
balance += char;
} else {
if (saw_number === false) {
} else {
if (char === '.') {
if (invalid_char_count === 1) {
break;
} else {
balance += char;
invalid_char_count += 1;
}
} else if (char !== ",") {
break;
}
}
}
++index;
};
return balance;
}
function getBalance(message) {
if (typeof message !== 'string') {
return '';
}
message = processMessage(message).join(' ');
let index_of_keyword = -1;
let balance = '';
for (const word of balanceKeywords) {
index_of_keyword = message.indexOf(word);
if (index_of_keyword !== -1) {
index_of_keyword += word.length;
break;
} else {
continue;
}
}
// found the index of keyword, moving on to finding 'rs.' occuring after index_of_keyword
let index = index_of_keyword;
let index_of_rs = -1;
let nextThreeChars = message.substr(index, 3);
index += 3;
while (index < message.length) {
// discard first char
nextThreeChars = nextThreeChars.slice(1);
// add the current char at the end
nextThreeChars += message[index];
if (nextThreeChars === 'rs.') {
index_of_rs = index + 2;
break;
}
++index;
}
// no occurence of 'rs.'
if (index_of_rs === -1) {
return '';
}
balance = extractBalance(index_of_rs, message, message.length);
return balance;
}
function getMoneySpent(message) {
if (typeof message === 'string') {
message = processMessage(message);
}
const index = message.indexOf('rs.');
// If "rs." does not exist
// Return ""
if (index === -1) {
return '';
} else {
let money = message[index + 1];
money = money.replace(/,/g, '');
// If data is false positive
// Look ahead one index and check for valid money
// Else return the found money
if (isNaN(Number(money))) {
money = message[index + 2];
money = money.replace(/,/g, '');
// If this is also false positive, return ""
// Else return the found money
if (isNaN(Number(money))) {
return '';
} else {
return money;
}
} else {
return money;
}
}
}
function processMessage(message) {
// convert to lower case
message = message.toLowerCase();
// remove '-'
message = message.replace(/-/g, '');
// remove ':'
message = message.replace(/:/g, '');
// remove '/'
message = message.replace(/\//g, '');
// remove 'ending'
message = message.replace(/ending /g, '');
// replace 'x'
message = message.replace(/x|[*]/g, '');
// // remove 'is' 'with'
// message = message.replace(/\bis\b|\bwith\b/g, '');
// replace 'is'
message = message.replace(/is /g, '');
// replace 'with'
message = message.replace(/with /g, '');
// remove 'no.'
message = message.replace(/no. /g, '');
// replace all ac, acct, account with ac
message = message.replace(/\bac\b|\bacct\b|\baccount\b/g, 'ac');
// replace all 'rs' with 'rs. '
message = message.replace(/rs(?=\w)/g, 'rs. ');
// replace all 'rs ' with 'rs. '
message = message.replace(/rs /g, 'rs. ');
// replace all inr with rs.
message = message.replace(/inr(?=\w)/g, 'rs. ');
//
message = message.replace(/inr /g, 'rs. ');
// replace all 'rs. ' with 'rs.'
message = message.replace(/rs. /g, 'rs.');
// replace all 'rs.' with 'rs. '
message = message.replace(/rs.(?=\w)/g, 'rs. ');
// split message into words
message = message.split(' ');
// remove '' from array
message = removeItemAll(message, '');
return message;
}
function removeItemAll(arr, value) {
let i = 0;
while (i < arr.length) {
if (arr[i] === value) {
arr.splice(i, 1);
} else {
++i;
}
}
return arr;
}
function getTransactionInfo(message) {
if (!message || typeof message !== 'string') {
return {};
}
const processedMessage = processMessage(message);
const account = getAccount(processedMessage);
const balance = getBalance(processedMessage.join(' '));
const money = getMoneySpent(processedMessage);
let trn = '';
const isValid =
[balance, money, account.no].filter((x) => x !== '').length >= 2;
if (isValid) {
trn = getTypeOfTransaction(processedMessage);
}
return {
account,
balance,
money,
typeOfTransaction: trn,
};
}
module.exports = {
getTransactionInfo,
getAccount,
getBalance,
getCard,
getMoneySpent,
getTypeOfTransaction,
processMessage,
removeItemAll,
};