@lunibee/collection
The @lunibee/collection package provides high-performance in-memory data structures engineered specifically for Bun and Discord bots.
Installation
Section titled “Installation”bun add @lunibee/collectionCollection<K, V>
Section titled “Collection<K, V>”The Collection class extends Map<K, V> and provides rich array-like manipulation methods without allocating unnecessary intermediate arrays.
import { Collection } from "@lunibee/collection";
const users = new Collection<string, { id: string; username: string; bot: boolean }>();
users.set("1001", { id: "1001", username: "Alice", bot: false });users.set("1002", { id: "1002", username: "Bob", bot: false });users.set("1003", { id: "1003", username: "ZedBot", bot: true });Methods Reference
Section titled “Methods Reference”first() / first(amount)
Section titled “first() / first(amount)”- Parameters:
amount?: number - Returns:
V | V[] | undefined
const firstUser = users.first();const firstTwo = users.first(2);firstKey() / firstKey(amount)
Section titled “firstKey() / firstKey(amount)”- Returns:
K | K[] | undefined
const firstId = users.firstKey();find(predicate)
Section titled “find(predicate)”- Parameters:
predicate: (value: V, key: K, collection: this) => boolean - Returns:
V | undefined
const botUser = users.find(u => u.bot === true);filter(predicate)
Section titled “filter(predicate)”- Parameters:
predicate: (value: V, key: K, collection: this) => boolean - Returns:
Collection<K, V>
const humans = users.filter(u => !u.bot);map(transform)
Section titled “map(transform)”- Parameters:
transform: (value: V, key: K, collection: this) => T - Returns:
T[]
const usernames = users.map(u => u.username);sweep(predicate)
Section titled “sweep(predicate)”- Returns:
number(count of removed entries)
const deletedCount = users.sweep(u => u.bot);Cache<K, V>
Section titled “Cache<K, V>”import { Cache } from "@lunibee/collection";
const cache = new Cache<string, UserData>({ maxSize: 5000, ttl: 15 * 60 * 1000, sweepInterval: 60_000,});
cache.set("1001", userData);const user = cache.get("1001");