@caspater/msisdn
v1.0.1
Published
A package to format or validate msisdns (Malawi)
Readme
MSISDN HELPER
This project is focused on validating and formatting MSISDNs (Mobile Station International Subscriber Directory Number).
Features
- Validate MSISDN numbers.
- Format and normalize phone numbers.
- Support for multiple country codes.
Installation
- npm installation.
npm install @caspater/msisdn
Usage
Import the library and use it in your project:
import { msisdn } from '@caspater/msisdn';
const isValid = msisdn('0888800900').isValid();
console.log(isValid);Creating a new number
The msisdn helper function creates an instance of MSISDN. You can use the get method to invoke the toString method.
import { msisdn } from '@caspater/msisdn';
const msisdnInstance = msisdn('0888800900');
console.log(msisdnInstance.get());
//output 888800900Options
The msisdn factory allows a second parameter to set the configs of the instance
type options = {
autoClean?: boolean; //default true,
country?: string; //default MW,
type?: string; //default null,
};| option | Required | Description |
| --------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| autoClean | optional? | This removes special characters from the msisdn when it is set to true. By default it is set to true. It can set it to false in situations where the number format is to be validated. |
| country | optional? | This is used to set the country. The list of supported countries can be found in the list below. |
| type | optional? | This is used to set the type. The types can be identifies as areas/providers depending on the number structure of the country. The list of supported types can be found in the list below. |
Validation
The isValid function can be used to validate the msisdn. Note set the autoClean option to false if the special characters are to be validated as well.
The isValid function will also use the configured country / type as validators.
import { msisdn } from '@caspater/msisdn';
const msisdnInstance = msisdn('08-88800900', { autoClean: false });
console.log(msisdnInstance.isValid());
//output falseFormatting
The formatting helpers will return the MSISDN instance so chaining can be used to apply multiple formats. Of course some may be overwritten , you can't have everything 😁
| Function | Parameters | Format | Description |
| ---------------- | ------------------- | ---------------------- | ------------------------------------------------------- |
| clean | | ##### | Removes special characters from the msisdn |
| internationalize | withPlus: boolean | {+?}{countryCode}##### | Prepends the country code |
| localize | | {0}##### | Prepends a 0 to the msisdn |
| humanize | | {0}##### | Prepends a 0 to the msisdn |
| dropLeading | | ##### | Drops the country code or the leading 0 from the msisdn |
Getters
The string version of the msisdn can be retrieved by using the get function.
| Function | Description | | -------- | ----------------------------------------------- | | get | Gets the msisdn only when it is valid | | getRaw | Gets the msisdn even when it is not valid |
Supported Countries
| Country | Code | Type | Type Identifier | | ---------------------------------------------------------------------------------- | ---- | -------- | --------------- | | Malawi | MW | TNM | TNM | | | | Airtel | Airtel | | | | Access | Access | | | | MTL | MTL | | Zambia | ZM | MTN | MTN | | | | Airtel | Airtel | | | | Zamtel | Zamtel | | Tanzania(The numbers are potable so type validation may not be accurate sometimes) | TZ | Vodacom | Vodacom | | | | Airtel | Airtel | | | | Tigo | Tigo | | | | Halotel | Halotel | | | | Zantel | Zantel | | | | Smile | Smile | | | | TTCL | TTCL | | Nigeria | NG | Portable | Portable | | Rwanda | RW | MTN | MTN | | | | Airtel | Airtel | | South Africa | ZA | Portable | Portable |
Contributing
Contributions are welcome! Please fork the repository and submit a pull request.
- Clone the repository:
git clone https://github.com/caspater/msisdn.git - Navigate to the project directory:
cd msisdn - Install dependencies:
npm install
License
This project is licensed under the MIT License. See the LICENSE file for details.
