-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweights.ts
40 lines (36 loc) · 945 Bytes
/
weights.ts
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
export type Weights = {
type: "none" | "single" | "multi" | "weighted";
weights: Map<string, number>;
canCopyAudio: boolean;
};
export function parseWeightsArray(weightsArray: number[] | undefined): Weights {
if (!weightsArray) {
weightsArray = [1];
}
const nonzeroWeights = new Map(Object.entries(weightsArray).filter(([, weight]) => weight !== 0));
if (nonzeroWeights.size === 0) {
return { type: "none", weights: new Map(), canCopyAudio: false };
}
if (nonzeroWeights.size === 1) {
return {
type: "single",
weights: nonzeroWeights,
canCopyAudio: true,
};
}
const firstWeight = nonzeroWeights.values().next().value;
const allSame = [...nonzeroWeights.values()].every((weight) => weight === firstWeight);
if (allSame) {
return {
type: "multi",
weights: nonzeroWeights,
canCopyAudio: false,
};
} else {
return {
type: "weighted",
weights: nonzeroWeights,
canCopyAudio: false,
};
}
}