@renderlesskit/react
Version:
Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit
63 lines (51 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.calculateStatus = void 0;
exports.clamp = clamp;
exports.isInRange = void 0;
var _utils = require("../utils");
/**
* Handle Inequalities with received values
*
* minimum ≤ value ≤ maximum
* minimum ≤ low ≤ maximum (if low is specified)
* minimum ≤ high ≤ maximum (if high is specified)
* minimum ≤ optimum ≤ maximum (if optimum is specified)
*
* @see https://html.spec.whatwg.org/multipage/form-elements.html#the-meter-element:attr-meter-max-3:~:text=following%20inequalities%20must%20hold
*/
function clamp(value, min, max) {
if ((0, _utils.isNull)(value)) return 0;
return Math.min(Math.max(value, min), max);
}
var calculateStatus = function calculateStatus(props) {
var value = props.value,
optimum = props.optimum,
min = props.min,
max = props.max,
low = props.low,
high = props.high; // This check always comes first
if (isInRange(optimum, low, high)) {
if (isInRange(value, low, high)) return "safe";
return "caution";
}
if (isInRange(optimum, min, low)) {
if (isInRange(value, min, low)) return "safe";
if (value > low && value <= high) return "caution";
return "danger";
}
if (isInRange(optimum, high, max)) {
if (isInRange(value, high, max)) return "safe";
if (value < high && value >= low) return "caution";
return "danger";
} // A safe return
return "safe";
};
exports.calculateStatus = calculateStatus;
var isInRange = function isInRange(value, min, max) {
return value >= min && value <= max;
};
exports.isInRange = isInRange;
//# sourceMappingURL=helpers.js.map