From c1a016ca9fbc537c516577767edd0c80202755f5 Mon Sep 17 00:00:00 2001 From: ayntee Date: Tue, 23 Mar 2021 14:31:29 +0400 Subject: [PATCH] fix(util/collection): .first(), .last(), .random() may return undefined (#680) * fix(util/collection): .first() may return undefined * fix(util/collection): .random(), .last() may return undefined --- src/util/collection.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/util/collection.ts b/src/util/collection.ts index 01489432e..78140bf0f 100644 --- a/src/util/collection.ts +++ b/src/util/collection.ts @@ -16,16 +16,17 @@ export class Collection extends Map { return [...this.values()]; } - first(): V { + /** Retrieve the value of the first element in this collection */ + first(): V | undefined { return this.values().next().value; } - last(): V { + last(): V | undefined { return [...this.values()][this.size - 1]; } - random() { - return chooseRandom([...this.values()]); + random(): V | undefined { + return chooseRandom([...this.values()]); } find(callback: (value: V, key: K) => boolean) {