@monterosa/sdk-interact-kit 0.18.0 → 0.18.1
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/dist/index.cjs.js
CHANGED
|
@@ -2179,57 +2179,6 @@ var ElementImpl = /** @class */ (function (_super) {
|
|
|
2179
2179
|
return ElementImpl;
|
|
2180
2180
|
}(sdkUtil.Emitter));
|
|
2181
2181
|
|
|
2182
|
-
/**
|
|
2183
|
-
* @license
|
|
2184
|
-
* calculate_percentage.ts
|
|
2185
|
-
* interact-kit
|
|
2186
|
-
*
|
|
2187
|
-
* Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-04-07
|
|
2188
|
-
* Copyright © 2022 Monterosa. All rights reserved.
|
|
2189
|
-
*
|
|
2190
|
-
* More details on the license can be found at https://www.monterosa.co/sdk/license
|
|
2191
|
-
*/
|
|
2192
|
-
var calculatePercentage = function (values) {
|
|
2193
|
-
// Calculate sum of all votes
|
|
2194
|
-
var sum = values.reduce(function (memo, value) { return memo + value; }, 0);
|
|
2195
|
-
// create array of hashes
|
|
2196
|
-
var results = values.map(function (value, idx) { return ({
|
|
2197
|
-
idx: idx,
|
|
2198
|
-
votes: value,
|
|
2199
|
-
percentage: Math.round((100 * value) / sum) || 0,
|
|
2200
|
-
}); });
|
|
2201
|
-
// Sum them all up - this can be less, equal or greater than a 100%
|
|
2202
|
-
var total = results.reduce(function (memo, _a) {
|
|
2203
|
-
var percentage = _a.percentage;
|
|
2204
|
-
return memo + percentage;
|
|
2205
|
-
}, 0);
|
|
2206
|
-
total = total || 100;
|
|
2207
|
-
// Calculate number of percent that we are missing or that we need to lose
|
|
2208
|
-
var delta = 100 - total;
|
|
2209
|
-
var sign = delta >= 0 ? 1 : -1;
|
|
2210
|
-
// Order all options by number of votes
|
|
2211
|
-
results.sort(function (a, b) {
|
|
2212
|
-
var result = sign * (b.votes - a.votes);
|
|
2213
|
-
if (result !== 0) {
|
|
2214
|
-
return result;
|
|
2215
|
-
}
|
|
2216
|
-
return sign * (a.idx - b.idx);
|
|
2217
|
-
});
|
|
2218
|
-
// If we have percents to lose then take one percent off lowest options
|
|
2219
|
-
// If we are missing percents then add one percent to highest options
|
|
2220
|
-
var i = 0;
|
|
2221
|
-
while (delta !== 0) {
|
|
2222
|
-
if (results[i].votes > 0) {
|
|
2223
|
-
results[i].percentage += sign;
|
|
2224
|
-
delta -= sign;
|
|
2225
|
-
}
|
|
2226
|
-
i++;
|
|
2227
|
-
}
|
|
2228
|
-
// Restore options order
|
|
2229
|
-
results.sort(function (a, b) { return a.idx - b.idx; });
|
|
2230
|
-
return results.map(function (item) { return item.percentage; });
|
|
2231
|
-
};
|
|
2232
|
-
|
|
2233
2182
|
/**
|
|
2234
2183
|
* @license
|
|
2235
2184
|
* factory.ts
|
|
@@ -2388,7 +2337,7 @@ function handleFeedbackMessage(message) {
|
|
|
2388
2337
|
return [2 /*return*/];
|
|
2389
2338
|
}
|
|
2390
2339
|
votes = counters.split(':').map(function (value) { return +value; });
|
|
2391
|
-
percentages = calculatePercentage(votes);
|
|
2340
|
+
percentages = sdkUtil.calculatePercentage(votes);
|
|
2392
2341
|
results = votes.map(function (item, idx) { return ({
|
|
2393
2342
|
percentage: percentages[idx],
|
|
2394
2343
|
votes: item,
|