UNPKG

@antonkolesnik/counter

Version:

React number increase/decrease animation

64 lines (62 loc) 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDecimals = getDecimals; exports.getIntervalStepTime = getIntervalStepTime; exports.getNextIntervalValue = getNextIntervalValue; exports.isFloat = isFloat; exports.isIntervalEnd = isIntervalEnd; function isFloat(str) { return str.includes('.'); } function getIntervalStepTime(_ref) { var duration = _ref.duration, startNumber = _ref.startNumber, endNumber = _ref.endNumber, isDecrease = _ref.isDecrease, decimals = _ref.decimals; var start = Number(startNumber); var end = Number(endNumber); // Calculate how many operations we need to make before setInterval is finished var operations = isDecrease ? start - end : end - start; if (isFloat(endNumber) || isFloat(startNumber)) { // Multiply operations by number after point // 10 ** decimals - operations after point var factor = Math.pow(10, decimals); var operationsWithFloat = operations * factor; return duration / operationsWithFloat; } return Math.floor(duration / operations); } function isIntervalEnd(_ref2) { var nextIntervalValue = _ref2.nextIntervalValue, end = _ref2.end, decimals = _ref2.decimals; // For a case where start = *.0 and end = *.01 if (isFloat(nextIntervalValue) || isFloat(end)) { return nextIntervalValue === Number(end).toFixed(decimals); } return nextIntervalValue === end; } function getDecimals(_ref3) { var start = _ref3.start, end = _ref3.end; var startDecimals = isFloat(start) ? start.split('.')[1].length : 0; var endDecimals = isFloat(end) ? end.split('.')[1].length : 0; return startDecimals > endDecimals ? startDecimals : endDecimals; } function getNextIntervalValue(_ref4) { var isFloatRange = _ref4.isFloatRange, currentValue = _ref4.currentValue, decimals = _ref4.decimals, operation = _ref4.operation; if (isFloatRange) { var factor = Math.pow(10, decimals); // operation / factor = 0.1, 0.001, 0,0001... (Depends on decimals) var _result = (currentValue + operation / factor).toFixed(decimals); return _result; } var result = currentValue + operation; return String(result); }