mirror of
https://github.com/discourse/discourse.git
synced 2024-12-02 13:39:36 -06:00
DEV: implements removeKeys(predicate)
in keyValueStore (#18019)
This commit is contained in:
parent
9130565895
commit
4e3c688d65
@ -26,14 +26,27 @@ export default class KeyValueStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abandonLocal() {
|
abandonLocal() {
|
||||||
|
return this.removeKeys();
|
||||||
|
}
|
||||||
|
|
||||||
|
removeKeys(predicate = () => true) {
|
||||||
if (!safeLocalStorage) {
|
if (!safeLocalStorage) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let i = safeLocalStorage.length - 1;
|
let i = safeLocalStorage.length - 1;
|
||||||
|
|
||||||
while (i >= 0) {
|
while (i >= 0) {
|
||||||
let k = safeLocalStorage.key(i);
|
let k = safeLocalStorage.key(i);
|
||||||
if (k.substring(0, this.context.length) === this.context) {
|
let v = safeLocalStorage[k];
|
||||||
|
try {
|
||||||
|
v = JSON.parse(v);
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
if (
|
||||||
|
k.substring(0, this.context.length) === this.context &&
|
||||||
|
predicate(k, v)
|
||||||
|
) {
|
||||||
safeLocalStorage.removeItem(k);
|
safeLocalStorage.removeItem(k);
|
||||||
}
|
}
|
||||||
i--;
|
i--;
|
||||||
|
@ -17,6 +17,21 @@ module("Unit | Utility | key-value-store", function () {
|
|||||||
assert.strictEqual(store.get("bob"), undefined);
|
assert.strictEqual(store.get("bob"), undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("is able to remove multiple items at once from the store", function (assert) {
|
||||||
|
const store = new KeyValueStore("example");
|
||||||
|
store.set({ key: "bob", value: "uncle" });
|
||||||
|
store.set({ key: "jane", value: "sister" });
|
||||||
|
store.set({ key: "clark", value: "brother" });
|
||||||
|
|
||||||
|
store.removeKeys((key, value) => {
|
||||||
|
return key.includes("bob") || value === "brother";
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.strictEqual(store.get("bob"), undefined);
|
||||||
|
assert.strictEqual(store.get("jane"), "sister");
|
||||||
|
assert.strictEqual(store.get("clark"), undefined);
|
||||||
|
});
|
||||||
|
|
||||||
test("is able to nuke the store", function (assert) {
|
test("is able to nuke the store", function (assert) {
|
||||||
const store = new KeyValueStore("example");
|
const store = new KeyValueStore("example");
|
||||||
store.set({ key: "bob1", value: "uncle" });
|
store.set({ key: "bob1", value: "uncle" });
|
||||||
|
Loading…
Reference in New Issue
Block a user