LocalStorage icon indicating copy to clipboard operation
LocalStorage copied to clipboard

[Feature Request] Add GetKeys()/RemoveKeys methods

Open Pjotrtje opened this issue 4 years ago • 2 comments

Is your feature request related to a problem? Please describe. I am porting a ReactJS website to Blazor and due to 'reasons' I store data with 'random' keys which I want to delete every x days. (this is vague I know). The easiest way is to compare all the keys in store with keys I 'allow' and then remove unwanted data by key.

Describe alternatives you've considered I can fix this now by: int Length(); -> foreach index -> string Key(int index); -> and depending on an if call: void RemoveItem(string key);

So when I have 100 keys in store and I have to remove 50. I will have to make 1 + 100 + 50 = 151 calls to JS interop.

Describe the solution you'd like If there were also IEnumerable<string> GetKeys() + void RemoveKeys(IEnumerable<string> keys)

Then I could achieve the same with just 2 calls. Which is nicer, more readable and faster.

var toRemoveKeys = localStorage.GetKeys().Except(allowedKeys);
localStorage.RemoveKeys(toRemoveKeys);

Additional context I describe it here as sync buth it would of cource also be nice in async.

FYI, code in my ReacttJS (TypeScript) app:

Object
    .keys(localStorage)
    .filter(k => !allowedKeys.includes(k))
    .forEach(r => localStorage.removeItem(r));

Pjotrtje avatar Feb 14 '21 10:02 Pjotrtje

Yes I need too

megafetis avatar May 23 '21 18:05 megafetis

I need the enumeration via GetKeys as well

steamonimo avatar Dec 22 '21 10:12 steamonimo

Fixed in #162

chrissainty avatar Aug 27 '22 22:08 chrissainty