89 lines
3.2 KiB
JavaScript
89 lines
3.2 KiB
JavaScript
//source: https://github.com/super-ienien/percent-round
|
|
/*Copyright 2020 Vivien Anglesio
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
export default function percentRound(ipt, precision) {
|
|
if (!precision) {
|
|
precision = 0;
|
|
}
|
|
if (!Array.isArray(ipt)) {
|
|
throw new Error('percentRound input should be an Array');
|
|
}
|
|
const iptPercents = ipt.slice();
|
|
const length = ipt.length;
|
|
const out = new Array(length);
|
|
|
|
let total = 0;
|
|
for (let i = length - 1; i >= 0; i--) {
|
|
if (typeof iptPercents[i] === "string") {
|
|
iptPercents[i] = Number.parseFloat(iptPercents[i]);
|
|
}
|
|
total += iptPercents[i] * 1;
|
|
}
|
|
if (isNaN(total)) {
|
|
throw new Error('percentRound invalid input');
|
|
}
|
|
|
|
if (total === 0) {
|
|
out.fill(0);
|
|
} else {
|
|
const powPrecision = Math.pow(10, precision);
|
|
const pow100 = 100 * powPrecision;
|
|
let check100 = 0;
|
|
for (let i = length - 1; i >= 0; i--) {
|
|
iptPercents[i] = 100 * iptPercents[i] / total;//hpf: insert bankersRound here? no, I think it's not necessary in this case!
|
|
check100 += out[i] = Math.round(iptPercents[i] * powPrecision); //or here? Or does this subsume the need for bankers round?
|
|
}
|
|
|
|
if (check100 !== pow100) {
|
|
const totalDiff = (check100 - pow100) ;
|
|
const roundGrain = 1;
|
|
let grainCount = Math.abs(totalDiff);
|
|
const diffs = new Array(length);
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
diffs[i] = Math.abs(out[i] - iptPercents[i] * powPrecision);
|
|
}
|
|
|
|
while (grainCount > 0) {
|
|
let idx = 0;
|
|
let maxDiff = diffs[0];
|
|
for (let i = 1; i < length; i++) {
|
|
if (maxDiff < diffs[i]) {
|
|
// avoid negative result
|
|
if (check100 > pow100 && out[i] - roundGrain < 0) {
|
|
continue;
|
|
}
|
|
idx = i;
|
|
maxDiff = diffs[i];
|
|
}
|
|
}
|
|
if (check100 > pow100) {
|
|
out[idx] -= roundGrain;
|
|
} else {
|
|
out[idx] += roundGrain;
|
|
}
|
|
diffs[idx] -= roundGrain;
|
|
grainCount--;
|
|
}
|
|
}
|
|
|
|
if (powPrecision > 1) {
|
|
for (let i = 0; i < length; i++) {
|
|
out[i] = out[i] / powPrecision;
|
|
}
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
// For es import compatibility
|
|
percentRound.default = percentRound;
|