Skip to content

Commit 61059d8

Browse files
Create Binary To Decimal Converter.js
This parser is a Binary to Decimal Converter designed to parse user input in a specific format and convert binary values to decimal. By providing a command with a binary code, this parser uses an external API to fetch and return the decimal equivalent. Activation Example To use this converter, simply enter the command in this format: !converttoDecimal <binary_code> For example: !converttoDecimal 1011 Features Regex-based Command Parsing: The parser validates the input command using a regular expression, ensuring only binary codes are accepted. Binary Validation: It checks the user input for valid binary numbers. API Call for Conversion: It uses the https://networkcalc.com/api/binary/<binary_code> API to convert binary to decimal. Slackbot Integration: Once the conversion is complete, the decimal value is sent back to the user in formatted message. *Binary to Decimal Conversion:* • Binary value: 1011 • Decimal value: 11
1 parent 8d78707 commit 61059d8

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
activation_example:!converttoDecimal <binary_code>
3+
regex:^!converttoDecimal
4+
flags:i
5+
*/
6+
7+
// get the user input in format !converttoDecimal binary_code for example - !converttoDecimal 1011
8+
var input = current.text.trim();
9+
10+
11+
// 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
14+
var expression = match ? match[1] : "";
15+
16+
17+
18+
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
21+
var converter = new sn_ws.RESTMessageV2();
22+
var apiurl = 'https://networkcalc.com/api/binary/' + binary_code;
23+
converter.setEndpoint(apiurl); // set endpoint url
24+
converter.setHttpMethod('GET'); // GET method
25+
var response = converter.execute();
26+
var result = JSON.parse(response.getBody()); // parse the response object
27+
return result;
28+
}
29+
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+
}
44+
45+
46+
47+

0 commit comments

Comments
 (0)