Skip to content

Commit

Permalink
feat: implemented isValid` check for if it's valid phone number o…
Browse files Browse the repository at this point in the history
…r not
  • Loading branch information
HuluWZ committed Aug 19, 2024
1 parent 38ab3d3 commit 66f1fdf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ npm install --save phone-formater-eth

### Usage
```
const { formatPhone,checkOperator } = require('phone-formater-eth');
const { formatPhone, checkOperator, isValid } = require('phone-formater-eth');
Or
import { formatPhone,checkOperator } from 'phone-formater-eth';
import { formatPhone, checkOperator, isValid } from 'phone-formater-eth';
```

Expand Down Expand Up @@ -50,14 +50,26 @@ console.log(checkOperator('251912345678')); // Outputs: Ethio Telecom
console.log(checkOperator('0812345678')); // Outputs: Unknown
```

3. <b>isValid</b> - Checks if phone number is valid or not
##### Parameters - phone (string): The phone number to check.
##### Returns - (boolean): <i>true</i> if the phone is valid else <i>false</i>
### Examples

```
console.log(isValid('712345678')); // Outputs: true
console.log(isValid('+251912345678')); // Outputs: true
console.log(isValid('07812345678')); // Outputs: false
console.log(isValid('251912345678')); // Outputs: true
console.log(isValid('0812345678')); // Outputs: false
```

### TODO ☑️

1. Validate - Returns <i>boolean</i> if it's Valid phone number or not
2. Parse - Cleans up or normalizes phone.e.g special characters like - and ()
3. toLocal - Converts phone number to local | Ethiopian format. 09 or 07 format
4. toInternational - Converts phone number to Int'l | Ethiopian format. +2519/7
5. isMobile - check if it's mobile sim. eg 09/07/+2519/7
6. isLandline - check if it's landline sim. eg. +2511
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
5. isLandline - check if it's landline sim. eg. +2511



Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phone-formater-eth",
"version": "1.1.3",
"version": "1.1.4",
"description": "Format Ethiopian phone numbers and check ISP under the number.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function formatPhone(phone: string) {
} else if (phone_length === 9 && ["9", "7"].includes(phone.charAt(0))) {
return `+251${phone}`;
} else {
return "INVALID PHONE NUMBER";
return "INVALID_PHONE_NUMBER";
}
}

Expand All @@ -23,3 +23,8 @@ export function checkOperator(phone: string) {
return "UNKNOWN";
}
}

export function isValid(phone: string) {
const formattedPhone = formatPhone(phone);
return formattedPhone === "INVALID_PHONE_NUMBER" ? false : true;
}
24 changes: 20 additions & 4 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatPhone, checkOperator } from "../src/index";
import { formatPhone, checkOperator, isValid } from "../src/index";

describe("formatPhone", () => {
test("should format phone numbers correctly", () => {
Expand All @@ -10,9 +10,9 @@ describe("formatPhone", () => {
expect(formatPhone("0712345678")).toBe("+251712345678"); // Incorrect format, treated as unknown
expect(formatPhone("912345678")).toBe("+251912345678"); // 9-digit ethio-tel number, missing '+251'
expect(formatPhone("712345678")).toBe("+251712345678"); // 9-digit safari number, missing '+251'
expect(formatPhone("1234567890")).toBe("INVALID PHONE NUMBER"); // Invalid Phone number
expect(formatPhone("812345678")).toBe("INVALID PHONE NUMBER"); // Invalid Phone number
expect(formatPhone("")).toBe("INVALID PHONE NUMBER"); // Empty input
expect(formatPhone("1234567890")).toBe("INVALID_PHONE_NUMBER"); // INVALID_PHONE_NUMBER
expect(formatPhone("812345678")).toBe("INVALID_PHONE_NUMBER"); // INVALID_PHONE_NUMBER
expect(formatPhone("")).toBe("INVALID_PHONE_NUMBER"); // Empty input
});
});

Expand All @@ -31,3 +31,19 @@ describe("checkOperator", () => {
expect(checkOperator("")).toBe("UNKNOWN"); // Empty input
});
});

describe("isValid", () => {
test("should identify if phone number is valid or not correctly", () => {
// Test with different formatted numbers and expected operators
expect(isValid("0912345678")).toBe(true); // Standard 10-digit format
expect(isValid("+251912345678")).toBe(true); // Already formatted number
expect(isValid("+251712345678")).toBe(true); // Already formatted number
expect(isValid("251912345678")).toBe(true); // Number starting with '07'
expect(isValid("0912345678")).toBe(true); // Missing '+2519'
expect(isValid("0712345678")).toBe(true); // Missing prefix `+2517`
expect(isValid("712345678")).toBe(true); // 9-digit number
expect(isValid("1234567890")).toBe(false); // Invalid Phone Number input
expect(isValid("812345678")).toBe(false); // Invalid Phone Number input
expect(isValid("")).toBe(false); // Empty input
});
});

0 comments on commit 66f1fdf

Please sign in to comment.