From 82753bd2e2c8e69235c9f4c0f9b9fd21328f0921 Mon Sep 17 00:00:00 2001 From: Richard Frost Date: Sun, 26 Oct 2025 14:55:34 -0600 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=85=20(dev):=20Add=20numberWithCommas?= =?UTF-8?q?=20negative=20and=20zero=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/spec/lib/helper.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/spec/lib/helper.spec.ts b/test/spec/lib/helper.spec.ts index fe2dcf46..9ea564ee 100644 --- a/test/spec/lib/helper.spec.ts +++ b/test/spec/lib/helper.spec.ts @@ -385,12 +385,16 @@ describe('Helper', function () { expect(numberWithCommas(123)).to.eql('123'); expect(numberWithCommas(1234)).to.eql('1,234'); expect(numberWithCommas(1234567890)).to.eql('1,234,567,890'); + expect(numberWithCommas(0)).to.eql('0'); + expect(numberWithCommas(-1234)).to.eql('-1,234'); }); it('Works with number string', function () { expect(numberWithCommas('123')).to.eql('123'); expect(numberWithCommas('1234')).to.eql('1,234'); expect(numberWithCommas('1234567890')).to.eql('1,234,567,890'); + expect(numberWithCommas('0')).to.eql('0'); + expect(numberWithCommas('-1234')).to.eql('-1,234'); }); }); From e2f058eb9e99a2d0f2543488065e174d7f9e1757 Mon Sep 17 00:00:00 2001 From: Richard Frost Date: Sun, 26 Oct 2025 14:57:52 -0600 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20(dev):=20NumberWithCom?= =?UTF-8?q?mas=20fallback=20negative=20and=20zero?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/script/lib/helper.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/script/lib/helper.ts b/src/script/lib/helper.ts index beb24bea..04dedd49 100644 --- a/src/script/lib/helper.ts +++ b/src/script/lib/helper.ts @@ -176,23 +176,21 @@ export function numberWithCommas(number: number | string): string { if (typeof number === 'string') number = parseInt(number); return number.toLocaleString(); } else { - number = number.toString(); + const numberStr = number.toString(); - // Get numbers before `.` (if present) - const decimalIndex = number.indexOf('.'); - let output = decimalIndex === -1 ? number : number.slice(0, decimalIndex); - - // Insert commas every 3 digits from the right - for (let i = output.length - 3; i > 0; i -= 3) { - output = output.slice(0, i) + ',' + output.slice(i); + // Use regex to match the sign, integer part, and decimal part + const match = numberStr.match(/^(-?)(\d+)\.?(\d*)$/); + if (!match) { + return numberStr; // Return as-is if it doesn't match expected format } - // Append fractional part - if (decimalIndex !== -1) { - output += number.slice(decimalIndex); - } + const [, sign, integerPart, decimalPart] = match; + + // Add commas to integer part + const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ','); - return output; + // Combine sign, formatted integer, and decimal part + return sign + formattedInteger + (decimalPart ? '.' + decimalPart : ''); } } From 26c980f3bb7c09221c7af7ae2e7003c6a33dfb2b Mon Sep 17 00:00:00 2001 From: Richard Frost Date: Sun, 26 Oct 2025 14:58:49 -0600 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=85=20(dev):=20Add=20numberWithCommas?= =?UTF-8?q?=20fallback=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/spec/lib/helper.spec.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/spec/lib/helper.spec.ts b/test/spec/lib/helper.spec.ts index 9ea564ee..c201b0ca 100644 --- a/test/spec/lib/helper.spec.ts +++ b/test/spec/lib/helper.spec.ts @@ -396,6 +396,33 @@ describe('Helper', function () { expect(numberWithCommas('0')).to.eql('0'); expect(numberWithCommas('-1234')).to.eql('-1,234'); }); + + describe('Fallback logic when Intl is not available', function () { + let originalIntl: any; + + beforeEach(function () { + // Save original Intl + originalIntl = (global as any).Intl; + }); + + afterEach(function () { + // Restore original Intl + (global as any).Intl = originalIntl; + }); + + it('should use fallback logic when Intl is not available', function () { + // Remove Intl + delete (global as any).Intl; + + expect(numberWithCommas(123)).to.eql('123'); + expect(numberWithCommas(1234)).to.eql('1,234'); + expect(numberWithCommas(1234567890)).to.eql('1,234,567,890'); + expect(numberWithCommas(0)).to.eql('0'); + expect(numberWithCommas(-1234)).to.eql('-1,234'); + expect(numberWithCommas(1234.56)).to.eql('1,234.56'); + expect(numberWithCommas(-1234.56)).to.eql('-1,234.56'); + }); + }); }); describe('prettyPrintArray()', function () {