Repository navigation
Expand file tree
/
Copy pathstack.ts
More file actions
47 lines (37 loc) · 1.3 KB
/
Copy pathstack.ts
File metadata and controls
47 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import shuffle from "./shuffle";
import { getStorage } from "./storage";
let stack: string[] = [];
let data = getStorage();
function calculateAverage(arr: number[]) {
if (arr.length === 0) return 0;
const sum = arr.reduce((acc, curr) => acc + curr, 0);
const average = sum / arr.length;
return Math.floor(average);
}
export function* stackGenerator() {
// get latest storage
data = getStorage();
// get average number of items, but always keep at least one bowl: once most
// profiles are drawn down the average rounds to 0, and `index % 0` is NaN,
// which makes `bowls[NaN]` undefined. A malformed count parses to NaN, which
// would poison the average and make Array(NaN) throw, so coerce those to 0.
const counts = data.map((x) => parseInt(x.count) || 0);
const avg = Math.max(1, calculateAverage(counts));
// create arrays of average
const bowls: string[][] = Array.from(Array(avg)).map(() => []);
let index = 0;
// shuffle profiles to randomize iteration
shuffle(data as []).forEach(({ count, uid }) => {
for (let i = 0; i < parseInt(count); i += 1) {
bowls[index++ % avg].push(uid);
}
});
stack = bowls
// shuffle arrays
.map((arr) => shuffle(arr as []))
// flatten stack
.flat();
for (let i = 0; i < stack.length; i++) {
yield stack[i];
}
}