Skip to content

Commit

Permalink
Merge branch 'main' into release-please--branches--main--components--…
Browse files Browse the repository at this point in the history
…lru.min
  • Loading branch information
wellwelwel authored Aug 27, 2024
2 parents 910b00d + 3bd7033 commit 745b9f2
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 5 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<h1 align="center">lru.min</h1>
<div align="center">

[![NPM Version](https://img.shields.io/npm/v/lru.min.svg?label=&color=70a1ff&logo=npm&logoColor=white)](https://www.npmjs.com/package/lru.min)
[![Coverage](https://img.shields.io/codecov/c/github/wellwelwel/lru.min?label=&logo=codecov&logoColor=white&color=98cc00)](https://github.com/wellwelwel/lru.min/tree/main/.nycrc)<br />
[![GitHub Workflow Status (Node.js)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_node.yml?event=push&label=&branch=main&logo=nodedotjs&logoColor=535c68&color=badc58)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_node.yml?query=branch%3Amain)
[![GitHub Workflow Status (Bun)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_bun.yml?event=push&label=&branch=main&logo=bun&logoColor=ffffff&color=f368e0)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_bun.yml?query=branch%3Amain)
[![GitHub Workflow Status (Deno)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_deno.yml?event=push&label=&branch=main&logo=deno&logoColor=ffffff&color=079992)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_deno.yml?query=branch%3Amain)

🔥 An extremely fast and efficient <strong><a href="https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29">LRU</a> Cache</strong> for <strong>JavaScript</strong> (<strong>Browser</strong> compatible) — **6.4KB**.

</div>
Expand Down Expand Up @@ -32,9 +38,6 @@ deno add npm:lru.min

## Usage

> - ⚠️ Please wait until `v1.x.x` before using this package.
> - 📘 Public repository coming soon.
### Import

#### ES Modules
Expand Down Expand Up @@ -266,8 +269,8 @@ See the [**Contributing Guide**](https://github.com/wellwelwel/lru.min/blob/main

## Acknowledgements

> **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.
>
**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 🤝
- 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).
Expand Down
279 changes: 279 additions & 0 deletions test/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
import { describe, it, assert } from 'poku';
import { createLRU } from '../src/index.ts';

const snapshop = {
level1: {
stringProperty:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor.',
numberProperty: 9876543210,
arrayProperty: [
{
deepNestedArray: [
{
deeperLevel: [new Set([1, 2, 3]), new Map([['key1', 'value1']])],
},
new Array(10).fill('string').map((_, i) => Symbol(`symbol_${i}`)),
new Map([
[
'38KijMDwYyYC9P9b1dYJgKmfvscVerilhkhzzYtXlIZh8AyVKe',
{
furtherNesting: new Map([
[
Symbol('unique3'),
{
evenDeeper: [
'A',
'B',
'C',
new Map([
[Symbol('deepKey'), [1, 2, 3]],
[Symbol('deepKey2'), { a: 'a', b: 'b' }],
]),
],
},
],
]),
},
],
]),
],
},
{
anotherNestedArray: new Array(5).fill(0).map(() => Math.random()),
},
],
mapProperty: new Map<unknown, unknown>([
['key1', 'value1'],
[
'key2',
{
subMap: new Map([
[
'subKey2',
{
veryDeep: {
level3: {
setExample: new Set([
1,
2,
3,
{ nestedSet: new Set(['A', 'B', 'C']) },
]),
complexObjectInside: {
deeplyNestedArray: new Array(3).fill(0).map(() => ({
deeperLevel: {
arrayInArray: [
[1, 2, 3],
[4, 5, 6],
],
mapInArray: [
new Map([
['mapKey2', new Set(['set1', 'set2', 'set3'])],
]),
Symbol('deepSymbol'),
],
},
})),
},
},
},
},
],
]),
},
],
]),
},
anotherLevel1: {
complexArray: new Array(10).fill(null).map((_, index) => ({
nestedMap: new Map([
[
`key_${index}`,
new Set([
Symbol(`symbol_${index}`),
Symbol(`another_symbol_${index}`),
]),
],
]),
})),
},
functionInObject: () =>
'This is a string returned by a function in the object.',
anotherFunctionInObject: () => ({
deeplyNestedFuncReturn: {
deeplyNestedArray: [
'String element',
123456,
new Map([
[
Symbol('nestedMapKey'),
{
anotherLevelDown: [
new Set([1, 2, 3]),
new Array(5).fill('deeplyNestedString'),
],
},
],
]),
],
},
}),
[Symbol('uniqueSymbolKey')]: {
deeplyNestedWithSymbolKey: {
level2: {
level3: {
setInsideSymbolKey: new Set([1, 2, 3]),
mapInsideSymbolKey: new Map([['key1', 'value1']]),
},
},
},
},
};

describe('Types Suite', () => {
it('should match a symbol key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = Symbol('undefined');

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);

assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match an object key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = {
test: true,
};

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match an intentional undefined key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = undefined;

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match an number key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = 941235;

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match a number key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = 941235;

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match a big int key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = 941283745934857639487563945235n;

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match a multi line key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = `
ln
`;

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match a `false` key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = false;

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match a `true` key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = true;

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match a complex key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

LRU.set(snapshop, snapshop);

assert.deepStrictEqual(LRU.get(snapshop), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[snapshop, snapshop]]);
});

it('should match an emoji key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = '🧑🏻‍🔬';

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match an emoji key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = '🧑🏻‍🔬';

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});

it('should match an ideogram key', () => {
const LRU = createLRU<unknown, unknown>({ max: 5 });

const key = 'テスト試験בדיקה测试測試тест';

LRU.set(key, snapshop);

assert.deepStrictEqual(LRU.get(key), snapshop);
assert.deepStrictEqual([...LRU.entries()], [[key, snapshop]]);
});
});

0 comments on commit 745b9f2

Please sign in to comment.