npm-pd
Version:
A beautiful CLI dashboard for NPM publishers
34 lines (25 loc) • 829 B
JavaScript
exports.__esModule = true;
exports.normalizeValue = normalizeValue;
var _lodash = require("lodash");
var POSITIVE_INFINITY = Number.POSITIVE_INFINITY,
isNaN = Number.isNaN;
var abs = Math.abs;
function normalizeValue(value, steps) {
var index = (0, _lodash.findLastIndex)(steps, function (step) {
return step.value <= value;
});
if (index === -1) {
return steps[0].norm;
}
if (index >= steps.length - 1) {
return steps[steps.length - 1].norm;
}
var stepLow = steps[index];
var stepHigh = steps[index + 1];
var normalized = stepLow.norm + (stepHigh.norm - stepLow.norm) * (value - stepLow.value) / (stepHigh.value - stepLow.value);
if (isNaN(normalized) || abs(normalized) === POSITIVE_INFINITY) {
throw new Error('Invalid value or steps');
}
return normalized;
}
;