Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat($cacheFactory): Make Cache.put return the value #1583

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ng/cacheFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @returns {object} Newly created cache object with the following set of methods:
*
* - `{object}` `info()` — Returns id, size, and options of cache.
* - `{void}` `put({string} key, {*} value)` — Puts a new key-value pair into the cache.
* - `{{*}}` `put({string} key, {*} value)` — Puts a new key-value pair into the cache.
* - `{{*}}` `get({string} key)` — Returns cached value for `key` or undefined for cache miss.
* - `{void}` `remove({string} key)` — Removes a key-value pair from the cache.
* - `{void}` `removeAll()` — Removes all cached values.
Expand Down Expand Up @@ -46,13 +46,15 @@ function $CacheFactoryProvider() {

refresh(lruEntry);

if (isUndefined(value)) return;
if (isUndefined(value)) return value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is useless because return undefined is the same thing as return.

if (!(key in data)) size++;
data[key] = value;

if (size > capacity) {
this.remove(staleEnd.key);
}

return value;
},


Expand Down
6 changes: 6 additions & 0 deletions test/ng/cacheFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ describe('$cacheFactory', function() {
cache.remove(123);
expect(cache.info().size).toBe(0);
}));

it("should return value from put", inject(function($cacheFactory) {
function DummyClass() {}
var obj = new DummyClass();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just var obj = {}, we just need to be able to test object identity and for that object literal is good enough.

expect(cache.put('k1', obj)).toBe(obj);
}));
});


Expand Down