We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b8ef198 commit 4246245Copy full SHA for 4246245
src/utils/RandUtils.ts
@@ -71,6 +71,19 @@ export default class RandUtils {
71
return array[Math.floor(this._getNext() * array.length)]
72
}
73
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
+
87
/**
88
* Return n elements from an array.
89
* @param array The array to sample
0 commit comments