Skip to content

Instantly share code, notes, and snippets.

@MMortari
Last active November 19, 2019 16:25
Show Gist options
  • Save MMortari/6945d79975967f641bc3fd61b44c8660 to your computer and use it in GitHub Desktop.
Save MMortari/6945d79975967f641bc3fd61b44c8660 to your computer and use it in GitHub Desktop.
const DinheiroMask = ({ prefix = "R$", decimal = 2, chunkDelimiter = '.', decimalDelimiter = ',', children }) => {
let total = "";
let value = children;
if (!value) {
value = 0;
}
const result = '\\d(?=(\\d{3})+' + (decimal > 0 ? '\\D' : '$') + ')';
const num = value.toFixed(Math.max(0, ~~decimal));
total = prefix + ' ' + (decimalDelimiter ? num.replace('.', decimalDelimiter) : num).replace(new RegExp(result, 'g'), '$&' + chunkDelimiter);
return total;
}
export default DinheiroMask;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment