Skip to content

Commit

Permalink
Implementing nyc for code coverage & adding tests to get 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Sep 21, 2023
1 parent 3caa373 commit eb7d2c8
Show file tree
Hide file tree
Showing 14 changed files with 1,898 additions and 299 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
/node_modules/
/test/webpack/
.idea
.nyc_output
*.tgz
1 change: 1 addition & 0 deletions .npmignore
@@ -1,4 +1,5 @@
.idea
.nyc_output
src
test
.eslintrc
Expand Down
189 changes: 96 additions & 93 deletions README.md
Expand Up @@ -10,216 +10,219 @@ const cache = lru(max, ttl = 0, resetTtl = false);
Lodash provides a `memoize` function with a cache that can be swapped out as long as it implements the right interface.
See the [lodash docs](https://lodash.com/docs#memoize) for more on `memoize`.

#### Example
## Example
```javascript
_.memoize.Cache = lru().constructor;
const memoized = _.memoize(myFunc);
memoized.cache.max = 10;
```

## clear
### Method
## Testing

Clears the contents of the cache
Tiny-LRU has 100% code coverage with its tests.

return {Object} LRU instance
```console
--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 100 | 89.85 | 100 | 100 |
tiny-lru.cjs | 100 | 89.85 | 100 | 100 | 11-31,130-138,172
--------------|---------|----------|---------|---------|-------------------
```

## API

## Properties

### first

Item in "first" or "bottom" position; default is `null`

**Example**

```javascript
cache.clear();
const cache = lru();

cache.first; // null - it's a new cache!
```

## delete
### Method
### last

Removes item from cache

param {String} key Item key
return {Object} LRU instance
Item in "last" or "top" position; default is `null`

**Example**

```javascript
cache.delete("myKey");
```
const cache = lru();

## entries(*["key1", "key2"]*)
### Method
cache.last; // null - it's a new cache!
```

Returns an `Array` cache items
### max

param {Array} keys (Optional) Cache item keys to get
return {Object} LRU instance
Max items to hold in cache; default is `1000`

**Example**

```javascript
cache.entries(['myKey1', 'myKey2']);
const cache = lru(500);

cache.max; // 500
```

## evict
### Method
### resetTtl

Evicts the least recently used item from cache

return {Object} LRU instance
Resets `item.expiry` with each `set()` if `true`; default is `false`

**Example**

```javascript
cache.evict();
const cache = lru(500, 5*6e4, true);

cache.resetTtl; // true
```

## expiresAt
### Method
### size

Gets expiration time for cached item

param {String} key Item key
return {Mixed} Undefined or number (epoch time)
Number of items in cache

**Example**

```javascript
const item = cache.expiresAt("myKey");
const cache = lru();

cache.size; // 0 - it's a new cache!
```

## first
### Property
### ttl

Item in "first" or "bottom" position
Milliseconds an item will remain in cache; lazy expiration upon next `get()` of an item

**Example**

```javascript
const cache = lru();
const cache = lru(100, 3e4);

cache.first; // null - it's a new cache!
cache.ttl; // 30000;
```

## get
### Method
## Methods

Gets cached item and moves it to the front
### clear

param {String} key Item key
return {Mixed} Undefined or Item value
Clears the contents of the cache

return {Object} LRU instance

**Example**

```javascript
const item = cache.get("myKey");
cache.clear();
```

## has
### Method
### delete

Returns a `Boolean` indicating if `key` is in cache
Removes item from cache

return {Object} LRU instance
param {String} key Item key
return {Object} LRU instance

**Example**

```javascript
cache.has('myKey');
cache.delete("myKey");
```

## keys
### Method
### entries(*["key1", "key2"]*)

Returns an `Array` of cache item keys.
Returns an `Array` cache items

return {Array} Array of keys
param {Array} keys (Optional) Cache item keys to get
return {Object} LRU instance

**Example**

```javascript
console.log(cache.keys());
cache.entries(['myKey1', 'myKey2']);
```

## max
### Property
### evict

Max items to hold in cache (1000)
Evicts the least recently used item from cache

return {Object} LRU instance

**Example**

```javascript
const cache = lru(500);

cache.max; // 500
cache.evict();
```

## last
### Property
### expiresAt

Item in "last" or "top" position
Gets expiration time for cached item

param {String} key Item key
return {Mixed} Undefined or number (epoch time)

**Example**

```javascript
const cache = lru();

cache.last; // null - it's a new cache!
const item = cache.expiresAt("myKey");
```

## resetTtl
### Property
### get

Resets `item.expiry` with each `set()` if `true` (false)
Gets cached item and moves it to the front

param {String} key Item key
return {Mixed} Undefined or Item value

**Example**

```javascript
const cache = lru();

cache.resetTtl; // false
const item = cache.get("myKey");
```

## set
### Method
### has

Sets item in cache as `first`
Returns a `Boolean` indicating if `key` is in cache

param {String} key Item key
param {Mixed} value Item value
return {Object} LRU instance
return {Object} LRU instance

**Example**

```javascript
cache.set("myKey", {prop: true});
cache.has('myKey');
```

## size
### Property
### keys

Number of items in cache
Returns an `Array` of cache item keys.

return {Array} Array of keys

**Example**

```javascript
const cache = lru();

cache.size; // 0 - it's a new cache!
console.log(cache.keys());
```

## ttl
### Property
### set

Milliseconds an item will remain in cache; lazy expiration upon next `get()` of an item
Sets item in cache as `first`

param {String} key Item key
param {Mixed} value Item value
return {Object} LRU instance

**Example**

```javascript
const cache = lru();

cache.ttl = 3e4;
cache.set("myKey", {prop: true});
```

## values(*["key1", "key2"]*)
### Method
### values(*["key1", "key2"]*)

Returns an `Array` cache items

Expand Down
6 changes: 1 addition & 5 deletions dist/tiny-lru.cjs
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 11.1.1
* @version 11.1.2
*/
'use strict';

Expand Down Expand Up @@ -135,10 +135,6 @@ class LRU {
item.prev = this.last;
last.next = item;

if (prev !== null) {
prev.next = next;
}

if (next !== null) {
next.prev = prev;
}
Expand Down
6 changes: 1 addition & 5 deletions dist/tiny-lru.js
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 11.1.1
* @version 11.1.2
*/
class LRU {
constructor (max = 0, ttl = 0, resetTtl = false) {
Expand Down Expand Up @@ -133,10 +133,6 @@ class LRU {
item.prev = this.last;
last.next = item;

if (prev !== null) {
prev.next = next;
}

if (next !== null) {
next.prev = prev;
}
Expand Down
4 changes: 2 additions & 2 deletions dist/tiny-lru.min.js

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

0 comments on commit eb7d2c8

Please sign in to comment.