Skip to content

Commit 5361c15

Browse files
committed
feat: swap list deps to connective-tissue
1 parent d38f7d5 commit 5361c15

File tree

7 files changed

+46
-282
lines changed

7 files changed

+46
-282
lines changed

__tests__/linkedList.test.js

Lines changed: 0 additions & 108 deletions
This file was deleted.

__tests__/lruCache.test.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ test(`${subject} pops off entries if it meets or exceeds the capacity`, t => {
1818
lru.put('END', 6);
1919
lru.put('END', 6);
2020

21-
t.equals(lru.adapter.length, cap);
22-
t.equals(lru.adapter.head.data.key, 'END');
21+
t.equals(lru.adapter.size(), cap);
22+
t.equals(lru.adapter.head().value.key, 'END');
2323
t.equals(lru.cache.size, cap);
2424
t.equals(lru.get('key1'), null);
2525
t.end();
@@ -38,13 +38,13 @@ test(`${subject} moves the most recently retrieved value to the head of its inte
3838
i++;
3939
}
4040

41-
t.equals(lru.adapter.head.data.key, `key${i - 1}`);
42-
t.equals(lru.adapter.head.data.value, i - 1);
41+
t.equals(lru.adapter.head().value.key, `key${i - 1}`);
42+
t.equals(lru.adapter.head().value.data, i - 1);
4343

4444
const recent = lru.get('key3');
4545

46-
t.equals(lru.adapter.head.data.key, 'key3');
47-
t.equals(lru.adapter.head.data.value, recent);
46+
t.equals(lru.adapter.head().value.key, 'key3');
47+
t.equals(lru.adapter.head().value.data, recent);
4848
t.end();
4949
});
5050

@@ -108,36 +108,36 @@ test(`${subject} internal list maintains integrity when compared to manually obs
108108
const lru = init(cap);
109109

110110
lru.put('key1'); // head = tail = key1
111-
t.equals(lru.adapter.head, lru.adapter.tail);
112-
t.equals(lru.adapter.head.data.key, 'key1');
111+
t.equals(lru.adapter.head(), lru.adapter.tail());
112+
t.equals(lru.adapter.head().value.key, 'key1');
113113

114114
lru.put('key2'); // head = k2, tail = k1
115-
t.equals(lru.adapter.head.data.key, 'key2');
116-
t.equals(lru.adapter.tail.data.key, 'key1');
115+
t.equals(lru.adapter.head().value.key, 'key2');
116+
t.equals(lru.adapter.tail().value.key, 'key1');
117117
t.equals(lru.adapter.size(), 2);
118118

119119
lru.get('key1'); // head = k1, tail = k2
120-
t.equals(lru.adapter.head.data.key, 'key1');
121-
t.equals(lru.adapter.tail.data.key, 'key2');
120+
t.equals(lru.adapter.head().value.key, 'key1');
121+
t.equals(lru.adapter.tail().value.key, 'key2');
122122
t.equals(lru.adapter.size(), 2);
123123

124124
lru.put('key3'); // head = k3, tail = k2 | k3, k1, k2 |
125-
t.equals(lru.adapter.head.data.key, 'key3');
126-
t.equals(lru.adapter.tail.data.key, 'key2');
125+
t.equals(lru.adapter.head().value.key, 'key3');
126+
t.equals(lru.adapter.tail().value.key, 'key2');
127127
t.equals(lru.adapter.size(), 3);
128128

129129
lru.get('key1'); // | k1, k3, k2 |
130-
t.equals(lru.adapter.head.data.key, 'key1');
131-
t.equals(lru.adapter.tail.data.key, 'key2');
130+
t.equals(lru.adapter.head().value.key, 'key1');
131+
t.equals(lru.adapter.tail().value.key, 'key2');
132132
t.equals(lru.adapter.size(), 3);
133133

134134
lru.put('key4'); // head = k4, tail = k1 -> | k4, k1, k3 |
135-
t.equals(lru.adapter.head.data.key, 'key4');
136-
t.equals(lru.adapter.tail.data.key, 'key3');
135+
t.equals(lru.adapter.head().value.key, 'key4');
136+
t.equals(lru.adapter.tail().value.key, 'key3');
137137

138138
lru.get('key1'); // head = k1, tail = k3 -> | k1, k4, k3 |
139-
t.equals(lru.adapter.head.data.key, 'key1');
140-
t.equals(lru.adapter.tail.data.key, 'key3');
139+
t.equals(lru.adapter.head().value.key, 'key1');
140+
t.equals(lru.adapter.tail().value.key, 'key3');
141141
t.equals(lru.adapter.size(), 3);
142142

143143
t.equals(lru.cache.size, lru.adapter.size());

lib/index.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import OpenDoublyLinkedList from './linkedList';
1+
import { CircularDoublyLinkedList } from 'connective-tissue';
22

3-
function Entry (key, value) {
3+
function Entry (key, data) {
44
return {
55
key,
6-
value
6+
data
77
};
88
}
99

1010
class LruCache {
1111
constructor (capacity) {
12-
this.adapter = new OpenDoublyLinkedList();
12+
this.adapter = new CircularDoublyLinkedList();
1313
this.cache = new Map();
1414
this.capacity = capacity || 10;
1515
}
@@ -24,14 +24,9 @@ class LruCache {
2424

2525
const node = this.cache.get(key);
2626

27-
this.adapter.remove(node);
28-
this.adapter.push(node.data);
29-
30-
// the node itself is updated qua the list
31-
// we want the cache to reflect the actual state
32-
this.cache.set(key, this.adapter.head);
27+
this.adapter.moveToFront(node);
3328

34-
return node.data.value;
29+
return node.value.data;
3530
}
3631

3732
/**
@@ -50,12 +45,12 @@ class LruCache {
5045
} else if (this.adapter.size() >= this.capacity) {
5146
const lru = this.adapter.pop();
5247

53-
this.cache.delete(lru.data.key);
48+
this.cache.delete(lru.value.key);
5449
}
5550

56-
this.adapter.push(Entry(key, value));
51+
this.adapter.pushFront(Entry(key, value));
5752

58-
this.cache.set(key, this.adapter.head);
53+
this.cache.set(key, this.adapter.head());
5954

6055
return value;
6156
}
@@ -73,7 +68,7 @@ class LruCache {
7368
this.adapter.remove(node);
7469
this.cache.delete(key);
7570

76-
return node.data.value;
71+
return node.value.data;
7772
}
7873
}
7974

lib/linkedList.js

Lines changed: 0 additions & 129 deletions
This file was deleted.

nodemon.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)