Skip to content
GitHub

JavaScript API

@mdgascar/phone-number

Madagascar (+261) phone numbers, mobile and fixed. Part of malagasy-intl by Mdgascar.

Error thrown by malagasy-intl functions. Check code, which is the same in every language.

try {
spell(-1);
} catch (error) {
if (error instanceof MalagasyIntlError && error.code === 'OUT_OF_RANGE') {
// handle it
}
}
  • Error

new MalagasyIntlError(code, message): MalagasyIntlError

Creates an error with a shared spec code.

ErrorCode

Shared spec error code.

string

Human-readable explanation, in English.

MalagasyIntlError

Error.constructor

readonly code: ErrorCode

Why the call failed; the same code in every language.

readonly name: "MalagasyIntlError" = 'MalagasyIntlError'

Always 'MalagasyIntlError'.

Error.name

static [hasInstance](value): boolean

Makes instanceof MalagasyIntlError true for errors from any malagasy-intl package or build.

unknown

Any value, usually a caught error.

boolean

true when value is an malagasy-intl error.

Each package, and its ESM and CommonJS builds, has its own copy of this class, so the check looks at the error’s shape instead of its prototype chain.

The town of a geographic fixed number, as the national numbering plan lists it.

const area: PhoneArea = { name: 'Toamasina', province: 'Toamasina' };

readonly name: string

Town, e.g. Toamasina.

readonly province: string

Former province, e.g. Toamasina.


A Malagasy phone number, mobile or fixed, that passed validation. Create it with parsePhone.

const phone: PhoneNumber = parsePhone('034 12 345 67');
phone.e164; // '+261341234567'

readonly [phoneNumberBrand]: true

Marks values that went through validation; it does not exist at runtime.

readonly area: PhoneArea | null

The town of a geographic fixed number, or null (mobile, VSAT, or an unlisted town).

readonly e164: string

The number in E.164 form, for storage and APIs: +261341234567.

readonly nationalNumber: string

The 9 digits after the country code: 341234567.

readonly operator: OperatorId

The operator that holds the number’s prefix.

readonly type: PhoneLineType

mobile or fixed.

ErrorCode = "INVALID_TYPE" | "OUT_OF_RANGE" | "INVALID_FORMAT" | "UNKNOWN_OPERATOR" | "PRECISION_LOSS"

Error code shared by every malagasy-intl language port.

  • INVALID_TYPE: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
  • OUT_OF_RANGE: The input has the right type but lies outside the supported range.
  • INVALID_FORMAT: The input text cannot be read as a value of this module (wrong characters or wrong length).
  • UNKNOWN_OPERATOR: The phone number is well formed but its operator code is not assigned to an operator.
  • PRECISION_LOSS: The value is exact in the text but the port’s number type cannot hold it exactly; ask for the exact text instead.

OperatorId = "orange" | "airtel" | "yas" | "blueline" | "gulfsat"

Identifier of a Malagasy telephone operator.


PhoneLineType = "mobile" | "fixed"

What kind of line a number reaches: mobile (codes 3x) or fixed (codes 2x).

const type: PhoneLineType = 'fixed';

PhoneNumberFormat = "e164" | "international" | "national"

How formatPhone writes a number.

  • e164: +261341234567
  • international: +261 34 12 345 67
  • national: 034 12 345 67
const format: PhoneNumberFormat = 'national';

SafeParseResult<T> = { data: T; success: true; } | { error: MalagasyIntlError; success: false; }

Outcome of a safeParse… function: the value, or the error that would have been thrown.

T

const result = safeParsePhone(input);
if (result.success) console.log(result.data);
else console.log(result.error.code);

const OPERATOR_NAMES: object

Display name of each operator, by OperatorId.

readonly airtel: "Airtel Madagascar" = 'Airtel Madagascar'

readonly blueline: "Blueline" = 'Blueline'

readonly gulfsat: "Gulfsat Madagascar" = 'Gulfsat Madagascar'

readonly orange: "Orange Madagascar" = 'Orange Madagascar'

readonly yas: "Yas Madagascar" = 'Yas Madagascar'

OPERATOR_NAMES[parsePhone('034 12 345 67').operator]; // 'Yas Madagascar'

const SPEC_VERSION: "0.14.0" = '0.14.0'

Version of the shared spec this code was generated from.

formatPhone(input, format): string

Writes a Malagasy mobile number in a standard form.

string

The number in any spelling accepted by parsePhone.

PhoneNumberFormat

'e164', 'international' or 'national'.

string

The formatted number.

MalagasyIntlError INVALID_TYPE for an unknown format, plus every error of parsePhone.

formatPhone('+261341234567', 'national'); // '034 12 345 67'
formatPhone('034 12 345 67', 'international'); // '+261 34 12 345 67'

isValidPhone(input): boolean

Tells whether the input is a valid Malagasy phone number, mobile or fixed. Never throws.

unknown

Any value.

boolean

true when parsePhone would succeed.

isValidPhone('034 12 345 67'); // true
isValidPhone('031 12 345 67'); // false

parsePhone(input): PhoneNumber

Reads a Malagasy phone number, mobile or fixed, written in any common way.

Accepts 034 12 345 67, +261 34 12 345 67, 00261…, 261… and 34 12 345 67; spaces, dashes, dots and parentheses are ignored.

string

The number as typed by a user.

PhoneNumber

The validated PhoneNumber.

MalagasyIntlError INVALID_TYPE for non-strings, INVALID_FORMAT for text that is not a Malagasy number, UNKNOWN_OPERATOR for an unassigned prefix.

parsePhone('034 12 345 67');
// { e164: '+261341234567', nationalNumber: '341234567', operator: 'yas', type: 'mobile', area: null }
parsePhone('020 53 123 45').area; // { name: 'Toamasina', province: 'Toamasina' }

safeParsePhone(input): SafeParseResult<PhoneNumber>

Reads a Malagasy phone number, mobile or fixed, without throwing.

unknown

The number as typed by a user, in any accepted spelling.

SafeParseResult<PhoneNumber>

{ success: true, data } with the PhoneNumber, or { success: false, error }.

const result = safeParsePhone('+261 34 12 345 67');
if (result.success) result.data.operator; // 'yas'

tryParsePhone(input): PhoneNumber | undefined

Reads a Malagasy phone number, or gives undefined instead of throwing.

Use safeParsePhone when you need to know why the input was rejected.

unknown

The number as typed by a user, in any accepted spelling.

PhoneNumber | undefined

The PhoneNumber, or undefined when parsePhone would throw.

tryParsePhone('034 12 345 67')?.operator; // 'yas'
tryParsePhone('031 12 345 67'); // undefined