Skip to content

Commit

Permalink
Moving has() into the class and into the README.md (want interop wi…
Browse files Browse the repository at this point in the history
…th common primitives), adding `entries()` & `values()`, & adding tests

Updating README.md
  • Loading branch information
avoidwork committed Sep 21, 2023
1 parent 6371a50 commit 535ff96
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Expand Up @@ -15,7 +15,8 @@
"after": true,
"before": true,
"beforeEach": true,
"expect": true
"expect": true,
"Iterator": true
},
"rules": {
"arrow-parens": [2, "as-needed"],
Expand Down
41 changes: 41 additions & 0 deletions README.md
Expand Up @@ -44,6 +44,20 @@ Removes item from cache
cache.delete("myKey");
```

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

Returns an `Array` cache items

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

**Example**

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

## evict
### Method

Expand Down Expand Up @@ -98,6 +112,19 @@ Gets cached item and moves it to the front
const item = cache.get("myKey");
```

## has
### Method

Returns a `Boolean` indicating if `key` is in cache

return {Object} LRU instance

**Example**

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

## keys
### Method

Expand Down Expand Up @@ -191,6 +218,20 @@ const cache = lru();
cache.ttl = 3e4;
```

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

Returns an `Array` cache items

param {Array} keys (Optional) Cache item keys to get
return {Array} Cache items

**Example**

```javascript
cache.values(['abc', 'def']);
```

## License
Copyright (c) 2023 Jason Mulligan
Licensed under the BSD-3 license.
26 changes: 17 additions & 9 deletions dist/tiny-lru.cjs
Expand Up @@ -3,14 +3,10 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 11.0.1
* @version 11.1.0
*/
'use strict';

function has (items, key) {
return key in items;
}

class LRU {
constructor (max = 0, ttl = 0, resetTtl = false) {
this.first = null;
Expand All @@ -32,7 +28,7 @@ class LRU {
}

delete (key) {
if (has(this.items, key)) {
if (this.has(key)) {
const item = this.items[key];

delete this.items[key];
Expand All @@ -58,6 +54,10 @@ class LRU {
return this;
}

entries (keys = this.keys()) {
return keys.map(key => [key, this.get(key)]);
}

evict (bypass = false) {
if (bypass || this.size > 0) {
const item = this.first;
Expand All @@ -79,7 +79,7 @@ class LRU {
expiresAt (key) {
let result;

if (has(this.items, key)) {
if (this.has(key)) {
result = this.items[key].expiry;
}

Expand All @@ -89,7 +89,7 @@ class LRU {
get (key) {
let result;

if (has(this.items, key)) {
if (this.has(key)) {
const item = this.items[key];

if (this.ttl > 0 && item.expiry <= Date.now()) {
Expand All @@ -103,14 +103,18 @@ class LRU {
return result;
}

has (key) {
return key in this.items;
}

keys () {
return Object.keys(this.items);
}

set (key, value, bypass = false, resetTtl = this.resetTtl) {
let item;

if (bypass || has(this.items, key)) {
if (bypass || this.has(key)) {
item = this.items[key];
item.value = value;

Expand Down Expand Up @@ -163,6 +167,10 @@ class LRU {

return this;
}

values (keys = this.keys()) {
return keys.map(key => this.get(key));
}
}

function lru (max = 1000, ttl = 0, resetTtl = false) {
Expand Down
26 changes: 18 additions & 8 deletions dist/tiny-lru.js
Expand Up @@ -3,11 +3,9 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 11.0.1
* @version 11.1.0
*/
function has (items, key) {
return key in items;
}class LRU {
class LRU {
constructor (max = 0, ttl = 0, resetTtl = false) {
this.first = null;
this.items = Object.create(null);
Expand All @@ -28,7 +26,7 @@ function has (items, key) {
}

delete (key) {
if (has(this.items, key)) {
if (this.has(key)) {
const item = this.items[key];

delete this.items[key];
Expand All @@ -54,6 +52,10 @@ function has (items, key) {
return this;
}

entries (keys = this.keys()) {
return keys.map(key => [key, this.get(key)]);
}

evict (bypass = false) {
if (bypass || this.size > 0) {
const item = this.first;
Expand All @@ -75,7 +77,7 @@ function has (items, key) {
expiresAt (key) {
let result;

if (has(this.items, key)) {
if (this.has(key)) {
result = this.items[key].expiry;
}

Expand All @@ -85,7 +87,7 @@ function has (items, key) {
get (key) {
let result;

if (has(this.items, key)) {
if (this.has(key)) {
const item = this.items[key];

if (this.ttl > 0 && item.expiry <= Date.now()) {
Expand All @@ -99,14 +101,18 @@ function has (items, key) {
return result;
}

has (key) {
return key in this.items;
}

keys () {
return Object.keys(this.items);
}

set (key, value, bypass = false, resetTtl = this.resetTtl) {
let item;

if (bypass || has(this.items, key)) {
if (bypass || this.has(key)) {
item = this.items[key];
item.value = value;

Expand Down Expand Up @@ -159,6 +165,10 @@ function has (items, key) {

return this;
}

values (keys = this.keys()) {
return keys.map(key => this.get(key));
}
}

function lru (max = 1000, ttl = 0, resetTtl = false) {
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 535ff96

Please sign in to comment.