Convert `Controller/Captcha` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Thu, 5 Nov 2020 19:05:04 +0000 (20:05 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 5 Nov 2020 19:05:04 +0000 (20:05 +0100)
This module was intentionally kept as an object instead of simple function exports because of the `delete()` function being a reserved keyword.

wcfsetup/install/files/js/WoltLabSuite/Core/Controller/Captcha.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Controller/Captcha.ts [new file with mode: 0644]

index 3b5b21cf37935b3ac92c147d8ed1b276491ddcd0..fc6b10509c7e053e474c5611aa394f90e2a80331 100644 (file)
@@ -1,64 +1,54 @@
 /**
  * Provides data of the active user.
  *
- * @author     Matthias Schmidt
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Controller/Captcha
+ * @author  Matthias Schmidt
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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 (file)
index c5ee69c..0000000
+++ /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 <http://opensource.org/licenses/lgpl-license.php>
- * @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 (file)
index 0000000..054e0ce
--- /dev/null
@@ -0,0 +1,63 @@
+/**
+ * Provides data of the active user.
+ *
+ * @author  Matthias Schmidt
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Controller/Captcha
+ */
+
+type CallbackCaptcha = () => unknown;
+
+const _captchas = new Map<string, CallbackCaptcha>();
+
+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;