DEV: implements removeKeys(predicate) in keyValueStore (#18019)

This commit is contained in:
Joffrey JAFFEUX 2022-08-21 19:19:25 +02:00 committed by GitHub
parent 9130565895
commit 4e3c688d65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -26,14 +26,27 @@ export default class KeyValueStore {
}
abandonLocal() {
return this.removeKeys();
}
removeKeys(predicate = () => true) {
if (!safeLocalStorage) {
return;
}
let i = safeLocalStorage.length - 1;
while (i >= 0) {
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);
}
i--;

View File

@ -17,6 +17,21 @@ module("Unit | Utility | key-value-store", function () {
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) {
const store = new KeyValueStore("example");
store.set({ key: "bob1", value: "uncle" });