Storage management function addition request


Topic: Storage management function addition request

dreamcc asked 2 years ago

Expected behavior

Just want to judge whether the specified cache name exists in the cache and return a Boolean value

只想判断缓存中是否存在指定的缓存名称,并返回一个布尔值。



Hope to increase

希望增加

Storage.has('StorageName');

var res = Storage.has('StorageName');
if(res == true){
    // ......
}else{
    // ......
}

Actual behavior。 No such function. 没有这样的功能


Grzegorz Bujański staff answered 2 years ago

We don't have such a method, but you can easily use the get method to do this:

Storage.set('date', new Date());
Storage.set('example', '');

console.log(Boolean(Storage.get('date'))); // -> true
console.log(Boolean(Storage.get('example'))); // -> false
console.log(Boolean(Storage.get('example2'))); // -> false

In some cases, you may want to store false in storage. In this case, it is better to create an additional method that will check it:

Storage.set('false', false);

const has = (name) => {
  const storageValue = Storage.get(name);
  return storageValue === false ? true : Boolean(storageValue);
}

console.log(has('false')); // -> true

dreamcc answered 2 years ago

All right. It's just that i always feel that lack a little function

In addition, Code version you released ,hope more tests can be conducted

When the caching function is used, an error is reported directly, and no method is found

I have modified the code myself. Please fix this problem in time


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: MDB Standard
  • MDB Version: MDB5 5.0.0
  • Device: PC
  • Browser: Microsoft Edge
  • OS: windows10
  • Provided sample code: No
  • Provided link: No