operator-checker
Version:
Iranian mobile operator detection package for Vue 3 and TypeScript
129 lines • 4.76 kB
JavaScript
;
/**
* Iranian Mobile Operator Checker
* Detects mobile operators based on the first 3 digits of phone numbers
*/
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OPERATORS = exports.Operator = void 0;
exports.detectOperator = detectOperator;
exports.getOperatorInfo = getOperatorInfo;
exports.getOperatorNameFa = getOperatorNameFa;
exports.getOperatorName = getOperatorName;
exports.isValidIranianMobileNumber = isValidIranianMobileNumber;
var Operator;
(function (Operator) {
Operator[Operator["IRANCELL"] = 0] = "IRANCELL";
Operator[Operator["HAMRAH_E_AVAL"] = 1] = "HAMRAH_E_AVAL";
Operator[Operator["RIGHTEL"] = 2] = "RIGHTEL";
})(Operator || (exports.Operator = Operator = {}));
exports.OPERATORS = (_a = {},
_a[Operator.IRANCELL] = {
code: Operator.IRANCELL,
name: 'Irancell',
nameFa: 'ایرانسل',
prefixes: ['0930', '0933', '0935', '0936', '0937', '0938', '0939'],
},
_a[Operator.HAMRAH_E_AVAL] = {
code: Operator.HAMRAH_E_AVAL,
name: 'Hamrah-e-Aval',
nameFa: 'همراه اول',
prefixes: [
'0910',
'0911',
'0912',
'0913',
'0914',
'0915',
'0916',
'0917',
'0918',
'0919',
'0990',
'0991',
'0992',
'0993',
'0994',
],
},
_a[Operator.RIGHTEL] = {
code: Operator.RIGHTEL,
name: 'RighTel',
nameFa: 'رایتل',
prefixes: ['0920', '0921', '0922'],
},
_a);
/**
* Detects the mobile operator based on the first 3 digits of the phone number
* @param phoneNumber - The phone number string (can include country code, spaces, dashes, etc.)
* @returns Operator enum value (0: Irancell, 1: Hamrah-e-Aval, 2: RighTel) or null if not found
*/
function detectOperator(phoneNumber) {
if (!phoneNumber || typeof phoneNumber !== 'string') {
return null;
}
// Clean the phone number - remove all non-digit characters
var cleanNumber = phoneNumber.replace(/\D/g, '');
// Remove country code if present (98 for Iran)
var numberWithoutCountryCode = cleanNumber.startsWith('98') ? cleanNumber.slice(2) : cleanNumber;
// If the number starts with 9 (after removing country code), add 0 at the beginning
if (numberWithoutCountryCode.startsWith('9')) {
numberWithoutCountryCode = '0' + numberWithoutCountryCode;
}
// Get the first 4 digits (operator prefixes are 4 digits)
var prefix = numberWithoutCountryCode.slice(0, 4);
// Find the operator
for (var _i = 0, _a = Object.values(exports.OPERATORS); _i < _a.length; _i++) {
var operator = _a[_i];
if (operator.prefixes.includes(prefix)) {
return operator.code;
}
}
return null;
}
/**
* Gets detailed operator information based on the phone number
* @param phoneNumber - The phone number string
* @returns OperatorInfo object or null if not found
*/
function getOperatorInfo(phoneNumber) {
var operatorCode = detectOperator(phoneNumber);
return operatorCode !== null ? exports.OPERATORS[operatorCode] : null;
}
/**
* Gets operator name in Persian
* @param phoneNumber - The phone number string
* @returns Persian operator name or null if not found
*/
function getOperatorNameFa(phoneNumber) {
var info = getOperatorInfo(phoneNumber);
return (info === null || info === void 0 ? void 0 : info.nameFa) || null;
}
/**
* Gets operator name in English
* @param phoneNumber - The phone number string
* @returns English operator name or null if not found
*/
function getOperatorName(phoneNumber) {
var info = getOperatorInfo(phoneNumber);
return (info === null || info === void 0 ? void 0 : info.name) || null;
}
/**
* Validates if the phone number is a valid Iranian mobile number
* @param phoneNumber - The phone number string
* @returns boolean indicating if the number is valid
*/
function isValidIranianMobileNumber(phoneNumber) {
if (!phoneNumber || typeof phoneNumber !== 'string') {
return false;
}
var cleanNumber = phoneNumber.replace(/\D/g, '');
var numberWithoutCountryCode = cleanNumber.startsWith('98') ? cleanNumber.slice(2) : cleanNumber;
// If the number starts with 9 (after removing country code), add 0 at the beginning
if (numberWithoutCountryCode.startsWith('9')) {
numberWithoutCountryCode = '0' + numberWithoutCountryCode;
}
// Iranian mobile numbers should be 11 digits starting with 09
return numberWithoutCountryCode.length === 11 && numberWithoutCountryCode.startsWith('09');
}
//# sourceMappingURL=operator-checker.js.map