Skip to content

Instantly share code, notes, and snippets.

@TonyDotDev
Last active March 10, 2020 01:26
Show Gist options
  • Save TonyDotDev/28b3b7a894fa45980dc813a90b2e8d51 to your computer and use it in GitHub Desktop.
Save TonyDotDev/28b3b7a894fa45980dc813a90b2e8d51 to your computer and use it in GitHub Desktop.
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