Skip to content

Commit

Permalink
feat(Lensflare): add defaultChoice
Browse files Browse the repository at this point in the history
  • Loading branch information
andretchen0 committed Sep 2, 2023
1 parent b8ef198 commit 4246245
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/utils/RandUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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
Expand Down

0 comments on commit 4246245

Please sign in to comment.