2022-07-08 19:17:56 +01:00
|
|
|
import { services, quality as mq } from "../config.js";
|
|
|
|
|
|
|
|
function closest(goal, array) {
|
2022-08-16 08:14:19 +01:00
|
|
|
return array.sort().reduce(function (prev, curr) {
|
2022-07-08 19:17:56 +01:00
|
|
|
return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function(service, quality, maxQuality) {
|
2022-08-16 08:14:19 +01:00
|
|
|
if (quality == "max") return maxQuality;
|
2022-07-08 19:17:56 +01:00
|
|
|
|
|
|
|
quality = parseInt(mq[quality])
|
|
|
|
maxQuality = parseInt(maxQuality)
|
|
|
|
|
2022-08-16 08:14:19 +01:00
|
|
|
if (quality >= maxQuality || quality == maxQuality) return maxQuality;
|
|
|
|
|
2022-07-08 19:17:56 +01:00
|
|
|
if (quality < maxQuality) {
|
|
|
|
if (services[service]["quality"][quality]) {
|
|
|
|
return quality
|
|
|
|
} else {
|
|
|
|
let s = Object.keys(services[service]["quality_match"]).filter((q) => {
|
|
|
|
if (q <= quality) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return closest(quality, s)
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 16:48:37 +01:00
|
|
|
}
|