JavaScript API
@mdgascar/phone
Madagascar (+261) mobile phone numbers. Part of mdg-utils by Mdgascar.
Classes
Section titled “Classes”MdgError
Section titled “MdgError”Error thrown by mdg-utils functions. Check code, which is the same in every language.
Example
Section titled “Example”try { numberToWords(-1);} catch (error) { if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') { // handle it }}Extends
Section titled “Extends”Error
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MdgError(
code,message):MdgError
Creates an error with a shared spec code.
Parameters
Section titled “Parameters”Shared spec error code.
message
Section titled “message”string
Human-readable explanation, in English.
Returns
Section titled “Returns”Overrides
Section titled “Overrides”Error.constructor
Properties
Section titled “Properties”
readonlycode:ErrorCode
Why the call failed; the same code in every language.
readonlyname:"MdgError"='MdgError'
Always 'MdgError'.
Overrides
Section titled “Overrides”Error.name
Methods
Section titled “Methods”[hasInstance]()
Section titled “[hasInstance]()”
static[hasInstance](value):boolean
Makes instanceof MdgError true for errors from any mdg-utils package or build.
Parameters
Section titled “Parameters”unknown
Any value, usually a caught error.
Returns
Section titled “Returns”boolean
true when value is an mdg-utils error.
Remarks
Section titled “Remarks”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.
Interfaces
Section titled “Interfaces”PhoneNumber
Section titled “PhoneNumber”A Malagasy mobile number that passed validation. Create it with parsePhoneNumber.
Example
Section titled “Example”const phone: PhoneNumber = parsePhoneNumber('034 12 345 67');phone.e164; // '+261341234567'Properties
Section titled “Properties”[phoneNumberBrand]
Section titled “[phoneNumberBrand]”
readonly[phoneNumberBrand]:true
Marks values that went through validation; it does not exist at runtime.
readonlye164:string
The number in E.164 form, for storage and APIs: +261341234567.
nationalNumber
Section titled “nationalNumber”
readonlynationalNumber:string
The 9 digits after the country code: 341234567.
operator
Section titled “operator”
readonlyoperator:OperatorId
The operator that holds the number’s prefix.
Type Aliases
Section titled “Type Aliases”ErrorCode
Section titled “ErrorCode”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
Section titled “OperatorId”OperatorId =
"orange"|"airtel"|"yas"|"blueline"
Identifier of a Malagasy mobile operator.
PhoneNumberFormat
Section titled “PhoneNumberFormat”PhoneNumberFormat =
"e164"|"international"|"national"
How formatPhoneNumber writes a number.
e164:+261341234567international:+261 34 12 345 67national:034 12 345 67
Example
Section titled “Example”const format: PhoneNumberFormat = 'national';SafeParseResult
Section titled “SafeParseResult”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.
Type Parameters
Section titled “Type Parameters”T
Example
Section titled “Example”const result = safeParsePhoneNumber(input);if (result.success) console.log(result.data);else console.log(result.error.code);Variables
Section titled “Variables”OPERATOR_NAMES
Section titled “OPERATOR_NAMES”
constOPERATOR_NAMES:object
Display name of each mobile operator, by OperatorId.
Type Declaration
Section titled “Type Declaration”airtel
Section titled “airtel”
readonlyairtel:"Airtel Madagascar"='Airtel Madagascar'
blueline
Section titled “blueline”
readonlyblueline:"Blueline"='Blueline'
orange
Section titled “orange”
readonlyorange:"Orange Madagascar"='Orange Madagascar'
readonlyyas:"Yas Madagascar"='Yas Madagascar'
Example
Section titled “Example”OPERATOR_NAMES[parsePhoneNumber('034 12 345 67').operator]; // 'Yas Madagascar'SPEC_VERSION
Section titled “SPEC_VERSION”
constSPEC_VERSION:"0.6.0"='0.6.0'
Version of the shared spec this code was generated from.
Functions
Section titled “Functions”formatPhoneNumber()
Section titled “formatPhoneNumber()”formatPhoneNumber(
input,format):string
Writes a Malagasy mobile number in a standard form.
Parameters
Section titled “Parameters”string
The number in any spelling accepted by parsePhoneNumber.
format
Section titled “format”'e164', 'international' or 'national'.
Returns
Section titled “Returns”string
The formatted number.
Throws
Section titled “Throws”MdgError INVALID_TYPE for an unknown format, plus every error of
parsePhoneNumber.
Example
Section titled “Example”formatPhoneNumber('+261341234567', 'national'); // '034 12 345 67'formatPhoneNumber('034 12 345 67', 'international'); // '+261 34 12 345 67'isValidPhoneNumber()
Section titled “isValidPhoneNumber()”isValidPhoneNumber(
input):boolean
Tells whether the input is a valid Malagasy mobile number. Never throws.
Parameters
Section titled “Parameters”unknown
Any value.
Returns
Section titled “Returns”boolean
true when parsePhoneNumber would succeed.
Example
Section titled “Example”isValidPhoneNumber('034 12 345 67'); // trueisValidPhoneNumber('031 12 345 67'); // falseparsePhoneNumber()
Section titled “parsePhoneNumber()”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.
Parameters
Section titled “Parameters”string
The number as typed by a user.
Returns
Section titled “Returns”The validated PhoneNumber.
Throws
Section titled “Throws”MdgError INVALID_TYPE for non-strings, INVALID_FORMAT for text that is
not a Malagasy number, UNKNOWN_OPERATOR for an unassigned mobile prefix.
Example
Section titled “Example”parsePhoneNumber('034 12 345 67');// { e164: '+261341234567', nationalNumber: '341234567', operator: 'yas' }safeParsePhoneNumber()
Section titled “safeParsePhoneNumber()”safeParsePhoneNumber(
input):SafeParseResult<PhoneNumber>
Reads a Malagasy mobile number without throwing.
Parameters
Section titled “Parameters”unknown
The number as typed by a user, in any accepted spelling.
Returns
Section titled “Returns”{ success: true, data } with the PhoneNumber, or { success: false, error }.
Example
Section titled “Example”const result = safeParsePhoneNumber('+261 34 12 345 67');if (result.success) result.data.operator; // 'yas'tryParsePhoneNumber()
Section titled “tryParsePhoneNumber()”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.
Parameters
Section titled “Parameters”unknown
The number as typed by a user, in any accepted spelling.
Returns
Section titled “Returns”PhoneNumber | undefined
The PhoneNumber, or undefined when parsePhoneNumber would throw.
Example
Section titled “Example”tryParsePhoneNumber('034 12 345 67')?.operator; // 'yas'tryParsePhoneNumber('031 12 345 67'); // undefined