Created
September 21, 2023 15:11
-
-
Save YonatanKra/9d02ea35e7c5212ca0af5431fdee9922 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const numberInput = document.createElement('input'); | |
numberInput.type = 'number'; | |
export function getValidValue(value: string) { | |
if (!this.isUserInput) { | |
numberInput.value = value; | |
return numberInput.value; | |
} | |
if (value === '' || value === '-' || value === '.') { | |
return value; | |
} | |
const decimalSplit = value.split('.'); | |
let valueSuffix = ''; | |
if (decimalSplit.length === 2 && decimalSplit[1] === '') { | |
valueSuffix = '.'; | |
numberInput.value = value.slice(0, -1); | |
} else { | |
numberInput.value = value; | |
} | |
if (numberInput.value === '') { | |
return this.currentValue; | |
} | |
return numberInput.value + valueSuffix; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getValidValue(value: string) { | |
function isValueInvalidValid(value: string): boolean { | |
return decimalSplit.length > 3 || !!value.match(/\.\.+/gm) || isNaN(parseFloat(validValue)); | |
} | |
function removePossibleTrailingExtraDecimal(value: string): string { | |
if (decimalSplit.length === 3 && decimalSplit[2] === '') { | |
value = decimalSplit[0] + '.' + decimalSplit[1]; | |
} | |
return value; | |
} | |
function addLeadingOrTrailingDecimal(value: string): string { | |
if (decimalSplit.length === 2) { | |
if (decimalSplit[1] === '') { | |
value += '.'; | |
} else if (decimalSplit[0] === '') { | |
value = '.' + decimalSplit[1]; | |
} | |
} | |
return value; | |
} | |
function setInMinMaxBoundaries(value: string, min: number, max: number) { | |
const numericValue: number = parseFloat(parseFloat(value).toPrecision(12)); | |
const minValue: number = min !== undefined ? min : numericValue; | |
const maxValue: number = max !== undefined ? max : numericValue; | |
if (numericValue < minValue) { | |
value = minValue.toString(); | |
} else if (numericValue > maxValue) { | |
value = maxValue.toString(); | |
} else { | |
value = numericValue.toString(); | |
} | |
return value; | |
} | |
if (value === '-') { | |
return '-'; | |
} | |
const decimalSplit = value.split('.'); | |
let validValue: string = value; | |
if (isValueInvalidValid(validValue)) { | |
return ''; | |
} | |
validValue = removePossibleTrailingExtraDecimal(validValue); | |
validValue = setInMinMaxBoundaries(validValue, this.min, this.max); | |
validValue = addLeadingOrTrailingDecimal(validValue); | |
return validValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment