mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-16 13:19:58 +00:00
32 lines
884 B
JavaScript
32 lines
884 B
JavaScript
|
import { services, quality as mq } from "../config.js";
|
||
|
|
||
|
function closest(goal, array) {
|
||
|
return array.sort().reduce(function(prev, curr) {
|
||
|
return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export default function(service, quality, maxQuality) {
|
||
|
if (quality == "max") {
|
||
|
return maxQuality
|
||
|
}
|
||
|
|
||
|
quality = parseInt(mq[quality])
|
||
|
maxQuality = parseInt(maxQuality)
|
||
|
|
||
|
if (quality >= maxQuality || quality == maxQuality) {
|
||
|
return maxQuality
|
||
|
}
|
||
|
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)
|
||
|
}
|
||
|
}
|
||
|
}
|