Skip to content

Commit

Permalink
Merge PR #4 from 'nodech/update-types-lint'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Oct 2, 2024
2 parents 6924584 + 7cc5925 commit ea1f616
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
15 changes: 2 additions & 13 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
{
"env": {
"es6": true,
"es2022": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readable",
"BigInt": "readable",
"BigInt64Array": "readable",
"BigUint64Array": "readable",
"queueMicrotask": "readable",
"SharedArrayBuffer": "readable",
"TextEncoder": "readable",
"TextDecoder": "readable"
},
"overrides": [
{
"files": ["*.mjs"],
Expand Down Expand Up @@ -44,9 +34,8 @@
}
}
],
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 10,
"ecmaVersion": 13,
"ecmaFeatures": {
"globalReturn": true
},
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ jobs:
- name: Lint
run: npm run lint

- name: Lint types
run: npm run lint-types


test:
name: Test
runs-on: ${{ matrix.os }}
Expand Down
74 changes: 45 additions & 29 deletions lib/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ const assert = require('bsert');

/**
* LRU Cache
* @template K - Key type
* @template V - Value type
*/

class LRU {
/** @type {Map} */
/** @type {Map<K, LRUItem<K, V>>} */
map;

/**
* Create an LRU cache.
* @constructor
* @param {Number} capacity
* @param {Function?} getSize
* @param {Function?} CustomMap
* @param {Function} [getSize]
* @param {Function} [CustomMap]
*/

constructor(capacity, getSize, CustomMap) {
Expand All @@ -34,8 +36,11 @@ class LRU {
this.map = CustomMap ? new CustomMap() : new Map();
this.size = 0;
this.items = 0;
/** @type {LRUItem<K, V>?} */
this.head = null;
/** @type {LRUItem<K, V>?} */
this.tail = null;
/** @type {LRUBatch<K, V>?} */
this.pending = null;

this.capacity = capacity;
Expand All @@ -45,7 +50,7 @@ class LRU {
/**
* Calculate size of an item.
* @private
* @param {LRUItem} item
* @param {LRUItem<K, V>} item
* @returns {Number} Size.
*/

Expand All @@ -65,7 +70,9 @@ class LRU {
if (this.size <= this.capacity)
return;

/** @type {LRUItem<K, V>?} */
let item = null;
/** @type {LRUItem<K, V>?} */
let next = null;

for (item = this.head; item; item = next) {
Expand Down Expand Up @@ -116,8 +123,8 @@ class LRU {

/**
* Add an item to the cache.
* @param {String|Number} key
* @param {Object} value
* @param {K} key
* @param {V} value
*/

set(key, value) {
Expand Down Expand Up @@ -150,8 +157,8 @@ class LRU {

/**
* Retrieve an item from the cache.
* @param {String|Number} key
* @returns {Object} Item.
* @param {K} key
* @returns {V} Item.
*/

get(key) {
Expand All @@ -171,7 +178,7 @@ class LRU {

/**
* Test whether the cache contains a key.
* @param {String|Number} key
* @param {K} key
* @returns {Boolean}
*/

Expand All @@ -183,7 +190,7 @@ class LRU {

/**
* Remove an item from the cache.
* @param {String|Number} key
* @param {K} key
* @returns {Boolean} Whether an item was removed.
*/

Expand All @@ -209,7 +216,7 @@ class LRU {
/**
* Append an item to the linked list (sets new tail).
* @private
* @param {LRUItem} item
* @param {LRUItem<K, V>} item
*/

_appendList(item) {
Expand All @@ -219,8 +226,8 @@ class LRU {
/**
* Insert item into the linked list.
* @private
* @param {LRUItem|null} ref
* @param {LRUItem} item
* @param {LRUItem<K, V>?} ref
* @param {LRUItem<K, V>} item
*/

_insertList(ref, item) {
Expand Down Expand Up @@ -253,7 +260,7 @@ class LRU {
/**
* Remove item from the linked list.
* @private
* @param {LRUItem} item
* @param {LRUItem<K, V>} item
*/

_removeList(item) {
Expand Down Expand Up @@ -281,7 +288,7 @@ class LRU {

/**
* Collect all keys in the cache, sorted by LRU.
* @returns {String[]}
* @returns {K[]}
*/

keys() {
Expand All @@ -302,7 +309,7 @@ class LRU {

/**
* Collect all values in the cache, sorted by LRU.
* @returns {String[]}
* @returns {V[]}
*/

values() {
Expand All @@ -316,7 +323,7 @@ class LRU {

/**
* Convert the LRU cache to an array of items.
* @returns {Object[]}
* @returns {LRUItem<K, V>[]}
*/

toArray() {
Expand All @@ -331,7 +338,7 @@ class LRU {
/**
* Create an atomic batch for the lru
* (used for caching database writes).
* @returns {LRUBatch}
* @returns {LRUBatch<K, V>}
*/

batch() {
Expand Down Expand Up @@ -377,8 +384,8 @@ class LRU {

/**
* Push an item onto the pending batch.
* @param {String} key
* @param {Object} value
* @param {K} key
* @param {V} value
*/

push(key, value) {
Expand All @@ -392,7 +399,7 @@ class LRU {

/**
* Push a removal onto the pending batch.
* @param {String} key
* @param {K} key
*/

unpush(key) {
Expand All @@ -408,45 +415,52 @@ class LRU {
/**
* LRU Item
* @alias module:utils.LRUItem
* @template K - Key type
* @template V - Value type
*/

class LRUItem {
/**
* Create an LRU item.
* @constructor
* @param {String|Number} key
* @param {Object} value
* @param {K} key
* @param {V} value
*/

constructor(key, value) {
this.key = key;
this.value = value;
/** @type {LRUItem<K, V>?} */
this.next = null;
/** @type {LRUItem<K, V>?} */
this.prev = null;
}
}

/**
* LRU Batch
* @alias module:utils.LRUBatch
* @template K - Key type
* @template V - Value type
*/

class LRUBatch {
/**
* Create an LRU batch.
* @constructor
* @param {LRU} lru
* @param {LRU<K, V>} lru
*/

constructor(lru) {
this.lru = lru;
/** @type {LRUOp<K, V>[]} */
this.ops = [];
}

/**
* Push an item onto the batch.
* @param {String} key
* @param {Object} value
* @param {K} key
* @param {V} value
*/

set(key, value) {
Expand All @@ -455,7 +469,7 @@ class LRUBatch {

/**
* Push a removal onto the batch.
* @param {String} key
* @param {K} key
*/

remove(key) {
Expand Down Expand Up @@ -490,6 +504,8 @@ class LRUBatch {
/**
* LRU Op
* @alias module:utils.LRUOp
* @template K
* @template V
* @private
*/

Expand All @@ -498,8 +514,8 @@ class LRUOp {
* Create an LRU op.
* @constructor
* @param {Boolean} remove
* @param {String} key
* @param {Object} value
* @param {K} key
* @param {V} value
*/

constructor(remove, key, value) {
Expand Down

0 comments on commit ea1f616

Please sign in to comment.