Skip to content

Commit 05336be

Browse files
Update Binary To Decimal Converter.js
Resolve the Issue raised 1) Please remove this capitalisation, as this adds confusion when compared to all other parsers. Additionally, it is not clear that this parser converts from binary to decimal. It might be worthwhile making it something like !bin2dec 2) It's not clear from other details about this parser, that this will not support multiple bytes (i.e. 00110101 11111111) or breaks in the binary 3) This does not use unordered lists so does not use slack markdown Now, this parser will accept the binary code with spaces and give output as a decimal value. Change the activation to !bin2dec <binary code> and also remove the slack markdown and show output as normal text.
1 parent 61059d8 commit 05336be

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed
Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
11
/*
22
activation_example:!converttoDecimal <binary_code>
3-
regex:^!converttoDecimal
3+
regex:!bin2dec
44
flags:i
55
*/
66

7-
// get the user input in format !converttoDecimal binary_code for example - !converttoDecimal 1011
8-
var input = current.text.trim();
9-
7+
// get the user input in format !bin2dec binary_code for example - !bin2dec 00110101 11111111
8+
var input = current.text.trim();
109

1110
// Extract the Binary expression from the input
12-
var binaryCheckregx = /^[01]+$/; // regular expresion for valid binary code
13-
var match = input.match(/!converttoDecimal (\d+)/); // check input format
11+
12+
var binaryCheckregx = /^[01\s]+$/; // Regular expression to check if input contains only binary characters and spaces
13+
var commandRegex = /^!bin2dec ([01\s]+)$/; // Extract the binary expression, including spaces between bytes.
14+
var match = input.match(commandRegex); // Updated commandRegex to include spaces between binary code
1415
var expression = match ? match[1] : "";
1516

1617

1718

1819
function evaluateBinaryExpression(binary_code) {
19-
binary_code = binary_code.replace(/\s+/g, ''); // Remove whitespace
20-
// execute the api to get the equivalent decimal value of binary
20+
// Remove whitespace
21+
binary_code = binary_code.replace(/\s+/g, '');
22+
// execute the api to get the equivalent decimal value of binary
2123
var converter = new sn_ws.RESTMessageV2();
2224
var apiurl = 'https://networkcalc.com/api/binary/' + binary_code;
2325
converter.setEndpoint(apiurl); // set endpoint url
2426
converter.setHttpMethod('GET'); // GET method
25-
var response = converter.execute();
26-
var result = JSON.parse(response.getBody()); // parse the response object
27-
return result;
27+
var response = converter.execute();
28+
var result = JSON.parse(response.getBody()); // parse the response object
29+
return result;
2830
}
2931

30-
try{
31-
// check the valid binary code in if statement
32-
if(binaryCheckregx.test(expression)){
33-
var result = evaluateBinaryExpression(expression); // call to evaluateBinaryExpression
34-
var DecimalresultSlackMessage = "* Binary to Decimal Conversion :*\n" + "• *Binary value*: " + expression + "\n" + "• *Decimal value*: " + result.converted + "\n"; // slack markdown message format
35-
new x_snc_slackerbot.Slacker().send_chat(current, DecimalresultSlackMessage , false); // display the output to user
36-
}
37-
else{
38-
new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid Binary code.", false);
39-
}
40-
}
41-
catch(error){
42-
new x_snc_slackerbot.Slacker().send_chat(current, "Oops! Facing Issue while converting Binary to Decimal with error" + error, false); //handling exception in try block
43-
}
32+
try {
33+
// check the valid binary code in if statement
34+
if (binaryCheckregx.test(expression)) {
35+
var result = evaluateBinaryExpression(expression); // call to evaluateBinaryExpression
36+
37+
var DecimalresultSlackMessage = "Decimal Value: " + result.converted;
38+
new x_snc_slackerbot.Slacker().send_chat(current, DecimalresultSlackMessage , false); // display the output to user
4439

4540

41+
} else {
42+
new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid Binary code.", false);
43+
44+
}
45+
} catch (error) {
46+
47+
new x_snc_slackerbot.Slacker().send_chat(current, "Oops! Facing Issue while converting Binary to Decimal with error" + error, false); //handling exception in try block
48+
}
49+
50+
4651

4752

0 commit comments

Comments
 (0)