Here I have used destructuring to extract the data from a message. I need to then use it outside of its scope. I duplicated the same variables/constants to made it exclusively outside of the scope. How can I avoid these duplicate variables and use the restructured variables/constants directly in the code?
let OTP2, amount2, account2, payee2;
let channel;
switch (sender) {
case 'SBIINB':
const regexSBINetBankingFundTransfer = /.*?OTP.*?Rs. (?<amount>\d+) .*?(?<account>\d+)\s+to\s+(?<payee>.*?)\s+is\s(?<OTP>\d+)/m;
if(regexSBINetBankingFundTransfer.test(message)) {
let { groups: { account, amount, payee, OTP } } = regexSBINetBankingFundTransfer.exec(message);
account2 = account; amount2 = amount; payee2 = payee;
notificationType = 'fundTransfer';
} else {
const regexSBINetBankingLogin = /.*OTP.*(?<OTP>\d{8}).*/m;
if(regexSBINetBankingLogin.test(message)) {
let { groups: { OTP } } = regexSBINetBankingLogin.exec(message);
if (OTP != null) {
notificationType = 'login';
OTP2 = OTP;
} else {
notificationType = 'uncategorized';
}
}
}
Edit: The assignment without declaration strategy didn't work