From 3dde41413b94027739695e85e0f7d77288c657b2 Mon Sep 17 00:00:00 2001 From: Yourstruggle11 Date: Fri, 7 Oct 2022 12:29:29 +0530 Subject: [PATCH 1/3] =?UTF-8?q?`feat`:=20Added=20a=20`utility`=20func=20to?= =?UTF-8?q?=20conver=20`form=20data`=20to=20`json`=20=F0=9F=8E=8A?= =?UTF-8?q?=F0=9F=8E=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formDataToJson.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 formDataToJson.js diff --git a/formDataToJson.js b/formDataToJson.js new file mode 100644 index 0000000..9b4250e --- /dev/null +++ b/formDataToJson.js @@ -0,0 +1,22 @@ +function fromDataToJson(formData) { + const data = {}; + + for (const [key, value] of formData.entries()) { + if (Object.prototype.hasOwnProperty.call(data, key)) { + const oldValue = data[key]; + if (!Array.isArray(data[key])) { + data[key] = []; + data[key].push(oldValue); + } + + data[key].push(value); + + continue; + } + + data[key] = value; + } + + return data; +} + From 6b58f78fd828bff3651299bb017f25780877381f Mon Sep 17 00:00:00 2001 From: Souvik Sen <82510209+Yourstruggle11@users.noreply.github.com> Date: Tue, 3 Oct 2023 03:23:28 +0530 Subject: [PATCH 2/3] Create string_to_base64 --- string_to_base64 | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 string_to_base64 diff --git a/string_to_base64 b/string_to_base64 new file mode 100644 index 0000000..8a4018c --- /dev/null +++ b/string_to_base64 @@ -0,0 +1,55 @@ +def string_to_base64(input_string): + """ + Convert a string to its Base64 representation without using external packages. + + Args: + input_string (str): The input string to be converted. + + Returns: + str: The Base64-encoded representation of the input string. + """ + # Define the Base64 character mapping + base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + + # Initialize variables + result = [] + current_bits = 0 + num_bits = 0 + + # Convert the input string to bytes + bytes_data = input_string.encode('utf-8') + + # Iterate through each byte in the input data + for byte in bytes_data: + # Shift the current_bits left by 8 bits and add the new byte + current_bits = (current_bits << 8) | byte + num_bits += 8 + + # While we have at least 6 bits, extract and encode them + while num_bits >= 6: + num_bits -= 6 + # Extract the top 6 bits + index = (current_bits >> num_bits) & 0x3F + result.append(base64_chars[index]) + + # Handle any remaining bits (less than 6 bits) + if num_bits > 0: + # Pad with zeros to make a multiple of 6 bits + current_bits <<= 6 - num_bits + index = current_bits & 0x3F + result.append(base64_chars[index]) + + # Add padding '=' characters if necessary + while len(result) % 4 != 0: + result.append('=') + + # Join the Base64 characters to form the final encoded string + base64_data = ''.join(result) + + return base64_data + +# Example usage: +if __name__ == "__main__": + input_string = "Hello, this is a string to be encoded in Base64." + base64_result = string_to_base64(input_string) + print("Base64 Encoded String:", base64_result) From 3ccd8c550fa61fe5132204f80df21ee2a85c7a67 Mon Sep 17 00:00:00 2001 From: Souvik Sen <82510209+Yourstruggle11@users.noreply.github.com> Date: Tue, 3 Oct 2023 03:28:20 +0530 Subject: [PATCH 3/3] Update code with regex --- string_to_base64 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/string_to_base64 b/string_to_base64 index 8a4018c..d83ec6d 100644 --- a/string_to_base64 +++ b/string_to_base64 @@ -11,15 +11,18 @@ def string_to_base64(input_string): # Define the Base64 character mapping base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + # Filter out non-alphanumeric characters and whitespace + filtered_string = ''.join(char for char in input_string if char.isalnum() or char.isspace()) + # Initialize variables result = [] current_bits = 0 num_bits = 0 - # Convert the input string to bytes - bytes_data = input_string.encode('utf-8') + # Convert the filtered input string to bytes + bytes_data = filtered_string.encode('utf-8') - # Iterate through each byte in the input data + # Iterate through each byte in the filtered input data for byte in bytes_data: # Shift the current_bits left by 8 bits and add the new byte current_bits = (current_bits << 8) | byte @@ -50,6 +53,6 @@ def string_to_base64(input_string): # Example usage: if __name__ == "__main__": - input_string = "Hello, this is a string to be encoded in Base64." + input_string = "Hello, this is a string to be encoded in Base64. #$%^" base64_result = string_to_base64(input_string) print("Base64 Encoded String:", base64_result)