@brizy/ui
Version:
React elements in Brizy style
51 lines (50 loc) • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.preciseSub = exports.preciseAdd = void 0;
exports.formatInputValue = formatInputValue;
exports.decimalLength = decimalLength;
exports.correctNumber = correctNumber;
const math_1 = require("../utils/math");
function formatInputValue(value, step) {
const stepDecimalLength = decimalLength(step);
return stepDecimalLength > 0 ? value.toFixed(stepDecimalLength) : String(value);
}
function decimalLength(number) {
const [, decimal] = String(number).split(".");
return decimal ? decimal.length : 0;
}
function correctNumber(number, min, max, step) {
return roundByStep((0, math_1.clamp)(number, min, max), step, min);
}
exports.preciseAdd = preciseOperation.bind(null, "add");
exports.preciseSub = preciseOperation.bind(null, "sub");
function roundByStep(number, step, offset = 0) {
// this is done to avoid quirks with floating point operations
// like 1.1 - 1 = 0.10000000000000009
const mul = Math.pow(10, decimalLength(step));
number = number * mul;
step = step * mul;
offset = offset * mul;
const tmp = Math.ceil((number - offset) / step) * step + offset;
return tmp / mul;
}
function preciseOperation(type, a, b) {
const dla = decimalLength(a);
const dlb = decimalLength(b);
const dlMax = Math.max(dla, dlb);
const mul = Math.pow(10, dlMax);
const a_ = a * mul;
const b_ = b * mul;
let res;
switch (type) {
case "add":
res = a_ + b_;
break;
case "sub":
res = a_ - b_;
break;
default:
throw new Error(`operation type unknown ${type}`);
}
return res / mul;
}