Skip to content
GitHub

Modules

Phone numbers

Read, check and format Madagascar (+261) phone numbers, mobile and fixed, written in any common way, and know the operator of each one.

  1. +261Country code
  2. 34Operator prefix
  3. 12 345 67Subscriber number
The library reads:
Operator
Yas Madagascar
Line
mobile

Result

+261 34 12 345 67 · Yas Madagascar

JSformatPhone('034 12 345 67', 'international')PHPPhoneNumber::parse('034 12 345 67')->format(PhoneNumberFormat::International)
Open in the playground →
parsePhone(input)Text → parsed value, or throws
safeParsePhone(input)Text → parsed value or error code, never throws
tryParsePhone(input) → undefinedText → parsed value, or nothing, never throws
isValidPhone(input)Any value → true/false, never throws
formatPhone(input, format)Text → e164, international or national
OPERATOR_NAMES[id]Operator id → name, e.g. yas → Yas Madagascar

Spaces, dashes, dots, parentheses, +261, 00261 and the leading 0 are all accepted.

import { parsePhone } from '@mdgascar/phone-number';
parsePhone('034 12 345 67');
// { e164: '+261341234567', nationalNumber: '341234567', operator: 'yas', type: 'mobile', area: null }
parsePhone('+261 32 12 345 67').operator; // 'orange'

Fixed-line numbers are read too: 020 for the Yas (Telecom Malagasy) network and 022 for Gulfsat. A parsed number tells its type, and the town of a geographic number when the national plan lists it.

  1. +261Country code
  2. 20Operator prefix
  3. 22 421 19Subscriber number
The library reads:
Operator
Yas Madagascar
Line
fixed
Town
Antananarivo
parsePhone('020 22 421 19').type; // 'fixed'
parsePhone('020 22 421 19').area; // { name: 'Antananarivo', province: 'Antananarivo' }
parsePhone('034 12 345 67').type; // 'mobile'

Three formats: e164 for storage and APIs, international for people abroad, national for people in Madagascar.

Results computed by the library
CaseCallResult
E.164formatPhone('0341234567', 'e164')+261341234567
internationalformatPhone('0341234567', 'international')+261 34 12 345 67
nationalformatPhone('0341234567', 'national')034 12 345 67
import { formatPhone } from '@mdgascar/phone-number';
formatPhone('034 12 345 67', 'e164'); // '+261341234567'
formatPhone('034 12 345 67', 'international'); // '+261 34 12 345 67'
formatPhone('+261341234567', 'national'); // '034 12 345 67'
import { OPERATOR_NAMES, parsePhone } from '@mdgascar/phone-number';
OPERATOR_NAMES[parsePhone('034 12 345 67').operator]; // 'Yas Madagascar'
OPERATOR_NAMES.orange; // 'Orange Madagascar'

isValid never throws. safeParse tells you why a number was rejected, tryParse gives you the number or nothing.

import { isValidPhone, safeParsePhone, tryParsePhone } from '@mdgascar/phone-number';
isValidPhone('031 12 345 67'); // false
const result = safeParsePhone('031 12 345 67');
if (!result.success) result.error.code; // 'UNKNOWN_OPERATOR'
tryParsePhone('031 12 345 67'); // undefined
Input Error code
Not a string INVALID_TYPE
Not a Malagasy phone number (wrong length, letters) INVALID_FORMAT
Prefix not assigned to an operator (031) UNKNOWN_OPERATOR