UNPKG

@twitchfy/chatbot

Version:

A powerful node module to make your own Twitch ChatBot

110 lines (109 loc) 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Collection = void 0; /** * A collection of key-value pairs. * @extends Map */ class Collection extends Map { /** * Creates a new collection. * @param entries The entries to set in the collection. */ constructor(entries) { super(entries); } /** * Finds the first value that satisfies the condition. * @param fn The function to execute. * @returns Returns the value if found, otherwise undefined. */ find(fn) { for (const [key, value] of this) { if (fn(value, key, this)) { return value; } } return undefined; } /** * Filters the collection by a condition. * @param fn The function to execute. * @returns The filtered collection. */ filter(fn) { const results = new Collection(); for (const [key, value] of this) { if (fn(value, key, this)) { results.set(key, value); } } return results; } /** * Maps the collection. * @param fn The function to execute. * @returns The mapped collection. */ map(fn) { const results = new Collection(); for (const [key, value] of this) { results.set(key, fn(value, key, this)); } return results; } /** * Checks if any value satisfies the condition. * @param fn The function to execute. * @returns Whether any value satisfies the condition. */ some(fn) { for (const [key, value] of this) { if (fn(value, key, this)) { return true; } } return false; } /** * Checks if every value satisfies the condition. * @param fn The function to execute. * @returns Whether every value satisfies the condition. */ every(fn) { for (const [key, value] of this) { if (!fn(value, key, this)) { return false; } } return true; } /** * Reduces the collection to a single value. * @param fn The function to execute. * @param initial The initial value. * @returns The reduced value. */ reduce(fn, initial) { let accumulator = initial; for (const [key, value] of this) { accumulator = fn(accumulator, value, key, this); } return accumulator; } /** * Returns the first value of the collection. * @returns The first value of the collection. */ first() { return this.values().next().value; } /** * Transform the collection into an array containing the values of it. * @returns The array containing the values of the collection. */ toArray() { return [...this.values()]; } } exports.Collection = Collection;