Skip to content

Commit

Permalink
feat(typings): expose CacheOptions type (#38)
Browse files Browse the repository at this point in the history
* feat(typings): support for specifying key-value types

* chore: use `CacheOptions` name
  • Loading branch information
wellwelwel authored Aug 28, 2024
1 parent b436a33 commit 47930f4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ LRU.forEach((value, key) => {
You can set types for both keys and values. For example:

```ts
import { createLRU } from 'lru.min';

type Key = number;

type Value = {
Expand All @@ -265,6 +267,31 @@ LRU.set(1, { name: 'Peter' });
LRU.set(2, { name: 'Mary' });
```

Also:

```ts
import { createLRU, type CacheOptions } from 'lru.min';

type Key = number;

type Value = {
name: string;
};

const options: CacheOptions<Key, Value> = {
max: 10,
onEviction(key, value) {
console.log(key, value);
},
};

// No need to repeat the type params
const LRU = createLRU(options);

LRU.set(1, { name: 'Peter' });
LRU.set(2, { name: 'Mary' });
```

---

### Performance
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export const createLRU = <Key, Value>(options: {
export type CacheOptions<Key = unknown, Value = unknown> = {
/** Maximum number of items the cache can hold. */
max: number;
/** Function called when an item is evicted from the cache. */
onEviction?: (key: Key, value: Value) => unknown;
}) => {
};

export const createLRU = <Key, Value>(options: CacheOptions<Key, Value>) => {
let { max, onEviction } = options;

if (!(Number.isInteger(max) && max > 0))
Expand Down
59 changes: 58 additions & 1 deletion test/quickstart.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, assert } from 'poku';
import { createLRU } from '../src/index.ts';
import { createLRU, type CacheOptions } from '../src/index.ts';

describe('Quickstart example test', () => {
it('Default', () => {
Expand Down Expand Up @@ -139,4 +139,61 @@ describe('Quickstart example test', () => {
'Key "C" with value "Another Value" has been evicted.',
]);
});

it('Type Test', () => {
const options: CacheOptions = {
max: 10,
onEviction(key, value) {
console.log(key, value);
},
};

createLRU(options);
});

it('Type Test: Params on `CacheOptions`', () => {
const options: CacheOptions<number, string> = {
max: 10,
onEviction(key, value) {
console.log(key, value);
},
};

createLRU(options).set(1, 'Test');
// @ts-expect-error
createLRU<number, string>(options).set('2', 'Test');
});

it('Type Test: Params in `createLRU`', () => {
const options: CacheOptions = {
max: 10,
onEviction(key, value) {
console.log(key, value);
},
};

createLRU<number, string>(options).set(1, 'Test');
// @ts-expect-error
createLRU<number, string>(options).set('2', 'Test');
});

it('Type Test: README.md', () => {
type Key = number;

type Value = {
name: string;
};

const options: CacheOptions<Key, Value> = {
max: 10,
onEviction(key, value) {
console.log(key, value);
},
};

const LRU = createLRU(options);

LRU.set(1, { name: 'Peter' });
LRU.set(2, { name: 'Mary' });
});
});

0 comments on commit 47930f4

Please sign in to comment.