Skip to content

Collection

Collection<K, V> extends JavaScript’s built-in Map with higher-order functional methods for querying and transforming data. All resource caches in Lunibee use Collection.

import { Collection } from "lunibee";
// or from the sub-package:
import { Collection } from "@lunibee/collection";
const col = new Collection<string, User>();
// From entries:
const col = new Collection([["id1", user1], ["id2", user2]]);

All standard Map methods are available: get, set, delete, has, clear, size, forEach, keys, values, entries.

Returns the first value matching the predicate, or undefined.

const admin = client.users.cache.find(u => u.username === "admin");

Returns the first key matching the predicate, or undefined.

const adminId = client.users.cache.findKey(u => u.username === "admin");

Returns a new Collection containing only entries where the predicate returns true.

const bots = client.users.cache.filter(u => u.bot);

Returns an array of the results of calling fn on every value.

const usernames = client.users.cache.map(u => u.username);
// → ["alice", "bob", "lunibee_bot", ...]

Returns a flat array from a function that returns arrays.

Reduces the collection to a single value.

const totalBoosters = client.guilds.cache.reduce(
(acc, guild) => acc + guild.premiumSubscriptionCount,
0,
);

Returns true if any entry matches the predicate.

const hasBot = client.users.cache.some(u => u.bot);

Returns true if all entries match the predicate.

Returns the first value, or the first n values as an array.

const firstUser = client.users.cache.first();
const firstThree = client.users.cache.first(3);

Returns the last value, or the last n values as an array.

Returns the first or last key(s).

Returns the value at a numeric index (supports negative for reverse).

const lastUser = client.users.cache.at(-1);

Returns the key at a numeric index.

Returns a random value, or n random values as an array.

Returns a random key.

Removes all entries where the predicate returns true. Returns the number removed.

const removed = collection.sweep(u => u.bot);

Splits the collection into two: [matching, notMatching].

const [bots, humans] = client.users.cache.partition(u => u.bot);

Returns a new Collection sorted by the comparator.

const byName = client.guilds.cache.sorted((a, b) => a.name.localeCompare(b.name));

Sorts the collection in place.

Returns a new Collection merging this with others.

Returns entries that are in this collection but not in other.

Returns entries that are in both collections.

Returns a new Collection with transformed values.

Groups values by the return value of fn.

Returns an array of values.

Filter and display online guild members

const onlineMembers = client.guilds.cache
.filter(g => g.isCommunity)
.map(g => g.name);
console.log("Community guilds:", onlineMembers.join(", "));

Get the 5 most recently joined users

const newest = client.users.cache
.sorted((a, b) => b.createdAt.getTime() - a.createdAt.getTime())
.first(5);