summaryrefslogtreecommitdiff
path: root/ext/js/data/sandbox/array-buffer-util.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2024-01-31 08:38:30 -0500
committerGitHub <noreply@github.com>2024-01-31 13:38:30 +0000
commit87ed7c8affd3ade9d3cd2d9ed1a61dd5f224e473 (patch)
treebe727294e31ef21e8a3f634734610e69e4a155ac /ext/js/data/sandbox/array-buffer-util.js
parent3e419aa562aab03ca20421aaf7e4d1a39194a5b4 (diff)
Module refactoring (#588)
* Convert PronunciationGenerator into static functions * Convert DictionaryDataUtil into static functions * Convert AnkiNoteDataCreator into static functions * Convert MediaUtil into static functions * Convert RegexUtil into static functions * Convert StringUtil into static functions * Convert ArrayBufferUtil into static functions * Convert AnkiUtil into static functions * Convert PermissionsUtil into static functions * Convert ProfileConditionsUtil into static functions
Diffstat (limited to 'ext/js/data/sandbox/array-buffer-util.js')
-rw-r--r--ext/js/data/sandbox/array-buffer-util.js93
1 files changed, 44 insertions, 49 deletions
diff --git a/ext/js/data/sandbox/array-buffer-util.js b/ext/js/data/sandbox/array-buffer-util.js
index 1857ec74..487fcd24 100644
--- a/ext/js/data/sandbox/array-buffer-util.js
+++ b/ext/js/data/sandbox/array-buffer-util.js
@@ -17,61 +17,56 @@
*/
/**
- * Class containing generic ArrayBuffer utility functions.
+ * Decodes the contents of an ArrayBuffer using UTF8.
+ * @param {ArrayBuffer} arrayBuffer The input ArrayBuffer.
+ * @returns {string} A UTF8-decoded string.
*/
-export class ArrayBufferUtil {
- /**
- * Decodes the contents of an ArrayBuffer using UTF8.
- * @param {ArrayBuffer} arrayBuffer The input ArrayBuffer.
- * @returns {string} A UTF8-decoded string.
- */
- static arrayBufferUtf8Decode(arrayBuffer) {
- try {
- return new TextDecoder('utf-8').decode(arrayBuffer);
- } catch (e) {
- return decodeURIComponent(escape(this.arrayBufferToBinaryString(arrayBuffer)));
- }
+export function arrayBufferUtf8Decode(arrayBuffer) {
+ try {
+ return new TextDecoder('utf-8').decode(arrayBuffer);
+ } catch (e) {
+ return decodeURIComponent(escape(arrayBufferToBinaryString(arrayBuffer)));
}
+}
- /**
- * Converts the contents of an ArrayBuffer to a base64 string.
- * @param {ArrayBuffer} arrayBuffer The input ArrayBuffer.
- * @returns {string} A base64 string representing the binary content.
- */
- static arrayBufferToBase64(arrayBuffer) {
- return btoa(this.arrayBufferToBinaryString(arrayBuffer));
- }
+/**
+ * Converts the contents of an ArrayBuffer to a base64 string.
+ * @param {ArrayBuffer} arrayBuffer The input ArrayBuffer.
+ * @returns {string} A base64 string representing the binary content.
+ */
+export function arrayBufferToBase64(arrayBuffer) {
+ return btoa(arrayBufferToBinaryString(arrayBuffer));
+}
- /**
- * Converts the contents of an ArrayBuffer to a binary string.
- * @param {ArrayBuffer} arrayBuffer The input ArrayBuffer.
- * @returns {string} A string representing the binary content.
- */
- static arrayBufferToBinaryString(arrayBuffer) {
- const bytes = new Uint8Array(arrayBuffer);
- try {
- return String.fromCharCode(...bytes);
- } catch (e) {
- let binary = '';
- for (let i = 0, ii = bytes.byteLength; i < ii; ++i) {
- binary += String.fromCharCode(bytes[i]);
- }
- return binary;
+/**
+ * Converts the contents of an ArrayBuffer to a binary string.
+ * @param {ArrayBuffer} arrayBuffer The input ArrayBuffer.
+ * @returns {string} A string representing the binary content.
+ */
+export function arrayBufferToBinaryString(arrayBuffer) {
+ const bytes = new Uint8Array(arrayBuffer);
+ try {
+ return String.fromCharCode(...bytes);
+ } catch (e) {
+ let binary = '';
+ for (let i = 0, ii = bytes.byteLength; i < ii; ++i) {
+ binary += String.fromCharCode(bytes[i]);
}
+ return binary;
}
+}
- /**
- * Converts a base64 string to an ArrayBuffer.
- * @param {string} content The binary content string encoded in base64.
- * @returns {ArrayBuffer} A new `ArrayBuffer` object corresponding to the specified content.
- */
- static base64ToArrayBuffer(content) {
- const binaryContent = atob(content);
- const length = binaryContent.length;
- const array = new Uint8Array(length);
- for (let i = 0; i < length; ++i) {
- array[i] = binaryContent.charCodeAt(i);
- }
- return array.buffer;
+/**
+ * Converts a base64 string to an ArrayBuffer.
+ * @param {string} content The binary content string encoded in base64.
+ * @returns {ArrayBuffer} A new `ArrayBuffer` object corresponding to the specified content.
+ */
+export function base64ToArrayBuffer(content) {
+ const binaryContent = atob(content);
+ const length = binaryContent.length;
+ const array = new Uint8Array(length);
+ for (let i = 0; i < length; ++i) {
+ array[i] = binaryContent.charCodeAt(i);
}
+ return array.buffer;
}