cafe-utility 10.16.0 → 10.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +5 -0
- package/index.js +18 -0
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -22,6 +22,10 @@ declare function chance(threshold: number, generator?: () => number): boolean;
|
|
|
22
22
|
declare function pick<T>(array: T[], generator?: () => number): T;
|
|
23
23
|
declare function pickMany<T>(array: T[], count: number, generator?: () => number): T[];
|
|
24
24
|
declare function pickManyUnique<T>(array: T[], count: number, equalityFunction: (a: T, b: T) => boolean, generator?: () => number): T[];
|
|
25
|
+
declare function pickGuaranteed<T>(array: T[], include: T | null, exclude: T | null, count: number, predicate: (value: T, values: T[]) => boolean, generator?: () => number): {
|
|
26
|
+
values: T[];
|
|
27
|
+
indexOfGuaranteed: number;
|
|
28
|
+
};
|
|
25
29
|
declare function last<T>(array: T[]): T;
|
|
26
30
|
declare function pickWeighted<T>(array: T[], weights: number[], randomNumber?: number): T;
|
|
27
31
|
declare function sortWeighted<T>(array: T[], weights: number[], generator?: () => number): T[];
|
|
@@ -387,6 +391,7 @@ export declare const Arrays: {
|
|
|
387
391
|
pickManyUnique: typeof pickManyUnique;
|
|
388
392
|
pickWeighted: typeof pickWeighted;
|
|
389
393
|
pickRandomIndices: typeof pickRandomIndices;
|
|
394
|
+
pickGuaranteed: typeof pickGuaranteed;
|
|
390
395
|
last: typeof last;
|
|
391
396
|
sortWeighted: typeof sortWeighted;
|
|
392
397
|
pushAll: typeof pushAll;
|
package/index.js
CHANGED
|
@@ -169,6 +169,23 @@ function pickManyUnique(array, count, equalityFunction, generator = Math.random)
|
|
|
169
169
|
return results
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
+
function pickGuaranteed(array, include, exclude, count, predicate, generator = Math.random) {
|
|
173
|
+
const choices = array.filter(x => x !== include && x !== exclude)
|
|
174
|
+
const picks = []
|
|
175
|
+
if (include !== null) {
|
|
176
|
+
picks.push(include)
|
|
177
|
+
}
|
|
178
|
+
while (choices.length && picks.length < count) {
|
|
179
|
+
const index = exports.Random.intBetween(0, choices.length - 1, generator)
|
|
180
|
+
if (predicate(choices[index], picks)) {
|
|
181
|
+
picks.push(choices[index])
|
|
182
|
+
}
|
|
183
|
+
choices.splice(index, 1)
|
|
184
|
+
}
|
|
185
|
+
shuffle(picks, generator)
|
|
186
|
+
return { values: picks, indexOfGuaranteed: include !== null ? picks.indexOf(include) : -1 }
|
|
187
|
+
}
|
|
188
|
+
|
|
172
189
|
function last(array) {
|
|
173
190
|
return array[array.length - 1]
|
|
174
191
|
}
|
|
@@ -2631,6 +2648,7 @@ exports.Arrays = {
|
|
|
2631
2648
|
pickManyUnique,
|
|
2632
2649
|
pickWeighted,
|
|
2633
2650
|
pickRandomIndices,
|
|
2651
|
+
pickGuaranteed,
|
|
2634
2652
|
last,
|
|
2635
2653
|
sortWeighted,
|
|
2636
2654
|
pushAll,
|