@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
37 lines (36 loc) • 1.19 kB
JavaScript
/*! All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://github.com/Esri/calcite-design-system/blob/dev/LICENSE.md for details.
v3.2.1 */
const clamp = (value, min, max) => Math.max(min, Math.min(value, max));
const decimalNumberRegex = new RegExp(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
const decimalPlaces = (value) => {
const match = ("" + value).match(decimalNumberRegex);
if (!match || parseInt(match[1]) === 0) {
return 0;
}
return Math.max(
0,
// Number of digits right of decimal point.
(match[1] ? match[1].length : 0) - // Adjust for scientific notation.
(match[2] ? +match[2] : 0)
);
};
function getDecimals(value) {
if (decimalPlaces(value) > 0 && value > 0) {
return parseFloat(`0.${value.toString().split(".")[1]}`);
}
return value;
}
function remap(value, fromMin, fromMax, toMin, toMax) {
return (value - fromMin) * (toMax - toMin) / (fromMax - fromMin) + toMin;
}
function closeToRangeEdge(value, range, threshold) {
return value < threshold ? -1 : value > range - threshold ? 1 : 0;
}
export {
closeToRangeEdge as a,
clamp as c,
decimalPlaces as d,
getDecimals as g,
remap as r
};