Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating folder structure & named export #53

Merged
merged 6 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

471 changes: 240 additions & 231 deletions CHANGELOG.md

Large diffs are not rendered by default.

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;
25 changes: 25 additions & 0 deletions dist/tiny-lru.esm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function lru(max?: number, ttl?: number): LRU;
/**
* tiny-lru
*
* @copyright 2022 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 9.0.0
*/
declare class LRU {
constructor(max?: number, ttl?: number);
first: any;
items: any;
last: any;
max: number;
size: number;
ttl: number;
has(key: any): boolean;
clear(): LRU;
delete(key: any): LRU;
evict(bypass?: boolean): LRU;
get(key: any): any;
keys(): string[];
set(key: any, value: any, bypass?: boolean): LRU;
}
export {};