Force declaring the setting types and stop storing them in localStorage
authorRaphaël Jakse <raphael.trivabble@jakse.fr>
Wed, 7 Oct 2020 19:48:30 +0000 (21:48 +0200)
committerRaphaël Jakse <raphael.trivabble@jakse.fr>
Wed, 7 Oct 2020 19:48:30 +0000 (21:48 +0200)
public/trivabble.js

index 3d8b69a974ab2a885c9d76963999377967f659a9..dfff5c5f9b8aa201fe3eaeb038568ad72f6bda01 100644 (file)
         let type;
 
         /* try to retrieve type from configuration */
-        if (Object.prototype.hasOwnProperty.call(Conf, key)) {
+        if (Object.prototype.hasOwnProperty.call(SettingsTypes, key)) {
+            type = SettingsTypes[key];
+        } else if (Object.prototype.hasOwnProperty.call(Conf, key)) {
             type = typeof Conf[key];
         }
 
             }
         }
 
-        /* if not set type is defined from value */
-        if (typeof type === "undefined") {
-            type = typeof value;
-        }
-
         /* storage value in localstorage */
         if (type === typeof value) {
             if (type === "object") {
                 (type === "object") ||
                 (type === "string")) {
                 localStorage.setItem("trivabble" + key, value);
-
-                /* store type into localstorage if no default value in configuration */
-                if (!Object.prototype.hasOwnProperty.call(Conf, key)) {
-                    localStorage.setItem("trivabble" + key + "_type", type);
-                }
             } else {
                 console.error("Unsupported type");
             }
         } else {
-            console.error("incoherent type");
-        }
-    }
-
-    function migrateSetting(key, type) {
-        if (Object.prototype.hasOwnProperty.call(localStorage, "trivabble" + key)) {
-            localStorage.setItem("trivabble" + key + "_type", type);
+            console.error("Incoherent or missing type. See the SettingsTypes object.");
         }
     }
 
-    migrateSetting("SpellCheckerEnabledOnce", "boolean");
-    migrateSetting("DisableSpellChecker", "boolean");
-    migrateSetting("GameNumber", "number");
-    migrateSetting("BoardLang", "string");
-    migrateSetting("GameNumber", "number");
-    migrateSetting("Lang", "string");
-    migrateSetting("PlayerName", "string");
+    const SettingsTypes = {
+        SpellCheckerEnabledOnce: "boolean",
+        DisableSpellChecker: "boolean",
+        GameNumber: "number",
+        BoardLang: "string",
+        Lang: "string",
+        PlayerName: "string"
+    };
 
     const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {
         return s;