Skip to content

Commit

Permalink
Fixing assignment of last & adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Dec 8, 2018
1 parent 8b6719a commit 12d7d06
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 73 deletions.
16 changes: 8 additions & 8 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2018
* @license BSD-3-Clause
* @link https://github.com/avoidwork/tiny-lru
* @version 4.0.5
* @version 4.0.6
*/
"use strict";

Expand Down Expand Up @@ -141,23 +141,23 @@
item.value = value;

if (this.first !== key) {
const left = item.next,
right = item.previous;
const n = item.next,
p = item.previous;

item.next = empty;
item.previous = this.first;
link(this.cache[this.first], key, "next", "previous");

if (left !== empty && left !== this.first) {
if (right !== empty) {
this.cache[right].next = left;
if (n !== empty && n !== this.first) {
if (p !== empty) {
this.cache[p].next = n;
}

this.cache[left].previous = right;
this.cache[n].previous = p;
}

if (this.last === key) {
this.last = item.previous;
this.last = n;
}
}
} else {
Expand Down
4 changes: 2 additions & 2 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

Large diffs are not rendered by default.

133 changes: 80 additions & 53 deletions 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": "4.0.5",
"version": "4.0.6",
"homepage": "https://github.com/avoidwork/tiny-lru",
"author": "Jason Mulligan <jason.mulligan@avoidwork.com>",
"repository": {
Expand Down
14 changes: 7 additions & 7 deletions src/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,23 @@
item.value = value;

if (this.first !== key) {
const left = item.next,
right = item.previous;
const n = item.next,
p = item.previous;

item.next = empty;
item.previous = this.first;
link(this.cache[this.first], key, "next", "previous");

if (left !== empty && left !== this.first) {
if (right !== empty) {
this.cache[right].next = left;
if (n !== empty && n !== this.first) {
if (p !== empty) {
this.cache[p].next = n;
}

this.cache[left].previous = right;
this.cache[n].previous = p;
}

if (this.last === key) {
this.last = item.previous;
this.last = n;
}
}
} else {
Expand Down

0 comments on commit 12d7d06

Please sign in to comment.