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
Section titled “Import”import { Collection } from "lunibee";// or from the sub-package:import { Collection } from "@lunibee/collection";import { Collection } from "lunibee";// or from the sub-package:import { Collection } from "@lunibee/collection";Constructor
Section titled “Constructor”const col = new Collection<string, User>();// From entries:const col = new Collection([["id1", user1], ["id2", user2]]);const col = new Collection<string, User>();// From entries:const col = new Collection([["id1", user1], ["id2", user2]]);Inherited Map Methods
Section titled “Inherited Map Methods”All standard Map methods are available: get, set, delete, has, clear, size, forEach, keys, values, entries.
Additional Methods
Section titled “Additional Methods”find(predicate)
Section titled “find(predicate)”Returns the first value matching the predicate, or undefined.
const admin = client.users.cache.find(u => u.username === "admin");const admin = client.users.cache.find(u => u.username === "admin");findKey(predicate)
Section titled “findKey(predicate)”Returns the first key matching the predicate, or undefined.
const adminId = client.users.cache.findKey(u => u.username === "admin");const adminId = client.users.cache.findKey(u => u.username === "admin");filter(predicate)
Section titled “filter(predicate)”Returns a new Collection containing only entries where the predicate returns true.
const bots = client.users.cache.filter(u => u.bot);const bots = client.users.cache.filter(u => u.bot);map(fn)
Section titled “map(fn)”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", ...]const usernames = client.users.cache.map(u => u.username);// → ["alice", "bob", "lunibee_bot", ...]flatMap(fn)
Section titled “flatMap(fn)”Returns a flat array from a function that returns arrays.
reduce(fn, initialValue)
Section titled “reduce(fn, initialValue)”Reduces the collection to a single value.
const totalBoosters = client.guilds.cache.reduce( (acc, guild) => acc + guild.premiumSubscriptionCount, 0,);const totalBoosters = client.guilds.cache.reduce( (acc, guild) => acc + guild.premiumSubscriptionCount, 0,);some(predicate)
Section titled “some(predicate)”Returns true if any entry matches the predicate.
const hasBot = client.users.cache.some(u => u.bot);const hasBot = client.users.cache.some(u => u.bot);every(predicate)
Section titled “every(predicate)”Returns true if all entries match the predicate.
first(amount?)
Section titled “first(amount?)”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);const firstUser = client.users.cache.first();const firstThree = client.users.cache.first(3);last(amount?)
Section titled “last(amount?)”Returns the last value, or the last n values as an array.
firstKey(amount?) / lastKey(amount?)
Section titled “firstKey(amount?) / lastKey(amount?)”Returns the first or last key(s).
at(index)
Section titled “at(index)”Returns the value at a numeric index (supports negative for reverse).
const lastUser = client.users.cache.at(-1);const lastUser = client.users.cache.at(-1);keyAt(index)
Section titled “keyAt(index)”Returns the key at a numeric index.
random(amount?)
Section titled “random(amount?)”Returns a random value, or n random values as an array.
randomKey(amount?)
Section titled “randomKey(amount?)”Returns a random key.
sweep(predicate)
Section titled “sweep(predicate)”Removes all entries where the predicate returns true. Returns the number removed.
const removed = collection.sweep(u => u.bot);const removed = collection.sweep(u => u.bot);partition(predicate)
Section titled “partition(predicate)”Splits the collection into two: [matching, notMatching].
const [bots, humans] = client.users.cache.partition(u => u.bot);const [bots, humans] = client.users.cache.partition(u => u.bot);sorted(compareFn?)
Section titled “sorted(compareFn?)”Returns a new Collection sorted by the comparator.
const byName = client.guilds.cache.sorted((a, b) => a.name.localeCompare(b.name));const byName = client.guilds.cache.sorted((a, b) => a.name.localeCompare(b.name));sort(compareFn?)
Section titled “sort(compareFn?)”Sorts the collection in place.
concat(...collections)
Section titled “concat(...collections)”Returns a new Collection merging this with others.
difference(other)
Section titled “difference(other)”Returns entries that are in this collection but not in other.
intersect(other)
Section titled “intersect(other)”Returns entries that are in both collections.
mapValues(fn)
Section titled “mapValues(fn)”Returns a new Collection with transformed values.
groupBy(fn, collection?)
Section titled “groupBy(fn, collection?)”Groups values by the return value of fn.
toJSON()
Section titled “toJSON()”Returns an array of values.
Example
Section titled “Example”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(", "));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);const newest = client.users.cache .sorted((a, b) => b.createdAt.getTime() - a.createdAt.getTime()) .first(5);