operator-checker
Version:
Iranian mobile operator detection package for Vue 3 and TypeScript
284 lines (203 loc) • 6.52 kB
Markdown
A TypeScript package for detecting Iranian mobile operators (Irancell, Hamrah-e-Aval, and RighTel) based on phone number prefixes. Perfect for Vue 3 Composition API projects.
## Features
- 🎯 **Accurate Detection**: Detects operators based on the first 4 digits of phone numbers
- 🚀 **Vue 3 Ready**: Built-in Composition API support
- 📱 **TypeScript**: Full TypeScript support with type definitions
- 🔧 **Flexible**: Can be used in any JavaScript/TypeScript project
- 🌐 **Bilingual**: Persian and English operator names
- ✅ **Validation**: Built-in Iranian mobile number validation
## Supported Operators
| Code | Operator | Persian Name | Prefixes |
| ---- | ------------- | ------------ | ---------------------------------------- |
| 0 | Irancell | ایرانسل | 0930, 0933, 0935, 0936, 0937, 0938, 0939 |
| 1 | Hamrah-e-Aval | همراه اول | 0910-0919, 0990-0994 |
| 2 | RighTel | رایتل | 0920, 0921, 0922 |
## Installation
```bash
npm install operator-checker
```
## Usage
### Basic Usage
```typescript
import { detectOperator } from 'operator-checker'
// Detect operator
const operator = detectOperator('09123456789')
console.log(operator) // 1 (Hamrah-e-Aval)
// Get operator name in Persian
import { getOperatorNameFa } from 'operator-checker'
const name = getOperatorNameFa('09351234567')
console.log(name) // "ایرانسل"
// Get operator name in English
import { getOperatorName } from 'operator-checker'
const nameEn = getOperatorName('09201234567')
console.log(nameEn) // "RighTel"
```
```vue
<template>
<div>
<input v-model="phoneNumber" placeholder="شماره موبایل" />
<div v-if="operator !== null">اپراتور: {{ operatorNameFa }} ({{ operator }})</div>
</div>
</template>
<script setup lang="ts">
import { useOperatorChecker } from 'operator-checker'
const { phoneNumber, operator, operatorNameFa, checkOperator } = useOperatorChecker()
// Auto-check when phone number changes
watch(phoneNumber, (newValue) => {
if (newValue) {
checkOperator(newValue)
}
})
</script>
```
```typescript
import {
detectOperator,
getOperatorInfo,
isValidIranianMobileNumber,
Operator,
} from 'operator-checker'
// Get detailed operator information
const info = getOperatorInfo('09123456789')
console.log(info)
// {
// code: 1,
// name: 'Hamrah-e-Aval',
// nameFa: 'همراه اول',
// prefixes: ['0910', '0911', ...]
// }
// Validate phone number format
const isValid = isValidIranianMobileNumber('09123456789')
console.log(isValid) // true
// Use operator constants
if (detectOperator('09351234567') === Operator.IRANCELL) {
console.log('This is an Irancell number')
}
```
Detects the mobile operator based on the phone number prefix.
**Parameters:**
- `phoneNumber` (string): The phone number to check
**Returns:**
- `Operator` enum value (0: Irancell, 1: Hamrah-e-Aval, 2: RighTel) or `null` if not found
#### `getOperatorInfo(phoneNumber: string): OperatorInfo | null`
Gets detailed operator information.
**Returns:**
```typescript
interface OperatorInfo {
code: Operator
name: string
nameFa: string
prefixes: string[]
}
```
Gets the operator name in English.
Gets the operator name in Persian.
Validates if the phone number is a valid Iranian mobile number.
**Options:**
```typescript
interface UseOperatorCheckerOptions {
autoValidate?: boolean // Default: true
showDetails?: boolean // Default: false
}
```
**Returns:**
```typescript
{
// Reactive state
phoneNumber: Ref<string>;
operator: Ref<Operator | null>;
operatorInfo: Ref<OperatorInfo | null>;
operatorName: Ref<string | null>;
operatorNameFa: Ref<string | null>;
isValid: Ref<boolean>;
isLoading: Ref<boolean>;
error: Ref<string | null>;
// Computed
operatorCode: ComputedRef<Operator | null>;
operatorDetails: ComputedRef<{...}>;
// Methods
checkOperator: (number: string) => void;
reset: () => void;
validateNumber: (number: string) => boolean;
// Constants
Operator: typeof Operator;
}
```
```typescript
enum Operator {
IRANCELL = 0, // ایرانسل
HAMRAH_E_AVAL = 1, // همراه اول
RIGHTEL = 2, // رایتل
}
```
```typescript
import { detectOperator } from 'operator-checker';
function PhoneInput() {
const [phoneNumber, setPhoneNumber] = useState('');
const [operator, setOperator] = useState(null);
const handleChange = (e) => {
const number = e.target.value;
setPhoneNumber(number);
setOperator(detectOperator(number));
};
return (
<div>
<input value={phoneNumber} onChange={handleChange} />
{operator !== null && <p>Operator: {operator}</p>}
</div>
);
}
```
```javascript
const { detectOperator, getOperatorNameFa } = require('operator-checker')
const operator = detectOperator('09123456789')
const name = getOperatorNameFa('09123456789')
console.log(`Operator: ${operator}, Name: ${name}`)
```
```bash
git clone https://github.com/yourusername/operator-checker.git
cd operator-checker
npm install
```
```bash
npm run build
```
```bash
npm run dev
```
```bash
npm run type-check
```
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Support
If you have any questions or need help, please open an issue on GitHub.
---
**نکته**: این پکیج برای تشخیص اپراتورهای موبایل ایران طراحی شده است و با شمارههای سایر کشورها کار نمیکند.