Skip to content

@lunibee/collection

The @lunibee/collection package provides high-performance in-memory data structures engineered specifically for Bun and Discord bots.

Terminal window
bun add @lunibee/collection

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 });
  • Parameters: amount?: number
  • Returns: V | V[] | undefined
const firstUser = users.first();
const firstTwo = users.first(2);
  • Returns: K | K[] | undefined
const firstId = users.firstKey();
  • Parameters: predicate: (value: V, key: K, collection: this) => boolean
  • Returns: V | undefined
const botUser = users.find(u => u.bot === true);
  • Parameters: predicate: (value: V, key: K, collection: this) => boolean
  • Returns: Collection<K, V>
const humans = users.filter(u => !u.bot);
  • Parameters: transform: (value: V, key: K, collection: this) => T
  • Returns: T[]
const usernames = users.map(u => u.username);
  • Returns: number (count of removed entries)
const deletedCount = users.sweep(u => u.bot);

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");