import removeInvalidChars from './removeInvalidChars'; import removeSeparators from './removeSeparators'; const cleanNumericValue = ({ value, allowDecimal = false }: { value: string; allowDecimal?: boolean }) => { const groupSeparator = ','; const decimalSeparator = '.'; const decimalsLimit = 2; const withoutSeparators = removeSeparators(value, groupSeparator); const withoutInvalidChars = removeInvalidChars(withoutSeparators); const valueOnly = withoutInvalidChars; // This is the part that takes care of trimming the decimals if (valueOnly.includes(decimalSeparator)) { const [int, decimals] = withoutInvalidChars.split(decimalSeparator); const trimmedDecimals = decimalsLimit && decimals ? decimals.slice(0, decimalsLimit) : decimals; const includeDecimals = allowDecimal ? `${decimalSeparator}${trimmedDecimals}` : ''; return `${int}${includeDecimals}`; } return valueOnly; }; export default cleanNumericValue;