Skip to content

Commit 4246245

Browse files
committedSep 2, 2023
feat(Lensflare): add defaultChoice
1 parent b8ef198 commit 4246245

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
 

‎src/utils/RandUtils.ts

+13
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ export default class RandUtils {
7171
return array[Math.floor(this._getNext() * array.length)]
7272
}
7373

74+
/**
75+
* Choose an element from an array or return defaultValue if array is empty.
76+
* @param array The array to choose from
77+
* @param defaultValue The value to return if the array is empty
78+
* @returns An element from the array or defaultValue if the array is empty
79+
*/
80+
defaultChoice<T>(array: T[], defaultValue: T): T {
81+
if (!array.length) {
82+
return defaultValue
83+
}
84+
return array[Math.floor(this._getNext() * array.length)]
85+
}
86+
7487
/**
7588
* Return n elements from an array.
7689
* @param array The array to sample

0 commit comments

Comments
 (0)