voko-sdk
Version:
Process payments with ease
53 lines (52 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PhoneValidator = void 0;
const types_1 = require("../core/types");
class PhoneValidator {
static TANZANIA_PREFIX = '+255';
static OPERATORS_MAP = {
'74': types_1.MobileOperator.VODACOM_MPESA,
'75': types_1.MobileOperator.VODACOM_MPESA,
'76': types_1.MobileOperator.VODACOM_MPESA,
'68': types_1.MobileOperator.AIRTEL_MONEY,
'69': types_1.MobileOperator.AIRTEL_MONEY,
'71': types_1.MobileOperator.TIGO_PESA,
'65': types_1.MobileOperator.HALO_PESA,
'67': types_1.MobileOperator.HALO_PESA,
};
static isValid(phoneNumber) {
const normalized = this.normalize(phoneNumber);
const regex = /^\+255[67]\d{8}$/;
return regex.test(normalized);
}
static normalize(phoneNumber) {
let cleaned = phoneNumber.replace(/[^\d+]/g, '');
if (cleaned.startsWith('0')) {
cleaned = this.TANZANIA_PREFIX + cleaned.substring(1);
}
else if (cleaned.startsWith('255')) {
cleaned = '+' + cleaned;
}
else if (!cleaned.startsWith('+255')) {
cleaned = this.TANZANIA_PREFIX + cleaned;
}
return cleaned;
}
static detectOperator(phoneNumber) {
const normalized = this.normalize(phoneNumber);
if (!this.isValid(normalized))
return null;
const prefix = normalized.substring(4, 6);
return this.OPERATORS_MAP[prefix] || null;
}
static getOperatorPrefix(operator) {
const prefixes = [];
for (const [prefix, op] of Object.entries(this.OPERATORS_MAP)) {
if (op === operator) {
prefixes.push(prefix);
}
}
return prefixes;
}
}
exports.PhoneValidator = PhoneValidator;