Skip to content

Commit 6bb6616

Browse files
authored
🔀 Merge pull request #720 from FrostCo/fix/negative-numbers-with-commas
Support negative numbers in numberWithCommas
2 parents a9c5433 + 26c980f commit 6bb6616

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

src/script/lib/helper.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,21 @@ export function numberWithCommas(number: number | string): string {
176176
if (typeof number === 'string') number = parseInt(number);
177177
return number.toLocaleString();
178178
} else {
179-
number = number.toString();
179+
const numberStr = number.toString();
180180

181-
// Get numbers before `.` (if present)
182-
const decimalIndex = number.indexOf('.');
183-
let output = decimalIndex === -1 ? number : number.slice(0, decimalIndex);
184-
185-
// Insert commas every 3 digits from the right
186-
for (let i = output.length - 3; i > 0; i -= 3) {
187-
output = output.slice(0, i) + ',' + output.slice(i);
181+
// Use regex to match the sign, integer part, and decimal part
182+
const match = numberStr.match(/^(-?)(\d+)\.?(\d*)$/);
183+
if (!match) {
184+
return numberStr; // Return as-is if it doesn't match expected format
188185
}
189186

190-
// Append fractional part
191-
if (decimalIndex !== -1) {
192-
output += number.slice(decimalIndex);
193-
}
187+
const [, sign, integerPart, decimalPart] = match;
188+
189+
// Add commas to integer part
190+
const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
194191

195-
return output;
192+
// Combine sign, formatted integer, and decimal part
193+
return sign + formattedInteger + (decimalPart ? '.' + decimalPart : '');
196194
}
197195
}
198196

test/spec/lib/helper.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,43 @@ describe('Helper', function () {
385385
expect(numberWithCommas(123)).to.eql('123');
386386
expect(numberWithCommas(1234)).to.eql('1,234');
387387
expect(numberWithCommas(1234567890)).to.eql('1,234,567,890');
388+
expect(numberWithCommas(0)).to.eql('0');
389+
expect(numberWithCommas(-1234)).to.eql('-1,234');
388390
});
389391

390392
it('Works with number string', function () {
391393
expect(numberWithCommas('123')).to.eql('123');
392394
expect(numberWithCommas('1234')).to.eql('1,234');
393395
expect(numberWithCommas('1234567890')).to.eql('1,234,567,890');
396+
expect(numberWithCommas('0')).to.eql('0');
397+
expect(numberWithCommas('-1234')).to.eql('-1,234');
398+
});
399+
400+
describe('Fallback logic when Intl is not available', function () {
401+
let originalIntl: any;
402+
403+
beforeEach(function () {
404+
// Save original Intl
405+
originalIntl = (global as any).Intl;
406+
});
407+
408+
afterEach(function () {
409+
// Restore original Intl
410+
(global as any).Intl = originalIntl;
411+
});
412+
413+
it('should use fallback logic when Intl is not available', function () {
414+
// Remove Intl
415+
delete (global as any).Intl;
416+
417+
expect(numberWithCommas(123)).to.eql('123');
418+
expect(numberWithCommas(1234)).to.eql('1,234');
419+
expect(numberWithCommas(1234567890)).to.eql('1,234,567,890');
420+
expect(numberWithCommas(0)).to.eql('0');
421+
expect(numberWithCommas(-1234)).to.eql('-1,234');
422+
expect(numberWithCommas(1234.56)).to.eql('1,234.56');
423+
expect(numberWithCommas(-1234.56)).to.eql('-1,234.56');
424+
});
394425
});
395426
});
396427

0 commit comments

Comments
 (0)