fix(localize): allow negative bulgarian numbers

This commit is contained in:
qa46hx 2020-01-07 10:30:55 +01:00
parent d66a90566c
commit 367e2f4f96
2 changed files with 14 additions and 5 deletions

View file

@ -7,9 +7,17 @@
*/ */
export function forceAddGroupSeparators(formattedParts, groupSeparator) { export function forceAddGroupSeparators(formattedParts, groupSeparator) {
let concatArray = []; let concatArray = [];
if (formattedParts[0].type === 'integer') { let firstPart;
const getInteger = formattedParts.splice(0, 1); let integerPart;
const numberOfDigits = getInteger[0].value.length;
for (let i = 0; i < formattedParts.length; i += 1) {
if (formattedParts[i].type === 'integer') {
firstPart = formattedParts.splice(0, i);
integerPart = formattedParts.splice(0, 1);
}
}
if (integerPart !== undefined) {
const numberOfDigits = integerPart[0].value.length;
const mod3 = numberOfDigits % 3; const mod3 = numberOfDigits % 3;
const groups = Math.floor(numberOfDigits / 3); const groups = Math.floor(numberOfDigits / 3);
const numberArray = []; const numberArray = [];
@ -18,7 +26,7 @@ export function forceAddGroupSeparators(formattedParts, groupSeparator) {
let firstGroup = false; let firstGroup = false;
// Loop through the integer // Loop through the integer
for (let i = 0; i < numberOfDigits; i += 1) { for (let i = 0; i < numberOfDigits; i += 1) {
numberPart += getInteger[0].value[i]; numberPart += integerPart[0].value[i];
// Create first grouping which is < 3 // Create first grouping which is < 3
if (numberPart.length === mod3 && firstGroup === false) { if (numberPart.length === mod3 && firstGroup === false) {
numberArray.push({ type: 'integer', value: numberPart }); numberArray.push({ type: 'integer', value: numberPart });
@ -38,7 +46,7 @@ export function forceAddGroupSeparators(formattedParts, groupSeparator) {
} }
} }
numberArray.push({ type: 'integer', value: numberPart }); numberArray.push({ type: 'integer', value: numberPart });
concatArray = numberArray.concat(formattedParts); concatArray = firstPart.concat(numberArray, formattedParts);
} }
return concatArray; return concatArray;
} }

View file

@ -291,6 +291,7 @@ describe('formatNumber', () => {
localize.locale = 'bg-BG'; localize.locale = 'bg-BG';
expect(formatNumber(1.234, currencyCode('EUR'))).to.equal('1,23 EUR'); expect(formatNumber(1.234, currencyCode('EUR'))).to.equal('1,23 EUR');
expect(formatNumber(1234.567, currencyCode('EUR'))).to.equal('1 234,57 EUR'); expect(formatNumber(1234.567, currencyCode('EUR'))).to.equal('1 234,57 EUR');
expect(formatNumber(-1234.567, currencyCode('EUR'))).to.equal('1 234,57 EUR');
}); });
}); });