Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lru-object

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lru-object - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

dist/doublyLinkedList.d.ts

6

dist/index.d.ts

@@ -1,5 +0,1 @@

export default function createLRUCache<K, V>(capacity: number): {
[key: string]: V;
} | {
[key: number]: V;
};
export default function createLRUCache<K, V>(capacity: number): any;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lru_1 = require("./lru");
var lru_1 = require("./lru");
function createLRUCache(capacity) {
const lru = new lru_1.default(capacity);
const cache = lru.nodes;
const handler = {
get(_, prop) {
var lru = new lru_1.default(capacity);
var cache = lru.nodes;
var handler = {
get: function (_, prop) {
return lru.get(prop);
},
set(_, prop, value) {
set: function (_, prop, value) {
return lru.set(prop, value);
},
deleteProperty(_, prop) {
deleteProperty: function (_, prop) {
return lru.delete(prop);
},
ownKeys: function () {
return lru.keys();
},
getOwnPropertyDescriptor: function (_, prop) {
return { enumerable: true, configurable: true };
},
};

@@ -18,0 +24,0 @@ return new Proxy(cache, handler);

@@ -1,5 +0,12 @@

import Node from "./node";
import DoublyLinkedList from "./doublyLinkedList";
interface Iterator<K, V> {
next(value?: any): IteratorResult<K, V>;
return?(value?: any): IteratorResult<K, V>;
throw?(e?: any): IteratorResult<K, V>;
}
interface IteratorResult<K, V> {
done: boolean;
value: [K, V];
}
export default class LRU<K, V> {
head: Node<K, V>;
tail: Node<K, V>;
nodes: {

@@ -9,4 +16,6 @@ [key: string]: V;

[key: number]: V;
} | {
[Symbol.iterator]: () => Iterator<K, V>;
};
size: number;
list: DoublyLinkedList<K, V>;
capacity: number;

@@ -17,2 +26,4 @@ constructor(capacity: number);

get(key: string | number): V | undefined;
has(key: any): boolean;
keys(): string[] | number[];
delete(key: string | number): true;

@@ -22,1 +33,2 @@ private moveToHead;

}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const node_1 = require("./node");
class LRU {
constructor(capacity) {
this.head = new node_1.default();
this.tail = new node_1.default();
this.head.next = this.tail;
this.tail.prev = this.head;
var node_1 = require("./node");
var doublyLinkedList_1 = require("./doublyLinkedList");
var LRU = /** @class */ (function () {
function LRU(capacity) {
this.list = new doublyLinkedList_1.default();
this.nodes = {};
this.size = 0;
this.capacity = capacity;
}
peek(key) {
LRU.prototype.peek = function (key) {
if (this.nodes[key]) {

@@ -19,6 +16,6 @@ return this.nodes[key].value;

return undefined;
}
set(key, value) {
};
LRU.prototype.set = function (key, value) {
if (this.nodes[key]) {
const node = this.nodes[key];
var node = this.nodes[key];
node.value = value;

@@ -28,57 +25,71 @@ this.moveToHead(node);

}
if (this.size === this.capacity) {
if (this.list.size === this.capacity) {
this.removeFromTail();
}
const newNode = new node_1.default(key, value);
const next = this.head.next;
this.head.next = newNode;
next.prev = newNode;
newNode.prev = this.head;
newNode.next = next;
var newNode = new node_1.default(key, value);
this.list.add(newNode);
this.nodes[key] = newNode;
this.size++;
return true;
}
get(key) {
};
LRU.prototype.get = function (key) {
if (!this.nodes[key]) {
return undefined;
}
const result = this.nodes[key];
this.moveToHead(result);
return result.value;
}
delete(key) {
var result = this.nodes[key];
if (this.nodes.hasOwnProperty(key)) {
this.moveToHead(result);
return result.value;
}
else {
return result;
}
};
LRU.prototype.has = function (key) {
if (!this.nodes[key] || this.list.size === 0) {
return false;
}
var node = this.list.head.next;
while (node) {
if (node.key === key && node.next) {
return true;
}
node = node.next;
}
return false;
};
LRU.prototype.keys = function () {
var keys = [];
var node = this.list.head.next;
while (node) {
if (node.next) {
keys.push(node.key);
}
node = node.next;
}
return keys;
};
LRU.prototype.delete = function (key) {
if (!this.nodes[key]) {
return true;
}
const node = this.nodes[key];
const prev = node.prev;
const next = node.next;
prev.next = next;
next.prev = prev;
node.prev = null;
node.next = null;
var node = this.nodes[key];
this.list.remove(node);
delete this.nodes[key];
this.size--;
return true;
}
moveToHead(node) {
};
LRU.prototype.moveToHead = function (node) {
if (node.prev.prev === null) {
return;
}
const prev = node.prev;
const next = node.next;
prev.next = next;
next.prev = prev;
const headNext = this.head.next;
this.head.next = node;
node.prev = this.head;
node.next = headNext;
headNext.prev = node;
}
removeFromTail() {
const toBeRemoved = this.tail.prev;
this.delete(toBeRemoved.key);
}
}
this.list.remove(node);
this.list.add(node);
};
LRU.prototype.removeFromTail = function () {
var toBeRemoved = this.list.tail.prev;
if (toBeRemoved.prev) {
this.delete(toBeRemoved.key);
}
};
return LRU;
}());
exports.default = LRU;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Node {
constructor(key, value, prev, next) {
var Node = /** @class */ (function () {
function Node(key, value, prev, next) {
this.key = key;

@@ -10,3 +10,4 @@ this.value = value;

}
}
return Node;
}());
exports.default = Node;
{
"name": "lru-object",
"version": "1.0.2",
"version": "1.0.3",
"description": "Javascript object with basic LRU functionalities",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -21,5 +21,5 @@ # lru-object

### How to use
### Features
Create the state sync middleware with config:
1. LRU cache features will be applied invisibly:

@@ -38,8 +38,67 @@ ```javascript

// lru[1] is removed, current object { '2': 2, '3': 3, '4': 4 };
lru[2] = lru[4];
lru[5] = 5;
```
Please aware that the newly created Object `{...lru}` is nolonger a LRU cache but a plain javascript object.
2. Keys will follow the least recently used sequence
```javascript
import createLRUCache from 'lru-object';
const lru = createLRUCache<number, number>(3);
lru[1] = 1;
lru[2] = 2;
lru[3] = 3;
// reached capacity
lru[4] = 4;
console.log(Object.keys(lru));
// keys will follow the priority: ['4', '3', '2'];
```
3. Use it as normal object
```javascript
import createLRUCache from 'lru-object';
const lru = createLRUCache<number, number>(3);
lru['one'] = 1;
lru['two'] = 2;
lru['three'] = 3;
// reached capacity
delete lru['three']
for(const [k, v] of Object.entries(lru)) {
console.log(k, v);
}
for (const key in lru) {
if (lru.hasOwnProperty(key)) {
console.log(key);
}
}
```
### Things that are not working as expected
1. Spread operator is not following the least recently used sequence
```javascript
import createLRUCache from 'lru-object';
const lru = createLRUCache<number, number>(3);
lru[1] = 1;
lru[2] = 2;
lru[3] = 3;
console.log({...lru});
// lru[3] is removed, current object { '2': 4, '4': 4, '5': 5 };
// result: { '1': 1, '2': 2, '3': 3 };
// expected: { '3': 3, '2': 2, '1': 1 };
```
Please aware that the newly created Object `{...lru}` is nolonger a LRU cache but a plain javascript object.
### TODO
1. Some options for the LRU cache
2. Fix typescript issues
3. ...
{
"compilerOptions": {
"module": "commonjs",
"target": "es2015",
"target": "es5",
"declaration": true,
"outDir": "./dist"
"outDir": "./dist",
"lib": [ "es6" ]
},

@@ -8,0 +9,0 @@ "include": ["src/**/*"],

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc