Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep phone number groups at three or four digits

Yudhi Armyndharis0995296244ca
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 84534333f..c569e7e01 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Index a Baileys clear, delete, archive or unread by the same chat jid as its message key, so the phone applies it (#1891). Thanks @rjsebening.
- Key Baileys chat writes and read receipts to the gateway's newest stored message when the session holds none in memory (#1891). Thanks @rjsebening.
+- Group dashboard phone numbers in threes and fours, so `+49 157 333 4 4455` now reads `+49 157 3334 4455`; 10-digit numbers keep their shape (#1940).
## [0.26.0] - 2026-10-28
diff --git a/dashboard/src/utils/formatPhone.test.ts b/dashboard/src/utils/formatPhone.test.ts
index 1609aef70..cffa1f10b 100644
--- a/dashboard/src/utils/formatPhone.test.ts
+++ b/dashboard/src/utils/formatPhone.test.ts
@@ -59,13 +59,34 @@ test('formatPhoneForDisplay accepts a raw JID as input (delegates to parsePhoneF
});
test('formatPhoneForDisplay picks the country code by its ITU prefix: 3 digits, and 1 for +7', () => {
- assert.equal(formatPhoneForDisplay('971501234567'), '+971 501 23 4567');
+ assert.equal(formatPhoneForDisplay('971501234567'), '+971 501 234 567');
assert.equal(formatPhoneForDisplay('2348012345678'), '+234 801 234 5678');
assert.equal(formatPhoneForDisplay('8801712345678'), '+880 171 234 5678');
assert.equal(formatPhoneForDisplay('79161234567'), '+7 916 123 4567');
assert.equal(formatPhoneForDisplay('447911123456'), '+44 791 112 3456');
});
+test('formatPhoneForDisplay never leaves a group shorter than three digits (#1940)', () => {
+ assert.equal(formatPhoneForDisplay('4915733344455'), '+49 157 3334 4455');
+ assert.equal(formatPhoneForDisplay('6281770008896'), '+62 817 7000 8896');
+ assert.equal(formatPhoneForDisplay('5511987654321'), '+55 119 8765 4321');
+ assert.equal(formatPhoneForDisplay('6591234567'), '+65 9123 4567');
+ assert.equal(formatPhoneForDisplay('85291234567'), '+852 9123 4567');
+ assert.equal(formatPhoneForDisplay('62812345678'), '+62 812 345 678');
+ // Five digits after the code cannot be split into threes and fours, so they stay one group.
+ assert.equal(formatPhoneForDisplay('29012345'), '+290 12345');
+ for (let length = 6; length <= 15; length++) {
+ const groups = formatPhoneForDisplay(`62${'8'.repeat(length)}`)!
+ .split(' ')
+ .slice(1);
+ assert.equal(groups.join('').length, length);
+ assert.ok(
+ groups.every(group => group.length === 3 || group.length === 4),
+ `${length} digits: ${groups.join(' ')}`,
+ );
+ }
+});
+
test('isolateNumber keeps a number one left-to-right run inside a right-to-left sentence', () => {
const lri = String.fromCodePoint(0x2066);
const pdi = String.fromCodePoint(0x2069);
diff --git a/dashboard/src/utils/formatPhone.ts b/dashboard/src/utils/formatPhone.ts
index c1dd5e496..6957eea10 100644
--- a/dashboard/src/utils/formatPhone.ts
+++ b/dashboard/src/utils/formatPhone.ts
@@ -34,8 +34,9 @@ const TWO_DIGIT_COUNTRY_CODE = /^(?:2[07]|3[0-469]|4[013-9]|5[1-8]|6[0-6]|8[1246
/**
* Format a digits-only phone number (already prefixed with its country code) into a
- * human-friendly international form. Uses 3-3-4 grouping after the country code; short codes
- * pass through. This is purely cosmetic — the raw JID stays authoritative for any technical use.
+ * human-friendly international form: groups of three after the country code, the last one or two
+ * of four when the length calls for it (see below); short codes pass through. This is purely
+ * cosmetic — the raw JID stays authoritative for any technical use.
*
* formatPhoneForDisplay('628123456789') → '+62 812 345 6789'
* formatPhoneForDisplay('14155552671') → '+1 415 555 2671'
@@ -54,12 +55,14 @@ export function formatPhoneForDisplay(phoneOrJid: string): string | null {
const cc = digits.slice(0, ccLen);
const rest = digits.slice(ccLen);
if (rest.length <= 4) return `+${cc} ${rest}`;
- // Standard display convention: trailing group of 4, then 3-digit groups from the left for the
- // remainder. Matches both the Indonesian "812 345 6789" and US "415 555 2671" shapes.
- const last4 = rest.slice(-4);
- const prefix = rest.slice(0, -4);
- const prefixGroups = prefix.match(/.{1,3}/g) ?? [prefix];
- return `+${cc} ${[...prefixGroups, last4].join(' ')}`;
+ // Groups of three from the left; a length that is not a multiple of three makes the last one or
+ // two groups four digits, so no group is shorter than three ("812 345 6789", "817 7000 8896",
+ // "9123 4567", "812 345 678"). Five digits cannot be split that way and stay whole.
+ const fours = rest.length % 3;
+ const cut = rest.length - 4 * fours;
+ if (cut < 0) return `+${cc} ${rest}`;
+ const groups = [...(rest.slice(0, cut).match(/.{3}/g) ?? []), ...(rest.slice(cut).match(/.{4}/g) ?? [])];
+ return `+${cc} ${groups.join(' ')}`;
}
/**