Repository: discordjs/discord.js
File: packages/collection/src/collection.ts (TypeScript)

Code before:

		const iter = this.values();
		for (let skip = 0; skip < index; skip++) {
			iter.next();
		}

		return iter.next().value!;
	}

	/**
	 * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
	 * Returns the key at a given index, allowing for positive and negative integers.
	 * Negative integers count back from the last item in the collection.
	 *
	 * @param index - The index of the key to obtain
	 */
	public keyAt(index: number): Key | undefined {
		index = Math.trunc(index);
		if (index >= 0) {
			if (index >= this.size) return undefined;
		} else {
			if (index < this.size * -1) return undefined;
			index += this.size;
		}

		const iter = this.keys();
		for (let skip = 0; skip < index; skip++) {
			iter.next();
		}

		return iter.next().value!;
	}

	/**
	 * Obtains unique random value(s) from this collection.
	 *
	 * @param amount - Amount of values to obtain randomly
	 * @returns A single value if no amount is provided or an array of values
	 */
	public random(): Value | undefined;
	public random(amount: number): Value[];
	public random(amount?: number): Value | Value[] | undefined {
		if (amount === undefined) return this.at(Math.floor(Math.random() * this.size));
		amount = Math.min(this.size, amount);
		if (!amount) return [];

		const values = [...this.values()];
		// eslint-disable-next-line unicorn/no-new-array
		const results: Value[] = new Array(amount);
		for (let index = 0; index < amount; index++) {
			results[index] = values.splice(Math.floor(Math.random() * values.length), 1)[0]!;

Review comment (suggestion):
Ditto
```suggestion
		if (index < 0) {
			index += this.size;
			if (index < 0) return undefined;
		} else if (index >= this.size) {
			return undefined;
		}
```

Code after:
			if (index < 0) return undefined;
		}

		const iter = this.values();
		for (let skip = 0; skip < index; skip++) {
			iter.next();
		}

		return iter.next().value!;
	}

	/**
	 * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
	 * Returns the key at a given index, allowing for positive and negative integers.
	 * Negative integers count back from the last item in the collection.
	 *
	 * @param index - The index of the key to obtain
	 */
	public keyAt(index: number): Key | undefined {
		index = Math.trunc(index);
		if (index >= 0) {
			if (index >= this.size) return undefined;
		} else {
			index += this.size;
			if (index < 0) return undefined;
		}

		const iter = this.keys();
		for (let skip = 0; skip < index; skip++) {
			iter.next();
		}

		return iter.next().value!;
	}

	/**
	 * Obtains unique random value(s) from this collection.
	 *
	 * @param amount - Amount of values to obtain randomly
	 * @returns A single value if no amount is provided or an array of values
	 */
	public random(): Value | undefined;
	public random(amount: number): Value[];
	public random(amount?: number): Value | Value[] | undefined {
		if (amount === undefined) return this.at(Math.floor(Math.random() * this.size));
		amount = Math.min(this.size, amount);
		if (!amount) return [];

		const values = [...this.values()];
		for (let sourceIndex = 0; sourceIndex < amount; sourceIndex++) {
			const targetIndex = sourceIndex + Math.floor(Math.random() * (values.length - sourceIndex));