@eyuel_engida/ethio-helpers 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -0
- package/index.js +41 -0
- package/package.json +12 -0
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Ethio-Helpers ðŠðđ
|
|
2
|
+
|
|
3
|
+
A lightweight, zero-dependency utility library for validating and formatting Ethiopian mobile phone numbers.
|
|
4
|
+
Supports both **Ethio Telecom** and **Safaricom**.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## ð Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @eyuel_engida/ethio-helpers
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## ð Usage
|
|
17
|
+
|
|
18
|
+
### JavaScript
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
const { isEthioPhone, formatEthioPhone } = require('@eyuel_engida/ethio-helpers');
|
|
22
|
+
|
|
23
|
+
// --- 1. Validation ---
|
|
24
|
+
// Checks if the number is a valid Ethio Telecom (09) or Safaricom (07) number
|
|
25
|
+
|
|
26
|
+
console.log(isEthioPhone('0911234567')); // true
|
|
27
|
+
console.log(isEthioPhone('+251712345678')); // true
|
|
28
|
+
console.log(isEthioPhone('0811234567')); // false (Invalid prefix)
|
|
29
|
+
console.log(isEthioPhone('0111550000')); // false (Landline)
|
|
30
|
+
|
|
31
|
+
// --- 2. Formatting ---
|
|
32
|
+
// Converts messy inputs into the standard international format: +251 9XX XXX XXX
|
|
33
|
+
|
|
34
|
+
const formatted = formatEthioPhone('0911223344');
|
|
35
|
+
console.log(formatted);
|
|
36
|
+
// Output: "+251 911 223 344"
|
|
37
|
+
|
|
38
|
+
const formattedSafaricom = formatEthioPhone('0712345678');
|
|
39
|
+
console.log(formattedSafaricom);
|
|
40
|
+
// Output: "+251 712 345 678"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## âĻ Features
|
|
46
|
+
|
|
47
|
+
- â
**Validation**: Correctly identifies valid `09` (Ethio Telecom) and `07` (Safaricom) prefixes
|
|
48
|
+
- â
**Flexible Input**: Supports numbers with `+251`, `251`, `0`, or no prefix
|
|
49
|
+
- â
**Formatting**: Auto-formats valid numbers into a clean, standardized UI-friendly format
|
|
50
|
+
- ðŠķ **Zero Dependencies**: Fast, lightweight, and dependency-free
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## ðĻâðŧ Author
|
|
55
|
+
|
|
56
|
+
Created by **Eyuel**
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## ð License
|
|
61
|
+
|
|
62
|
+
ISC
|
package/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// index.js
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validates if a string is a valid Ethiopian mobile number.
|
|
5
|
+
* Accepts formats: +2519..., 2519..., 09..., 9... (and 07 for Safaricom)
|
|
6
|
+
* Returns: boolean
|
|
7
|
+
*/
|
|
8
|
+
function isEthioPhone(phoneNumber) {
|
|
9
|
+
// This regex checks for optional +251, 251, or 0, followed by 9 or 7, then 8 digits.
|
|
10
|
+
const regex = /^(?:\+251|251|0)?(9|7)\d{8}$/;
|
|
11
|
+
return regex.test(phoneNumber);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Formats a number to the standard international format: +251 9XX XXX XXX
|
|
16
|
+
*/
|
|
17
|
+
function formatEthioPhone(phoneNumber) {
|
|
18
|
+
// 1. Check if it's valid first
|
|
19
|
+
if (!isEthioPhone(phoneNumber)) return "Invalid Phone Number";
|
|
20
|
+
|
|
21
|
+
// 2. Remove all non-number characters (like + or spaces)
|
|
22
|
+
let cleaned = phoneNumber.toString().replace(/\D/g, '');
|
|
23
|
+
|
|
24
|
+
// 3. Normalize to 9-digit format (remove 251 or 0 prefix)
|
|
25
|
+
if (cleaned.startsWith('251')) {
|
|
26
|
+
cleaned = cleaned.slice(3);
|
|
27
|
+
} else if (cleaned.startsWith('0')) {
|
|
28
|
+
cleaned = cleaned.slice(1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 4. Format the string nicely
|
|
32
|
+
// slice(0,1) gets the '9' or '7'
|
|
33
|
+
// slice(1,3) gets the next two digits
|
|
34
|
+
return `+251 ${cleaned.slice(0, 1)}${cleaned.slice(1, 3)} ${cleaned.slice(3, 6)} ${cleaned.slice(6)}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Export the functions so others can use them
|
|
38
|
+
module.exports = {
|
|
39
|
+
isEthioPhone,
|
|
40
|
+
formatEthioPhone
|
|
41
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eyuel_engida/ethio-helpers",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"author": "Eyuel",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"description": "A utility to validate Ethiopian phone numbers"
|
|
12
|
+
}
|