react95-native
Version:
Refreshed Windows 95 style UI components for your React Native app
48 lines (42 loc) • 1.3 kB
JavaScript
// TODO: write tests for utils
export function clamp(value, min, max) {
if (max !== null && value > max) {
return max;
}
if (min !== null && value < min) {
return min;
}
return value;
} // helper functions below are from Material UI (https://github.com/mui-org/material-ui)
export function getDecimalPrecision(num) {
if (Math.abs(num) < 1) {
const parts = num.toExponential().split('e-');
const matissaDecimalPart = parts[0].split('.')[1];
return (matissaDecimalPart ? matissaDecimalPart.length : 0) + parseInt(parts[1], 10);
}
const decimalPart = num.toString().split('.')[1];
return decimalPart ? decimalPart.length : 0;
}
export function roundValueToStep(value, step, min) {
const nearest = Math.round((value - min) / step) * step + min;
return Number(nearest.toFixed(getDecimalPrecision(step)));
}
export function findClosest(values, currentValue) {
const {
index: closestIndex
} = values.reduce((acc, value, index) => {
const distance = Math.abs(currentValue - value);
if (acc === null || distance < acc.distance || distance === acc.distance) {
return {
distance,
index
};
}
return acc;
}, {
index: -1,
distance: Infinity
});
return closestIndex;
}
//# sourceMappingURL=index.js.map