Skip to content

A platform for community discussion. Free, open, simple.

DEV: Use `class` syntax in key-value-store (#15005)

Jarek Radosz8dc9e0f4bd17
diff --git a/app/assets/javascripts/discourse/app/lib/key-value-store.js b/app/assets/javascripts/discourse/app/lib/key-value-store.js
index 94c67018b91..1b67bf52a3e 100644
--- a/app/assets/javascripts/discourse/app/lib/key-value-store.js
+++ b/app/assets/javascripts/discourse/app/lib/key-value-store.js
@@ -14,11 +14,13 @@ try {
safeLocalStorage = null;
}
-const KeyValueStore = function (ctx) {
- this.context = ctx;
-};
+export default class KeyValueStore {
+ context = null;
+
+ constructor(ctx) {
+ this.context = ctx;
+ }
-KeyValueStore.prototype = {
abandonLocal() {
if (!safeLocalStorage) {
return;
@@ -32,57 +34,64 @@ KeyValueStore.prototype = {
}
i--;
}
+
return true;
- },
+ }
remove(key) {
if (!safeLocalStorage) {
return;
}
+
return safeLocalStorage.removeItem(this.context + key);
- },
+ }
set(opts) {
if (!safeLocalStorage) {
return false;
}
+
safeLocalStorage[this.context + opts.key] = opts.value;
- },
+ }
setObject(opts) {
this.set({ key: opts.key, value: JSON.stringify(opts.value) });
- },
+ }
get(key) {
if (!safeLocalStorage) {
return null;
}
return safeLocalStorage[this.context + key];
- },
+ }
getInt(key, def) {
if (!def) {
def = 0;
}
+
if (!safeLocalStorage) {
return def;
}
+
const result = parseInt(this.get(key), 10);
if (!isFinite(result)) {
return def;
}
+
return result;
- },
+ }
getObject(key) {
if (!safeLocalStorage) {
return null;
}
+
try {
return JSON.parse(safeLocalStorage[this.context + key]);
} catch (e) {}
- },
-};
+ }
+}
// API compatibility with `localStorage`
KeyValueStore.prototype.getItem = KeyValueStore.prototype.get;
@@ -90,5 +99,3 @@ KeyValueStore.prototype.removeItem = KeyValueStore.prototype.remove;
KeyValueStore.prototype.setItem = function (key, value) {
this.set({ key, value });
};
-
-export default KeyValueStore;
diff --git a/app/assets/javascripts/discourse/tests/unit/lib/key-value-store-test.js b/app/assets/javascripts/discourse/tests/unit/lib/key-value-store-test.js
index 243dc9a981a..ed323d85b9a 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/key-value-store-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/key-value-store-test.js
@@ -2,18 +2,37 @@ import { module, test } from "qunit";
import KeyValueStore from "discourse/lib/key-value-store";
module("Unit | Utility | key-value-store", function () {
- test("it's able to get the result back from the store", function (assert) {
- const store = new KeyValueStore("_test");
+ test("is able to get the result back from the store", function (assert) {
+ const store = new KeyValueStore("test_");
store.set({ key: "bob", value: "uncle" });
+
assert.strictEqual(store.get("bob"), "uncle");
});
+ test("is able remove items from the store", function (assert) {
+ const store = new KeyValueStore("test_");
+ store.set({ key: "bob", value: "uncle" });
+ store.remove("bob");
+
+ assert.strictEqual(store.get("bob"), undefined);
+ });
+
test("is able to nuke the store", function (assert) {
- const store = new KeyValueStore("_test");
+ const store = new KeyValueStore("test_");
store.set({ key: "bob1", value: "uncle" });
store.abandonLocal();
localStorage.a = 1;
- assert.strictEqual(store.get("bob1"), void 0);
+
+ assert.strictEqual(store.get("bob1"), undefined);
assert.strictEqual(localStorage.a, "1");
});
+
+ test("is API-compatible with `localStorage`", function (assert) {
+ const store = new KeyValueStore("test_");
+ store.setItem("bob", "uncle");
+ assert.strictEqual(store.getItem("bob"), "uncle");
+
+ store.removeItem("bob");
+ assert.strictEqual(store.getItem("bob"), undefined);
+ });
});