From 61059d867bdceb43d2e89a8c4df2ca6669b785b1 Mon Sep 17 00:00:00 2001 From: Pradeep567iaf <60265769+Pradeep567iaf@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:57:21 +0530 Subject: [PATCH 1/2] Create Binary To Decimal Converter.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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/ 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 --- Parsers/Binary To Decimal Converter.js | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Parsers/Binary To Decimal Converter.js diff --git a/Parsers/Binary To Decimal Converter.js b/Parsers/Binary To Decimal Converter.js new file mode 100644 index 0000000..f4e69bf --- /dev/null +++ b/Parsers/Binary To Decimal Converter.js @@ -0,0 +1,47 @@ +/* +activation_example:!converttoDecimal +regex:^!converttoDecimal +flags:i +*/ + +// get the user input in format !converttoDecimal binary_code for example - !converttoDecimal 1011 +var input = current.text.trim(); + + +// Extract the Binary expression from the input +var binaryCheckregx = /^[01]+$/; // regular expresion for valid binary code +var match = input.match(/!converttoDecimal (\d+)/); // check input format +var expression = match ? match[1] : ""; + + + +function evaluateBinaryExpression(binary_code) { + binary_code = binary_code.replace(/\s+/g, ''); // Remove whitespace + // execute the api to get the equivalent decimal value of binary + var converter = new sn_ws.RESTMessageV2(); + var apiurl = 'https://networkcalc.com/api/binary/' + binary_code; + converter.setEndpoint(apiurl); // set endpoint url + converter.setHttpMethod('GET'); // GET method + var response = converter.execute(); + var result = JSON.parse(response.getBody()); // parse the response object + return result; +} + +try{ + // check the valid binary code in if statement + if(binaryCheckregx.test(expression)){ + var result = evaluateBinaryExpression(expression); // call to evaluateBinaryExpression + var DecimalresultSlackMessage = "* Binary to Decimal Conversion :*\n" + "• *Binary value*: " + expression + "\n" + "• *Decimal value*: " + result.converted + "\n"; // slack markdown message format + new x_snc_slackerbot.Slacker().send_chat(current, DecimalresultSlackMessage , false); // display the output to user + } + else{ + new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid Binary code.", false); + } +} +catch(error){ + 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 +} + + + + From 05336be29787b51390d57fb42e13868bec9db457 Mon Sep 17 00:00:00 2001 From: Pradeep567iaf <60265769+Pradeep567iaf@users.noreply.github.com> Date: Mon, 4 Nov 2024 18:09:12 +0530 Subject: [PATCH 2/2] 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 and also remove the slack markdown and show output as normal text. --- Parsers/Binary To Decimal Converter.js | 55 ++++++++++++++------------ 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/Parsers/Binary To Decimal Converter.js b/Parsers/Binary To Decimal Converter.js index f4e69bf..d2a1732 100644 --- a/Parsers/Binary To Decimal Converter.js +++ b/Parsers/Binary To Decimal Converter.js @@ -1,47 +1,52 @@ /* activation_example:!converttoDecimal -regex:^!converttoDecimal +regex:!bin2dec flags:i */ -// get the user input in format !converttoDecimal binary_code for example - !converttoDecimal 1011 -var input = current.text.trim(); - +// get the user input in format !bin2dec binary_code for example - !bin2dec 00110101 11111111 +var input = current.text.trim(); // Extract the Binary expression from the input -var binaryCheckregx = /^[01]+$/; // regular expresion for valid binary code -var match = input.match(/!converttoDecimal (\d+)/); // check input format + +var binaryCheckregx = /^[01\s]+$/; // Regular expression to check if input contains only binary characters and spaces +var commandRegex = /^!bin2dec ([01\s]+)$/; // Extract the binary expression, including spaces between bytes. +var match = input.match(commandRegex); // Updated commandRegex to include spaces between binary code var expression = match ? match[1] : ""; function evaluateBinaryExpression(binary_code) { - binary_code = binary_code.replace(/\s+/g, ''); // Remove whitespace - // execute the api to get the equivalent decimal value of binary + // Remove whitespace + binary_code = binary_code.replace(/\s+/g, ''); + // execute the api to get the equivalent decimal value of binary var converter = new sn_ws.RESTMessageV2(); var apiurl = 'https://networkcalc.com/api/binary/' + binary_code; converter.setEndpoint(apiurl); // set endpoint url converter.setHttpMethod('GET'); // GET method - var response = converter.execute(); - var result = JSON.parse(response.getBody()); // parse the response object - return result; + var response = converter.execute(); + var result = JSON.parse(response.getBody()); // parse the response object + return result; } -try{ - // check the valid binary code in if statement - if(binaryCheckregx.test(expression)){ - var result = evaluateBinaryExpression(expression); // call to evaluateBinaryExpression - var DecimalresultSlackMessage = "* Binary to Decimal Conversion :*\n" + "• *Binary value*: " + expression + "\n" + "• *Decimal value*: " + result.converted + "\n"; // slack markdown message format - new x_snc_slackerbot.Slacker().send_chat(current, DecimalresultSlackMessage , false); // display the output to user - } - else{ - new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid Binary code.", false); - } -} -catch(error){ - 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 -} +try { + // check the valid binary code in if statement + if (binaryCheckregx.test(expression)) { + var result = evaluateBinaryExpression(expression); // call to evaluateBinaryExpression + + var DecimalresultSlackMessage = "Decimal Value: " + result.converted; + new x_snc_slackerbot.Slacker().send_chat(current, DecimalresultSlackMessage , false); // display the output to user + } else { + new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid Binary code.", false); + + } +} catch (error) { + + 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 +} + +