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() {
|
||||
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--;
|
||||
|
|
|
@ -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" });
|
||||
|
|
Loading…
Reference in New Issue