Skip to content

Commit

Permalink
updated wordToNumber function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amruth-Vamshi committed Sep 13, 2023
1 parent b050bf1 commit 2d1726e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,8 @@ export class AppController {
}

if(type == 'Audio' && ['askingAadhaarNumber','askingOTP','askLastAaadhaarDigits','confirmInput2','confirmInput3','confirmInput4'].indexOf(botFlowService.state.context.currentState) != -1) {
console.log("checkthis")
console.log(prompt.inputTextInEnglish)
let number = wordToNumber(prompt.inputTextInEnglish)
console.log(number)
let isOTP = ['askingOTP','askLastAaadhaarDigits','confirmInput3','confirmInput4'].indexOf(botFlowService.state.context.currentState) != -1
let number = wordToNumber(prompt.inputTextInEnglish, isOTP?'number':'benId')
// let number = prompt.inputTextInEnglish.replace(/\s/g, '')
prompt.inputTextInEnglish = number.toUpperCase()
isNumber = true
Expand Down
56 changes: 35 additions & 21 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function isMostlyEnglish(text: string): boolean {
return englishCharacterPercentage >= 90;
}

export const wordToNumber = (input) => {
export const wordToNumber = (input,type='benId') => {
input = input.toLowerCase()
// Map of words to numbers
const wordToNum = {
Expand Down Expand Up @@ -80,31 +80,45 @@ export const wordToNumber = (input) => {
input = input.replace(word,wordToNum[word])
}

// Convert standalone words to numbers or alphabets
let sanitizedStr = input.split(' ').map(word => {
if (wordToNum[word.toLowerCase()] !== undefined) {
return wordToNum[word.toLowerCase()];
if(type=='benId'){
// Convert standalone words to numbers or alphabets
let sanitizedStr = input.split(' ').map(word => {
if (wordToNum[word.toLowerCase()] !== undefined) {
return wordToNum[word.toLowerCase()];
}
return word;
}).join('');

// Ensure the format: <2 alphabets><9 digits>
const formatRegex = /^([a-zA-Z]{2})(\d{9})$/;
if (!formatRegex.test(sanitizedStr)) {

if(/[a-zA-Z]+/.test(sanitizedStr.slice(0,2))) {
sanitizedStr = sanitizedStr.slice(0,2) + sanitizedStr.slice(2).replace(/[^0-9]/g, '');;
return sanitizedStr
// if(sanitizedStr.slice(2).length == 9) {
// return sanitizedStr
// }
}
return word;
}).join('');

// Ensure the format: <2 alphabets><9 digits>
const formatRegex = /^([a-zA-Z]{2})(\d{9})$/;
if (!formatRegex.test(sanitizedStr)) {

if(/[a-zA-Z]+/.test(sanitizedStr.slice(0,2))) {
sanitizedStr = sanitizedStr.slice(0,2) + sanitizedStr.slice(2).replace(/[^0-9]/g, '');;
return sanitizedStr
// if(sanitizedStr.slice(2).length == 9) {
// return sanitizedStr
// }

return sanitizedStr.replace(/[^\d]/g, '')
}
return sanitizedStr;
}else{
// Convert standalone words to numbers
let numStr = input.split(' ').map(word => {
if (wordToNum[word.toLowerCase()] !== undefined) {
return wordToNum[word.toLowerCase()];
}
return word;
}).join('');

return sanitizedStr.replace(/[^\d]/g, '')
}
// Remove any non-numeric characters
numStr = numStr.replace(/[^\d]/g, '');

return sanitizedStr;
return numStr;
}
}

export const encryptRequest = async (text:string) => {
try {
Expand Down

0 comments on commit 2d1726e

Please sign in to comment.