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

feat(typings): expose CacheOptions type #38

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
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' });
});
});
Loading