Last active
March 10, 2020 01:26
-
-
Save TonyDotDev/28b3b7a894fa45980dc813a90b2e8d51 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
export const maskPrice = price => { | |
const scrubValue = price => { | |
const removedDecimal = value.replace(/\./g, ""); | |
const onlyDigits = new RegExp(/^(\d)+$/); | |
if (!onlyDigits.test(removedDecimal)) return; | |
const removedDecimalArray = removedDecimal.split(""); | |
removedDecimalArray.splice(-2, 0, "."); | |
return removedDecimalArray.join(""); | |
}; | |
const scrubbedValue = scrubValue(price); | |
const formatValue = value => price.toFixed(2); | |
return formatValue(scrubbedValue); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment