diff --git a/README.md b/README.md index fe8b6a6..2a975aa 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A simple npm package to format Ethiopian phone numbers to the ISP standard code. ### Installation via npm ``` -npm install --save phone-formatter-eth +npm install --save phone-formater-eth ``` ### Via CDN @@ -16,11 +16,11 @@ npm install --save phone-formatter-eth ### Usage ``` - const { formatPhone, checkOperator, isValid } = require('phone-formatter-eth'); + const { formatPhone, checkOperator, isValid } = require('phone-formater-eth'); Or - import { formatPhone, checkOperator, isValid } from 'phone-formatter-eth'; + import { formatPhone, checkOperator, isValid } from 'phone-formater-eth'; ``` @@ -67,7 +67,7 @@ console.log(isValid('0812345678')); // Outputs: false ### TODO ☑️ -1. Parse - Cleans up or normalizes phone.e.g special characters like - and () +1. parse - Cleans up or normalizes phone.e.g special characters like - and () 2. toLocal - Converts phone number to local | Ethiopian format. 09 or 07 format 3. toInternational - Converts phone number to Int'l | Ethiopian format. +2519/7 4. isMobile - check if it's mobile sim. eg 09/07/+2519/7 diff --git a/dist/index.js b/dist/index.js index c1355db..26a54e1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4,18 +4,21 @@ exports.formatPhone = formatPhone; exports.checkOperator = checkOperator; exports.isValid = isValid; function formatPhone(phone) { - const phone_length = phone === null || phone === void 0 ? void 0 : phone.toString().length; - if (phone_length === 13 && phone.startsWith("+251")) { - return phone; + const formatted_phone = phone.replace(/[^+\d]/g, ""); + const phone_length = formatted_phone === null || formatted_phone === void 0 ? void 0 : formatted_phone.toString().length; + if (phone_length === 13 && formatted_phone.startsWith("+251")) { + return formatted_phone; } - else if (phone_length === 12 && phone.startsWith("251")) { - return `+${phone}`; + else if (phone_length === 12 && formatted_phone.startsWith("251")) { + return `+${formatted_phone}`; } - else if (phone_length === 10 && ["09", "07"].includes(phone.slice(0, 2))) { - return `+251${phone.slice(1)}`; + else if (phone_length === 10 && + ["09", "07"].includes(formatted_phone.slice(0, 2))) { + return `+251${formatted_phone.slice(1)}`; } - else if (phone_length === 9 && ["9", "7"].includes(phone.charAt(0))) { - return `+251${phone}`; + else if (phone_length === 9 && + ["9", "7"].includes(formatted_phone.charAt(0))) { + return `+251${formatted_phone}`; } else { return "INVALID_PHONE_NUMBER"; diff --git a/package.json b/package.json index c2f1d1c..602bd7f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "phone-formatter-eth", - "version": "1.1.6", + "name": "phone-formater-eth", + "version": "1.1.6-beta", "description": "Format Ethiopian phone numbers and check ISP under the number.", "main": "dist/index.js", "types": "dist/index.d.ts",