From afabe28c4905e2d56dc75f8a60c9cd2715ae8d61 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 5 Nov 2020 20:05:04 +0100 Subject: [PATCH] Convert `Controller/Captcha` to TypeScript This module was intentionally kept as an object instead of simple function exports because of the `delete()` function being a reserved keyword. --- .../WoltLabSuite/Core/Controller/Captcha.js | 48 +++++------- .../WoltLabSuite/Core/Controller/Captcha.js | 73 ------------------- .../WoltLabSuite/Core/Controller/Captcha.ts | 63 ++++++++++++++++ 3 files changed, 82 insertions(+), 102 deletions(-) delete mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.js create mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.ts diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Controller/Captcha.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Controller/Captcha.js index 3b5b21cf37..fc6b10509c 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Controller/Captcha.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Controller/Captcha.js @@ -1,64 +1,54 @@ /** * Provides data of the active user. * - * @author Matthias Schmidt - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/Controller/Captcha + * @author Matthias Schmidt + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Controller/Captcha */ -define(['Dictionary'], function (Dictionary) { +define(["require", "exports"], function (require, exports) { "use strict"; - var _captchas = new Dictionary(); - /** - * @exports WoltLabSuite/Core/Controller/Captcha - */ - return { + const _captchas = new Map(); + const ControllerCaptcha = { /** * Registers a captcha with the given identifier and callback used to get captcha data. - * - * @param {string} captchaId captcha identifier - * @param {function} callback callback to get captcha data */ - add: function (captchaId, callback) { + add(captchaId, callback) { if (_captchas.has(captchaId)) { - throw new Error("Captcha with id '" + captchaId + "' is already registered."); + throw new Error(`Captcha with id '${captchaId}' is already registered.`); } - if (typeof callback !== 'function') { + if (typeof callback !== "function") { throw new TypeError("Expected a valid callback for parameter 'callback'."); } _captchas.set(captchaId, callback); }, /** * Deletes the captcha with the given identifier. - * - * @param {string} captchaId identifier of the captcha to be deleted */ - 'delete': function (captchaId) { + delete(captchaId) { if (!_captchas.has(captchaId)) { - throw new Error("Unknown captcha with id '" + captchaId + "'."); + throw new Error(`Unknown captcha with id '${captchaId}'.`); } _captchas.delete(captchaId); }, /** * Returns true if a captcha with the given identifier exists. - * - * @param {string} captchaId captcha identifier - * @return {boolean} */ - has: function (captchaId) { + has(captchaId) { return _captchas.has(captchaId); }, /** * Returns the data of the captcha with the given identifier. * - * @param {string} captchaId captcha identifier - * @return {Object} captcha data + * @param {string} captchaId captcha identifier + * @return {Object} captcha data */ - getData: function (captchaId) { + getData(captchaId) { if (!_captchas.has(captchaId)) { - throw new Error("Unknown captcha with id '" + captchaId + "'."); + throw new Error(`Unknown captcha with id '${captchaId}'.`); } return _captchas.get(captchaId)(); - } + }, }; + return ControllerCaptcha; }); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.js deleted file mode 100644 index c5ee69c7fb..0000000000 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.js +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Provides data of the active user. - * - * @author Matthias Schmidt - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/Controller/Captcha - */ -define(['Dictionary'], function(Dictionary) { - "use strict"; - - var _captchas = new Dictionary(); - - /** - * @exports WoltLabSuite/Core/Controller/Captcha - */ - return { - /** - * Registers a captcha with the given identifier and callback used to get captcha data. - * - * @param {string} captchaId captcha identifier - * @param {function} callback callback to get captcha data - */ - add: function(captchaId, callback) { - if (_captchas.has(captchaId)) { - throw new Error("Captcha with id '" + captchaId + "' is already registered."); - } - - if (typeof callback !== 'function') { - throw new TypeError("Expected a valid callback for parameter 'callback'."); - } - - _captchas.set(captchaId, callback); - }, - - /** - * Deletes the captcha with the given identifier. - * - * @param {string} captchaId identifier of the captcha to be deleted - */ - 'delete': function(captchaId) { - if (!_captchas.has(captchaId)) { - throw new Error("Unknown captcha with id '" + captchaId + "'."); - } - - _captchas.delete(captchaId); - }, - - /** - * Returns true if a captcha with the given identifier exists. - * - * @param {string} captchaId captcha identifier - * @return {boolean} - */ - has: function(captchaId) { - return _captchas.has(captchaId); - }, - - /** - * Returns the data of the captcha with the given identifier. - * - * @param {string} captchaId captcha identifier - * @return {Object} captcha data - */ - getData: function(captchaId) { - if (!_captchas.has(captchaId)) { - throw new Error("Unknown captcha with id '" + captchaId + "'."); - } - - return _captchas.get(captchaId)(); - } - }; -}); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.ts new file mode 100644 index 0000000000..054e0ce4a0 --- /dev/null +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.ts @@ -0,0 +1,63 @@ +/** + * Provides data of the active user. + * + * @author Matthias Schmidt + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Controller/Captcha + */ + +type CallbackCaptcha = () => unknown; + +const _captchas = new Map(); + +const ControllerCaptcha = { + /** + * Registers a captcha with the given identifier and callback used to get captcha data. + */ + add(captchaId: string, callback: CallbackCaptcha): void { + if (_captchas.has(captchaId)) { + throw new Error(`Captcha with id '${captchaId}' is already registered.`); + } + + if (typeof callback !== "function") { + throw new TypeError("Expected a valid callback for parameter 'callback'."); + } + + _captchas.set(captchaId, callback); + }, + + /** + * Deletes the captcha with the given identifier. + */ + delete(captchaId: string): void { + if (!_captchas.has(captchaId)) { + throw new Error(`Unknown captcha with id '${captchaId}'.`); + } + + _captchas.delete(captchaId); + }, + + /** + * Returns true if a captcha with the given identifier exists. + */ + has(captchaId: string): boolean { + return _captchas.has(captchaId); + }, + + /** + * Returns the data of the captcha with the given identifier. + * + * @param {string} captchaId captcha identifier + * @return {Object} captcha data + */ + getData(captchaId: string): unknown { + if (!_captchas.has(captchaId)) { + throw new Error(`Unknown captcha with id '${captchaId}'.`); + } + + return _captchas.get(captchaId)!(); + }, +}; + +export = ControllerCaptcha; -- 2.20.1