78 lines
2.9 KiB
JavaScript
78 lines
2.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.downStep = exports.upStep = exports.getPrecisionFactor = exports.getMaxPrecision = exports.getPrecision = exports.toNumber = exports.isNotCompleteNumber = void 0;
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
// '1.' '1x' 'xx' '' => are not complete numbers
|
|
function isNotCompleteNumber(num) {
|
|
return (isNaN(num) ||
|
|
num === '' ||
|
|
num === null ||
|
|
(num && num.toString().indexOf('.') === num.toString().length - 1));
|
|
}
|
|
exports.isNotCompleteNumber = isNotCompleteNumber;
|
|
function toNumber(num, precision) {
|
|
// num.length > 16 => This is to prevent input of large numbers
|
|
var numberIsTooLarge = num && num.length > 16;
|
|
if (isNotCompleteNumber(num) || numberIsTooLarge) {
|
|
return undefined;
|
|
}
|
|
if (precision != null) {
|
|
return Math.round(+num * Math.pow(10, precision) / Math.pow(10, precision));
|
|
}
|
|
return Number(num);
|
|
}
|
|
exports.toNumber = toNumber;
|
|
function getPrecision(value, precision) {
|
|
if (precision != null) {
|
|
return precision;
|
|
}
|
|
var valueString = String(value);
|
|
if (valueString.indexOf('e-') >= 0) {
|
|
return parseInt(valueString.slice(valueString.indexOf('e-') + 2), 10);
|
|
}
|
|
var p = 0;
|
|
if (valueString.indexOf('.') >= 0) {
|
|
p = valueString.length - valueString.indexOf('.') - 1;
|
|
}
|
|
return p;
|
|
}
|
|
exports.getPrecision = getPrecision;
|
|
// step={1.0} value={1.51}
|
|
// press +
|
|
// then value should be 2.51, rather than 2.5
|
|
// if this.props.precision is undefined
|
|
// https://github.com/react-component/input-number/issues/39
|
|
function getMaxPrecision(currentValue, step, precision) {
|
|
if (precision != null) {
|
|
return precision;
|
|
}
|
|
var stepPrecision = getPrecision(step, precision);
|
|
var currentValuePrecision = getPrecision(currentValue, precision);
|
|
if (!currentValue) {
|
|
return stepPrecision;
|
|
}
|
|
return Math.max(currentValuePrecision, stepPrecision);
|
|
}
|
|
exports.getMaxPrecision = getMaxPrecision;
|
|
function getPrecisionFactor(currentValue, precision) {
|
|
var p = getMaxPrecision(currentValue, undefined, precision);
|
|
return Math.pow(10, p);
|
|
}
|
|
exports.getPrecisionFactor = getPrecisionFactor;
|
|
function upStep(val, step, precision) {
|
|
var precisionFactor = getPrecisionFactor(val, precision);
|
|
var p = Math.abs(getMaxPrecision(val, step, precision));
|
|
var result = ((precisionFactor * val + precisionFactor * step) /
|
|
precisionFactor).toFixed(p);
|
|
return toNumber(result, precision);
|
|
}
|
|
exports.upStep = upStep;
|
|
function downStep(val, step, precision) {
|
|
var precisionFactor = getPrecisionFactor(val, precision);
|
|
var p = Math.abs(getMaxPrecision(val, step, precision));
|
|
var result = ((precisionFactor * val - precisionFactor * step) /
|
|
precisionFactor).toFixed(p);
|
|
return toNumber(result);
|
|
}
|
|
exports.downStep = downStep; |