jmdobry / angular-cache

angular-cache is a very useful replacement for the Angular 1 $cacheFactory.
http://jmdobry.github.io/angular-cache
MIT License
1.39k stars 156 forks source link

cache.has feature request #214

Closed rmckeel closed 8 years ago

rmckeel commented 8 years ago

Hello,

Nice work on this library!

It would be nice to have a .has function in addition to .get. Consider the following scenarios:

var myCache = cache.getCache('myNamespace');
myCache.set('A', 0);
myCache.set('B', undefined);

if(myCache.get('A')) {
  // this never triggers, but this may be a common mistake among newer JS developers as it is a falsey value
}

if(myCache.get('B')) {
  // this never triggers because value is undefined, but that is a legitimate value that has been set
}

Instead of having to iterate over keys to see if it actually has been set, a .has function would clear this up.

Thoughts?

jmdobry commented 8 years ago

You can already achieve the functionality you're looking for (determine whether a key has been set) with Cache#info([key]).

var myCache = cache.getCache('myNamespace');
myCache.set('A', 0);
myCache.set('B', undefined);

!!myCache.get('A'); // false
!!myCache.info('A'); // true

!!myCache.get('A'); // false
!!myCache.info('A'); // true

So if your inexperienced developer writes if(myCache.info('A')) {...} it will still work as intended, even if the value set for that key is falsey. If you pass a key to Cache#info([key]) but the key has not been set, then undefined will be returned.

rmckeel commented 8 years ago

Thank, @jmdobry. That makes sense, but being new to angular-cache, I wouldn't have found it on my own!

I believe updating the documentation to include a similar use case / example would help others like myself.

Thanks!