Aller au contenu
GitHub

API JavaScript

@mdgascar/phone

Ce contenu n’est pas encore disponible dans votre langue.

Madagascar (+261) mobile phone numbers. Part of mdg-utils by Mdgascar.

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

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

new MdgError(code, message): MdgError

Creates an error with a shared spec code.

ErrorCode

Shared spec error code.

string

Human-readable explanation, in English.

MdgError

Error.constructor

readonly code: ErrorCode

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

readonly name: "MdgError" = 'MdgError'

Always 'MdgError'.

Error.name

static [hasInstance](value): boolean

Makes instanceof MdgError true for errors from any mdg-utils package or build.

unknown

Any value, usually a caught error.

boolean

true when value is an mdg-utils 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.

A Malagasy mobile number that passed validation. Create it with parsePhoneNumber.

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

readonly [phoneNumberBrand]: true

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

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.

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

Error code shared by every mdg-utils 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 a mobile operator.

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

Identifier of a Malagasy mobile operator.


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

How formatPhoneNumber 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: MdgError; success: false; }

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

T

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

const OPERATOR_NAMES: object

Display name of each mobile operator, by OperatorId.

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

readonly blueline: "Blueline" = 'Blueline'

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

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

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

const SPEC_VERSION: "0.6.0" = '0.6.0'

Version of the shared spec this code was generated from.

formatPhoneNumber(input, format): string

Writes a Malagasy mobile number in a standard form.

string

The number in any spelling accepted by parsePhoneNumber.

PhoneNumberFormat

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

string

The formatted number.

MdgError INVALID_TYPE for an unknown format, plus every error of parsePhoneNumber.

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

isValidPhoneNumber(input): boolean

Tells whether the input is a valid Malagasy mobile number. Never throws.

unknown

Any value.

boolean

true when parsePhoneNumber would succeed.

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

parsePhoneNumber(input): PhoneNumber

Reads a Malagasy mobile number 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.

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

parsePhoneNumber('034 12 345 67');
// { e164: '+261341234567', nationalNumber: '341234567', operator: 'yas' }

safeParsePhoneNumber(input): SafeParseResult<PhoneNumber>

Reads a Malagasy mobile number 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 = safeParsePhoneNumber('+261 34 12 345 67');
if (result.success) result.data.operator; // 'yas'

tryParsePhoneNumber(input): PhoneNumber | undefined

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

Use safeParsePhoneNumber 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 parsePhoneNumber would throw.

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