Skip to content

Commit

Permalink
Updating build and changing ./lib to ./dist, removing default exp…
Browse files Browse the repository at this point in the history
…ort for named `lru` export, adding `.npmignore` file
  • Loading branch information
avoidwork committed Sep 21, 2022
1 parent be2111e commit 981cc72
Show file tree
Hide file tree
Showing 20 changed files with 3,127 additions and 7,607 deletions.
14 changes: 13 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
"es6": true,
"amd": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"globals": {
"describe": true,
"it": true,
"after": true,
"before": true,
"beforeEach": true,
"expect": true
},
"rules": {
"arrow-parens": [2, "as-needed"],
"arrow-spacing": [2, {"before": true, "after": true}],
Expand Down Expand Up @@ -162,4 +174,4 @@
"wrap-regex": [2],
"yoda": [2, "never", { "exceptRange": true }]
}
}
}
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
src
test
.eslintrc
.gitignore
rollup.config.js
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

Least Recently Used cache for Client or Server.

[![build status](https://secure.travis-ci.org/avoidwork/tiny-lru.svg)](http://travis-ci.org/avoidwork/tiny-lru)

```javascript
import {lru} from "tiny-lru";
const cache = lru(max, ttl = 0);
```

Expand Down
16 changes: 8 additions & 8 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require("path"),
lru = require(path.join(__dirname, "lib", "tiny-lru.js")),
precise = require("precise"),
nth = 2e3,
import {lru} from "./dist/tiny-lru.esm.js";
import {precise} from "precise";

const nth = 2e3,
cache = lru(nth),
data = new Array(nth);

Expand All @@ -14,19 +14,19 @@ function seed () {
}

function populate (arg, start = 0) {
const nth = arg.max;
const pnth = arg.max;
let i = -1;

while (++i < nth) {
while (++i < pnth) {
arg.set(i + start, data[i]);
}
}

function get (arg, start = 0) {
const nth = arg.max;
const gnth = arg.max;
let i = -1;

while (++i < nth) {
while (++i < gnth) {
arg.get(i + start, data[i]);
}
}
Expand Down
32 changes: 0 additions & 32 deletions bower.json

This file was deleted.

168 changes: 168 additions & 0 deletions dist/tiny-lru.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/**
* tiny-lru
*
* @copyright 2022 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 9.0.0
*/
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

class LRU {
constructor (max = 0, ttl = 0) {
this.first = null;
this.items = Object.create(null);
this.last = null;
this.max = max;
this.size = 0;
this.ttl = ttl;
}

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

clear () {
this.first = null;
this.items = Object.create(null);
this.last = null;
this.size = 0;

return this;
}

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

delete this.items[key];
this.size--;

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

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

if (this.first === item) {
this.first = item.next;
}

if (this.last === item) {
this.last = item.prev;
}
}

return this;
}

evict (bypass = false) {
if (bypass || this.size > 0) {
const item = this.first;

delete this.items[item.key];
this.size--;

if (this.size === 0) {
this.first = null;
this.last = null;
} else {
this.first = item.next;
this.first.prev = null;
}
}

return this;
}

get (key) {
let result;

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

if (this.ttl > 0 && item.expiry <= new Date().getTime()) {
this.delete(key);
} else {
result = item.value;
this.set(key, result, true);
}
}

return result;
}

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

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

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

if (this.last !== item) {
const last = this.last,
next = item.next,
prev = item.prev;

if (this.first === item) {
this.first = item.next;
}

item.next = null;
item.prev = this.last;
last.next = item;

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

if (next !== null) {
next.prev = prev;
}
}
} else {
if (this.max > 0 && this.size === this.max) {
this.evict(true);
}

item = this.items[key] = {
expiry: this.ttl > 0 ? new Date().getTime() + this.ttl : this.ttl,
key: key,
prev: this.last,
next: null,
value
};

if (++this.size === 1) {
this.first = item;
} else {
this.last.next = item;
}
}

this.last = item;

return this;
}
}

function lru (max = 1000, ttl = 0) {
if (isNaN(max) || max < 0) {
throw new TypeError("Invalid max value");
}

if (isNaN(ttl) || ttl < 0) {
throw new TypeError("Invalid ttl value");
}

return new LRU(max, ttl);
}

exports.lru = lru;
Loading

0 comments on commit 981cc72

Please sign in to comment.