Skip to content

Commit

Permalink
Swapping siguratures of delete() & remove() and adding keys() t…
Browse files Browse the repository at this point in the history
…o get an API closer to a `Map` (leaving `length` vs `size` for now)
  • Loading branch information
avoidwork committed Jan 13, 2019
1 parent c0d0fb2 commit b347c8e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 60 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ Gets cached item and moves it to the front
const item = cache.get("myKey");
```

## keys
### Method

Returns an `Array` of cache item keys.

return {Array} Array of keys

**Example**

```javascript
console.log(cache.keys());
```

## max
### Property

Expand Down Expand Up @@ -127,7 +140,7 @@ cache.length; // 0 - it's a new cache!
## remove
### Method

Removes item from cache
(Deprecated) Removes item from cache

param {String} key Item key
return {Object} LRU instance
Expand Down
56 changes: 30 additions & 26 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2019
* @license BSD-3-Clause
* @link https://github.com/avoidwork/tiny-lru
* @version 5.0.7
* @version 5.1.0
*/
"use strict";

Expand All @@ -29,7 +29,30 @@
}

delete (key, bypass = false) {
return this.remove(key, bypass);
if (bypass === true || this.has(key) === true) {
const item = this.cache[key];

delete this.cache[key];
this.length--;

if (item.next !== empty) {
this.cache[item.next].prev = item.prev;
}

if (item.prev !== empty) {
this.cache[item.prev].next = item.next;
}

if (this.first === key) {
this.first = item.next;
}

if (this.last === key) {
this.last = item.prev;
}
}

return this;
}

evict () {
Expand Down Expand Up @@ -61,31 +84,12 @@
return key in this.cache;
}

remove (key, bypass = false) {
if (bypass === true || this.has(key) === true) {
const item = this.cache[key];

delete this.cache[key];
this.length--;

if (item.next !== empty) {
this.cache[item.next].prev = item.prev;
}

if (item.prev !== empty) {
this.cache[item.prev].next = item.next;
}

if (this.first === key) {
this.first = item.next;
}

if (this.last === key) {
this.last = item.prev;
}
}
keys () {
return Object.keys(this.cache);
}

return this;
remove (key, bypass = false) {
return this.delete(key, bypass);
}

set (key, value, bypass = false) {
Expand Down
6 changes: 3 additions & 3 deletions lib/tiny-lru.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/tiny-lru.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tiny-lru",
"description": "Tiny LRU cache for Client or Server",
"version": "5.0.7",
"version": "5.1.0",
"homepage": "https://github.com/avoidwork/tiny-lru",
"author": "Jason Mulligan <jason.mulligan@avoidwork.com>",
"repository": {
Expand Down

0 comments on commit b347c8e

Please sign in to comment.