From 4246245c1b06421b51c341a2ef4ca71ffe689c99 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 1 Sep 2023 14:34:44 +0200 Subject: [PATCH] feat(Lensflare): add defaultChoice --- src/utils/RandUtils.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/utils/RandUtils.ts b/src/utils/RandUtils.ts index 730c85bd..805914a6 100644 --- a/src/utils/RandUtils.ts +++ b/src/utils/RandUtils.ts @@ -71,6 +71,19 @@ export default class RandUtils { return array[Math.floor(this._getNext() * array.length)] } + /** + * Choose an element from an array or return defaultValue if array is empty. + * @param array The array to choose from + * @param defaultValue The value to return if the array is empty + * @returns An element from the array or defaultValue if the array is empty + */ + defaultChoice(array: T[], defaultValue: T): T { + if (!array.length) { + return defaultValue + } + return array[Math.floor(this._getNext() * array.length)] + } + /** * Return n elements from an array. * @param array The array to sample