diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-07-31 13:04:16 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-31 13:04:16 -0400 |
commit | 454ffaad9658256cb4980ee27eb013606b285edc (patch) | |
tree | 85638439db117db833d15a0184ecd45b1f1a83b6 /ext/js | |
parent | 00c5ae79833a641ccc5f7d31b6eea3e91db4cb71 (diff) |
StringUtil (#1861)
* Add StringUtil class
* Use StringUtil
Diffstat (limited to 'ext/js')
-rw-r--r-- | ext/js/data/sandbox/string-util.js | 61 | ||||
-rw-r--r-- | ext/js/media/audio-downloader.js | 7 | ||||
-rw-r--r-- | ext/js/pages/settings/backup-controller.js | 12 |
3 files changed, 65 insertions, 15 deletions
diff --git a/ext/js/data/sandbox/string-util.js b/ext/js/data/sandbox/string-util.js new file mode 100644 index 00000000..37e021c9 --- /dev/null +++ b/ext/js/data/sandbox/string-util.js @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +/** + * Class containing generic string utility functions. + */ +class StringUtil { + /** + * Decodes the contents of an ArrayBuffer using UTF8. + * @param arrayBuffer The input ArrayBuffer. + * @returns A UTF8-decoded string. + */ + static arrayBufferUtf8Decode(arrayBuffer) { + try { + return new TextDecoder('utf-8').decode(arrayBuffer); + } catch (e) { + return decodeURIComponent(escape(this.arrayBufferToBinaryString(arrayBuffer))); + } + } + + /** + * Converts the contents of an ArrayBuffer to a base64 string. + * @param arrayBuffer The input ArrayBuffer. + * @returns A base64 string representing the binary content. + */ + static arrayBufferToBase64(arrayBuffer) { + return btoa(this.arrayBufferToBinaryString(arrayBuffer)); + } + + /** + * Converts the contents of an ArrayBuffer to a binary string. + * @param arrayBuffer The input ArrayBuffer. + * @returns 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; + } + } +} diff --git a/ext/js/media/audio-downloader.js b/ext/js/media/audio-downloader.js index e3b507b4..4bc4cf1b 100644 --- a/ext/js/media/audio-downloader.js +++ b/ext/js/media/audio-downloader.js @@ -19,6 +19,7 @@ * JsonSchema * NativeSimpleDOMParser * SimpleDOMParser + * StringUtil */ class AudioDownloader { @@ -257,7 +258,7 @@ class AudioDownloader { throw new Error('Could not retrieve audio'); } - const data = this._arrayBufferToBase64(arrayBuffer); + const data = StringUtil.arrayBufferToBase64(arrayBuffer); const contentType = response.headers.get('Content-Type'); return {data, contentType}; } @@ -288,10 +289,6 @@ class AudioDownloader { return digest; } - _arrayBufferToBase64(arrayBuffer) { - return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer))); - } - _createSimpleDOMParser(content) { if (typeof NativeSimpleDOMParser !== 'undefined' && NativeSimpleDOMParser.isSupported()) { return new NativeSimpleDOMParser(content); diff --git a/ext/js/pages/settings/backup-controller.js b/ext/js/pages/settings/backup-controller.js index 02ad368c..cf1a6b0c 100644 --- a/ext/js/pages/settings/backup-controller.js +++ b/ext/js/pages/settings/backup-controller.js @@ -18,6 +18,7 @@ /* global * DictionaryController * OptionsUtil + * StringUtil */ class BackupController { @@ -317,17 +318,8 @@ class BackupController { return warnings; } - _utf8Decode(arrayBuffer) { - try { - return new TextDecoder('utf-8').decode(arrayBuffer); - } catch (e) { - const binaryString = String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)); - return decodeURIComponent(escape(binaryString)); - } - } - async _importSettingsFile(file) { - const dataString = this._utf8Decode(await this._readFileArrayBuffer(file)); + const dataString = StringUtil.arrayBufferUtf8Decode(await this._readFileArrayBuffer(file)); const data = JSON.parse(dataString); // Type check |