JavaScript API
@mdgascar/phone-number
Madagascar (+261) phone numbers, mobile and fixed. Part of malagasy-intl by Mdgascar.
Classes
Section titled “Classes”MalagasyIntlError
Section titled “MalagasyIntlError”Error thrown by malagasy-intl functions. Check code, which is the same in every language.
Example
Section titled “Example”try { spell(-1);} catch (error) { if (error instanceof MalagasyIntlError && error.code === 'OUT_OF_RANGE') { // handle it }}Extends
Section titled “Extends”Error
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MalagasyIntlError(
code,message):MalagasyIntlError
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:"MalagasyIntlError"='MalagasyIntlError'
Always 'MalagasyIntlError'.
Overrides
Section titled “Overrides”Error.name
Methods
Section titled “Methods”[hasInstance]()
Section titled “[hasInstance]()”
static[hasInstance](value):boolean
Makes instanceof MalagasyIntlError true for errors from any malagasy-intl package or build.
Parameters
Section titled “Parameters”unknown
Any value, usually a caught error.
Returns
Section titled “Returns”boolean
true when value is an malagasy-intl 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”PhoneArea
Section titled “PhoneArea”The town of a geographic fixed number, as the national numbering plan lists it.
Example
Section titled “Example”const area: PhoneArea = { name: 'Toamasina', province: 'Toamasina' };Properties
Section titled “Properties”
readonlyname:string
Town, e.g. Toamasina.
province
Section titled “province”
readonlyprovince:string
Former province, e.g. Toamasina.
PhoneNumber
Section titled “PhoneNumber”A Malagasy phone number, mobile or fixed, that passed validation. Create it with parsePhone.
Example
Section titled “Example”const phone: PhoneNumber = parsePhone('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.
readonlyarea:PhoneArea|null
The town of a geographic fixed number, or null (mobile, VSAT, or an unlisted town).
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.
readonlytype:PhoneLineType
mobile or fixed.
Type Aliases
Section titled “Type Aliases”ErrorCode
Section titled “ErrorCode”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
Section titled “OperatorId”OperatorId =
"orange"|"airtel"|"yas"|"blueline"|"gulfsat"
Identifier of a Malagasy telephone operator.
PhoneLineType
Section titled “PhoneLineType”PhoneLineType =
"mobile"|"fixed"
What kind of line a number reaches: mobile (codes 3x) or fixed (codes 2x).
Example
Section titled “Example”const type: PhoneLineType = 'fixed';PhoneNumberFormat
Section titled “PhoneNumberFormat”PhoneNumberFormat =
"e164"|"international"|"national"
How formatPhone 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:MalagasyIntlError;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 = safeParsePhone(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 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'
gulfsat
Section titled “gulfsat”
readonlygulfsat:"Gulfsat Madagascar"='Gulfsat Madagascar'
orange
Section titled “orange”
readonlyorange:"Orange Madagascar"='Orange Madagascar'
readonlyyas:"Yas Madagascar"='Yas Madagascar'
Example
Section titled “Example”OPERATOR_NAMES[parsePhone('034 12 345 67').operator]; // 'Yas Madagascar'SPEC_VERSION
Section titled “SPEC_VERSION”
constSPEC_VERSION:"0.14.0"='0.14.0'
Version of the shared spec this code was generated from.
Functions
Section titled “Functions”formatPhone()
Section titled “formatPhone()”formatPhone(
input,format):string
Writes a Malagasy mobile number in a standard form.
Parameters
Section titled “Parameters”string
The number in any spelling accepted by parsePhone.
format
Section titled “format”'e164', 'international' or 'national'.
Returns
Section titled “Returns”string
The formatted number.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE for an unknown format, plus every error of
parsePhone.
Example
Section titled “Example”formatPhone('+261341234567', 'national'); // '034 12 345 67'formatPhone('034 12 345 67', 'international'); // '+261 34 12 345 67'isValidPhone()
Section titled “isValidPhone()”isValidPhone(
input):boolean
Tells whether the input is a valid Malagasy phone number, mobile or fixed. Never throws.
Parameters
Section titled “Parameters”unknown
Any value.
Returns
Section titled “Returns”boolean
true when parsePhone would succeed.
Example
Section titled “Example”isValidPhone('034 12 345 67'); // trueisValidPhone('031 12 345 67'); // falseparsePhone()
Section titled “parsePhone()”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.
Parameters
Section titled “Parameters”string
The number as typed by a user.
Returns
Section titled “Returns”The validated PhoneNumber.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE for non-strings, INVALID_FORMAT for text that is
not a Malagasy number, UNKNOWN_OPERATOR for an unassigned prefix.
Example
Section titled “Example”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()
Section titled “safeParsePhone()”safeParsePhone(
input):SafeParseResult<PhoneNumber>
Reads a Malagasy phone number, mobile or fixed, 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 = safeParsePhone('+261 34 12 345 67');if (result.success) result.data.operator; // 'yas'tryParsePhone()
Section titled “tryParsePhone()”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.
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 parsePhone would throw.
Example
Section titled “Example”tryParsePhone('034 12 345 67')?.operator; // 'yas'tryParsePhone('031 12 345 67'); // undefined