-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
68 lines (59 loc) · 2.03 KB
/
example.js
File metadata and controls
68 lines (59 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
const {
normalizeE164,
formatLocal,
isValidSomaliMobile,
getOperator,
normalizeE164Safe,
formatLocalSafe,
getOperatorSafe,
validate,
SomaliPhoneError,
ERROR_CODES,
} = require("./src/index.js");
console.log("=== Comprehensive sophone API Examples ===\n");
const testNumbers = [
"0611234567", // valid Hormuud
"0621234567", // valid Somtel
"invalid", // no digits
"123", // too short
"12345678901234", // too long
"0111234567", // invalid prefix
"", // empty
];
testNumbers.forEach((number) => {
console.log(`\n📱 Testing: "${number}"`);
// 1. Basic validation (never throws)
const isValid = isValidSomaliMobile(number);
console.log(` Valid: ${isValid ? "✅" : "❌"}`);
// 2. Comprehensive validation with details
const result = validate(number);
if (result.ok) {
console.log(` ✅ Validation Success:`);
console.log(` E164: ${result.value.e164}`);
console.log(` Local: ${result.value.local}`);
console.log(` Operator: ${result.value.operator || "unknown"}`);
} else {
console.log(` ❌ Validation Error:`);
console.log(` Code: ${result.error.code}`);
console.log(` Message: ${result.error.message}`);
}
// 3. Safe functions (never throw)
console.log(` Safe E164: ${normalizeE164Safe(number) || "null"}`);
console.log(` Safe Local: ${formatLocalSafe(number) || "null"}`);
console.log(` Safe Operator: ${getOperatorSafe(number) || "null"}`);
// 4. Throwing functions with error handling
try {
const e164 = normalizeE164(number);
console.log(` Throwing E164: ${e164}`);
} catch (error) {
if (error instanceof SomaliPhoneError) {
console.log(` Throwing E164 Error: [${error.code}] ${error.message}`);
}
}
});
console.log("\n=== Error Code Examples ===");
console.log(`INVALID_LENGTH: ${ERROR_CODES.INVALID_LENGTH}`);
console.log(`INVALID_PREFIX: ${ERROR_CODES.INVALID_PREFIX}`);
console.log(`INVALID_INPUT: ${ERROR_CODES.INVALID_INPUT}`);
console.log(`UNKNOWN: ${ERROR_CODES.UNKNOWN}`);