Cater for negative values in number formats (#415)

* Cater for negative values in number formats

* Handle edge-case where -text is provided
This commit is contained in:
Jaco Greeff
2019-06-04 09:43:11 +02:00
committed by GitHub
parent a1724c7e30
commit bb8877894b
16 changed files with 107 additions and 31 deletions
+7 -2
View File
@@ -5,9 +5,14 @@
const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
export default function formatDecimal (value: string): string {
const matched = value.match(NUMBER_REGEX);
// We can do this by adjusting the regx, however for the sake of clarity
// we rather strip and re-add the negative sign in the output
const isNegative = value[0] === '-';
const matched = isNegative
? value.substr(1).match(NUMBER_REGEX)
: value.match(NUMBER_REGEX);
return matched
? matched.join(',')
? `${isNegative ? '-' : ''}${matched.join(',')}`
: value;
}