@aikidosec/firewall
Version:
Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks
26 lines (25 loc) • 876 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.percentiles = percentiles;
function percentiles(percentiles, list) {
if (list.length === 0) {
throw new Error("List should not be empty");
}
percentiles.forEach((p) => {
if (p < 0) {
throw new Error(`Expect percentile to be >= 0 but given "${p}" and its type is "${typeof p}".`);
}
if (p > 100) {
throw new Error(`Expect percentile to be <= 100 but given "${p}" and its type is "${typeof p}".`);
}
});
const sortedList = Array.from(list).sort((a, b) => a - b);
return percentiles.map((p) => getPercentileValue(p, sortedList));
}
function getPercentileValue(p, list) {
if (p === 0) {
return list[0];
}
const kIndex = Math.ceil(list.length * (p / 100)) - 1;
return list[kIndex];
}