Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): move the pasted number reader to a shared util
Yudhi Armyndharis332e4edd0e64
diff --git a/dashboard/src/pages/NumberChecker.tsx b/dashboard/src/pages/NumberChecker.tsxindex 12902c7ad..b05bd76a5 100644--- a/dashboard/src/pages/NumberChecker.tsx+++ b/dashboard/src/pages/NumberChecker.tsx@@ -27,6 +27,7 @@ import { useToast } from '../hooks/useToast';import { useRole } from '../hooks/useRole';import { escapeCsvCell } from '../utils/csv';import { isGatewayRefusal } from '../utils/groupSelection';+import { normalizePhoneText } from '../utils/phone';import './NumberChecker.css';export type CheckStatus = 'registered' | 'unregistered' | 'failed';@@ -71,29 +72,6 @@ export interface CheckResultItem extends CheckNumberResponse {const timeOfDay = (iso: string) => new Date(iso).toLocaleTimeString();-// Unicode keeps every script's decimal digits in an unbroken run that starts at its zero.-function asciiDigit(digit: string): string {- const code = digit.codePointAt(0)!;- let zero = code;- while (/\p{Nd}/u.test(String.fromCodePoint(zero - 1))) zero--;- return String((code - zero) % 10);-}--// The text as typed or pasted: the full-width forms of ASCII characters (a plus or a digit from a CJK input method)-// folded to ASCII, invisible format marks such as the direction marks of a right-to-left chat dropped, and every-// script's decimal digits as ASCII ones. Only that block is folded: a general compatibility fold would also turn-// circled and superscript numbers and fractions, which are list bullets and footnote marks, into digits.-function normalizePhoneText(text: string): string {- return [...text]- .map(ch => {- const code = ch.codePointAt(0)!;- return code >= 0xff01 && code <= 0xff5e ? String.fromCharCode(code - 0xfee0) : ch;- })- .join('')- .replace(/\p{Cf}/gu, '')- .replace(/\p{Nd}/gu, asciiDigit);-}-export function NumberChecker() {const { t } = useTranslation();useDocumentTitle(t('nav.numberChecker'));diff --git a/dashboard/src/utils/phone.test.ts b/dashboard/src/utils/phone.test.tsnew file mode 100644index 000000000..a43895d76--- /dev/null+++ b/dashboard/src/utils/phone.test.ts@@ -0,0 +1,32 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { normalizePhoneText } from './phone.ts';++const char = (code: number) => String.fromCharCode(code);+const digitsOf = (zero: number) => [...'0123456789'].map(d => char(zero + Number(d))).join('');++test('the digits of any script read as ASCII digits', () => {+ for (const zero of [0x0660, 0x06f0, 0x0966, 0x0c66, 0xff10]) {+ assert.equal(normalizePhoneText(digitsOf(zero)), '0123456789');+ }+ assert.equal(normalizePhoneText(`+${digitsOf(0x0660).slice(0, 2)} 6281`), '+01 6281');+});++test('the full-width forms of ASCII characters read as ASCII', () => {+ assert.equal(normalizePhoneText(`${char(0xff0b)}65 8123 4567`), '+65 8123 4567');+ assert.equal(normalizePhoneText(`6281${char(0xff0c)}6282`), '6281,6282');+});++test('invisible format marks are dropped', () => {+ assert.equal(normalizePhoneText(`${char(0x202a)}+65 8123${char(0x202c)}`), '+65 8123');+});++test('circled, superscript and fraction characters are left alone', () => {+ const marks = `${char(0x2460)}${char(0x00b9)}${char(0x00bc)}`;+ assert.equal(normalizePhoneText(`${marks}628`), `${marks}628`);+});++test('ASCII text, letters and punctuation come back unchanged', () => {+ assert.equal(normalizePhoneText('+62 (812) 345-6789, abc\n'), '+62 (812) 345-6789, abc\n');+ assert.equal(normalizePhoneText(''), '');+});diff --git a/dashboard/src/utils/phone.ts b/dashboard/src/utils/phone.tsnew file mode 100644index 000000000..fc8f4bd80--- /dev/null+++ b/dashboard/src/utils/phone.ts@@ -0,0 +1,25 @@+// Reading a phone number typed or pasted into the dashboard. The gateway takes ASCII digits only, while an operator+// may type or paste the number in the digits of their own script or from a CJK input method.++// Unicode keeps every script's decimal digits in an unbroken run that starts at its zero.+function asciiDigit(digit: string): string {+ const code = digit.codePointAt(0)!;+ let zero = code;+ while (/\p{Nd}/u.test(String.fromCodePoint(zero - 1))) zero--;+ return String((code - zero) % 10);+}++// The text as typed or pasted: the full-width forms of ASCII characters (a plus or a digit from a CJK input method)+// folded to ASCII, invisible format marks such as the direction marks of a right-to-left chat dropped, and every+// script's decimal digits as ASCII ones. Only that block is folded: a general compatibility fold would also turn+// circled and superscript numbers and fractions, which are list bullets and footnote marks, into digits.+export function normalizePhoneText(text: string): string {+ return [...text]+ .map(ch => {+ const code = ch.codePointAt(0)!;+ return code >= 0xff01 && code <= 0xff5e ? String.fromCharCode(code - 0xfee0) : ch;+ })+ .join('')+ .replace(/\p{Cf}/gu, '')+ .replace(/\p{Nd}/gu, asciiDigit);+}