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

docs: fix typos and improve examples #33

Merged
merged 2 commits into from
Aug 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

## Why another LRU?

- 🎖️ **lru.min** is fully compatible with both **Node.js** _(8+)_, **Bun**, **Deno** and, browser environments. All of this, while maintaining the same high performance _(and a little more)_.
- 🎖️ **lru.min** is fully compatible with both **Node.js** _(8+)_, **Bun**, **Deno** and, browser environments. All of this, while maintaining the same high performance [_(and a little more)_](https://github.com/wellwelwel/lru.min?tab=readme-ov-file#performance).

---

Expand Down Expand Up @@ -102,7 +102,7 @@ LRU.get('key');
Retrieves the value for a given key without changing its position.

```ts
LRU.get('key');
LRU.peek('key');
```

### Check if a key exists
Expand Down Expand Up @@ -161,41 +161,35 @@ LRU.size;
LRU.available;
```

### Iterating the cache

#### Get all keys

Iterates over all keys in the cache, from least recent to most recent.
Iterates over all keys in the cache, from most recent to least recent.

```ts
const keys = [...LRU.keys()];
```

#### Get all values

Iterates over all values in the cache, from least recent to most recent.
Iterates over all values in the cache, from most recent to least recent.

```ts
const keys = [...LRU.values()];
```

#### Get all entries

Iterates over `[key, value]` pairs in the cache, from least recent to most recent.

```ts
const entries = [...LRU.entries()];
```

#### Get all entries

Iterates over `[key, value]` pairs in the cache, from least recent to most recent.
Iterates over `[key, value]` pairs in the cache, from most recent to least recent.

```ts
const entries = [...LRU.entries()];
```

#### Run a callback for each entry

Iterates over each key-value pair in the cache, from most recent to least recent.
Iterates over each value-key pair in the cache, from most recent to least recent.

```ts
LRU.forEach((value, key) => {
Expand Down Expand Up @@ -246,14 +240,42 @@ See the [**Contributing Guide**](https://github.com/wellwelwel/lru.min/blob/main

**lru.min** is based and inspired on the architecture and code of both [**lru-cache**](https://github.com/isaacs/node-lru-cache) and [**quick-lru**](https://github.com/sindresorhus/quick-lru), simplifying their core concepts for enhanced performance and compatibility.

> For more comprehensive features such as **TTL** support, consider using and supporting them 🤝
For more comprehensive features such as **TTL** support, consider using and supporting them 🤝

- The architecture is mostly based on [@isaacs](https://github.com/isaacs) — [**lru-cache**](https://github.com/isaacs/node-lru-cache/blob/8f51d75351cbb4ac819952eb8e9f95eda00ef800/src/index.ts).
- Most of the methods names and its functionalities were inspired by [@sindresorhus](https://github.com/sindresorhus) — [**quick-lru**](https://github.com/sindresorhus/quick-lru/blob/a2262c65e1952539cb4d985a67c46363a780d234/index.js).
- [![Contributors](https://img.shields.io/github/contributors/wellwelwel/lru.min?label=Contributors)](https://github.com/wellwelwel/lru.min/graphs/contributors)

---

#### What comes from [**lru-cache**](https://github.com/isaacs/node-lru-cache)?

- _Not the same, but majority based on:_

```ts
let free: number[] = [];

const keyMap: Map<Key, number> = new Map();
const keyList: (Key | undefined)[] = new Array(max).fill(undefined);
const valList: (Value | undefined)[] = new Array(max).fill(undefined);
const next: number[] = new Array(max).fill(0);
const prev: number[] = new Array(max).fill(0);
```

---

#### What comes from [**quick-lru**](https://github.com/sindresorhus/quick-lru)?

Name of methods and options _(including its final functionality idea)_:

- `resize`
- `peek`
- `onEviction`
- `forEach`
- `entriesDescending` as `entries`

---

## License

**lru.min** is under the [**MIT License**](https://github.com/wellwelwel/lru.min/blob/main/LICENSE).<br />
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const createLRU = <Key, Value>(options: {
/** Checks if a key exists in the cache. */
has: (key: Key): boolean => keyMap.has(key),

/** Iterates over all keys in the cache, from least recent to most recent. */
/** Iterates over all keys in the cache, from most recent to least recent. */
*keys(): IterableIterator<Key> {
let current = tail;

Expand All @@ -109,7 +109,7 @@ export const createLRU = <Key, Value>(options: {
}
},

/** Iterates over all values in the cache, from least recent to most recent. */
/** Iterates over all values in the cache, from most recent to least recent. */
*values(): IterableIterator<Value> {
let current = tail;

Expand All @@ -119,7 +119,7 @@ export const createLRU = <Key, Value>(options: {
}
},

/** Iterates over `[key, value]` pairs in the cache, from least recent to most recent. */
/** Iterates over `[key, value]` pairs in the cache, from most recent to least recent. */
*entries(): IterableIterator<[Key, Value]> {
let current = tail;

Expand All @@ -129,7 +129,7 @@ export const createLRU = <Key, Value>(options: {
}
},

/** Iterates over each key-value pair in the cache, from most recent to least recent. */
/** Iterates over each value-key pair in the cache, from most recent to least recent. */
forEach: (callback: (value: Value, key: Key) => unknown): undefined => {
let current = tail;

Expand Down
Loading