|
| 1 | +# API Documentation |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | +- [Core Functions](#core-functions) |
| 5 | + - [geocoder](#geocoder) |
| 6 | + - [carrier](#carrier) |
| 7 | + - [timezones](#timezones) |
| 8 | +- [Cache Management](#cache-management) |
| 9 | + - [clearCache](#clearcache) |
| 10 | + - [getCacheSize](#getcachesize) |
| 11 | + - [setCacheSize](#setcachesize) |
| 12 | +- [Type Definitions](#type-definitions) |
| 13 | +- [Locales](#locales) |
| 14 | + |
| 15 | +## Core Functions |
| 16 | + |
| 17 | +### geocoder |
| 18 | + |
| 19 | +Provides geographical information related to a phone number. |
| 20 | + |
| 21 | +```typescript |
| 22 | +geocoder( |
| 23 | + phonenumber: PhoneNumber | undefined, |
| 24 | + locale?: GeocoderLocale |
| 25 | +): string | null |
| 26 | +``` |
| 27 | + |
| 28 | +#### Parameters |
| 29 | +- `phonenumber` - A parsed phone number object from `libphonenumber-js` |
| 30 | +- `locale` - Optional locale for localized location names (default: `'en'`) |
| 31 | + |
| 32 | +#### Returns |
| 33 | +- Location name as string if found |
| 34 | +- `null` if no location data available or invalid input |
| 35 | + |
| 36 | +#### Example |
| 37 | +```javascript |
| 38 | +import { geocoder, parsePhoneNumberFromString } from '@devmehq/phone-number-validator-js' |
| 39 | + |
| 40 | +const phoneNumber = parsePhoneNumberFromString('+41431234567') |
| 41 | +const locationEN = geocoder(phoneNumber) // "Zurich" |
| 42 | +const locationDE = geocoder(phoneNumber, 'de') // "Zürich" |
| 43 | +``` |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +### carrier |
| 48 | + |
| 49 | +Maps a phone number to its original carrier. |
| 50 | + |
| 51 | +**Note:** This method returns the original carrier assigned to the number range, not the current carrier if the number has been ported. |
| 52 | + |
| 53 | +```typescript |
| 54 | +carrier( |
| 55 | + phonenumber: PhoneNumber | undefined, |
| 56 | + locale?: CarrierLocale |
| 57 | +): string | null |
| 58 | +``` |
| 59 | + |
| 60 | +#### Parameters |
| 61 | +- `phonenumber` - A parsed phone number object from `libphonenumber-js` |
| 62 | +- `locale` - Optional locale for localized carrier names (default: `'en'`) |
| 63 | + |
| 64 | +#### Returns |
| 65 | +- Carrier name as string if found |
| 66 | +- `null` if no carrier data available (e.g., landline numbers) or invalid input |
| 67 | + |
| 68 | +#### Example |
| 69 | +```javascript |
| 70 | +import { carrier, parsePhoneNumberFromString } from '@devmehq/phone-number-validator-js' |
| 71 | + |
| 72 | +const phoneNumber = parsePhoneNumberFromString('+8619912345678') |
| 73 | +const carrierEN = carrier(phoneNumber) // "China Telecom" |
| 74 | +const carrierZH = carrier(phoneNumber, 'zh') // "中国电信" |
| 75 | +``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +### timezones |
| 80 | + |
| 81 | +Provides all timezones associated with a phone number. |
| 82 | + |
| 83 | +```typescript |
| 84 | +timezones( |
| 85 | + phonenumber: PhoneNumber | undefined |
| 86 | +): string[] | null |
| 87 | +``` |
| 88 | + |
| 89 | +#### Parameters |
| 90 | +- `phonenumber` - A parsed phone number object from `libphonenumber-js` |
| 91 | + |
| 92 | +#### Returns |
| 93 | +- Array of timezone identifiers (e.g., `['America/New_York']`) |
| 94 | +- `null` if no timezone data available or invalid input |
| 95 | + |
| 96 | +#### Example |
| 97 | +```javascript |
| 98 | +import { timezones, parsePhoneNumberFromString } from '@devmehq/phone-number-validator-js' |
| 99 | + |
| 100 | +const phoneNumber = parsePhoneNumberFromString('+12124567890') |
| 101 | +const tzs = timezones(phoneNumber) // ['America/New_York'] |
| 102 | +``` |
| 103 | + |
| 104 | +## Cache Management |
| 105 | + |
| 106 | +The library uses an LRU (Least Recently Used) cache to optimize performance. The cache automatically evicts the least recently used entries when it reaches its size limit. |
| 107 | + |
| 108 | +### clearCache |
| 109 | + |
| 110 | +Clears all cached data. Useful for memory management in long-running processes. |
| 111 | + |
| 112 | +```typescript |
| 113 | +clearCache(): void |
| 114 | +``` |
| 115 | + |
| 116 | +#### Example |
| 117 | +```javascript |
| 118 | +import { clearCache } from '@devmehq/phone-number-validator-js' |
| 119 | + |
| 120 | +// Clear all cached data |
| 121 | +clearCache() |
| 122 | +``` |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +### getCacheSize |
| 127 | + |
| 128 | +Returns the current number of items in the cache. |
| 129 | + |
| 130 | +```typescript |
| 131 | +getCacheSize(): number |
| 132 | +``` |
| 133 | + |
| 134 | +#### Returns |
| 135 | +- Number of cached entries |
| 136 | + |
| 137 | +#### Example |
| 138 | +```javascript |
| 139 | +import { getCacheSize } from '@devmehq/phone-number-validator-js' |
| 140 | + |
| 141 | +const size = getCacheSize() |
| 142 | +console.log(`Current cache size: ${size}`) |
| 143 | +``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +### setCacheSize |
| 148 | + |
| 149 | +Sets the maximum cache size. When the cache reaches this limit, the least recently used entries are evicted. |
| 150 | + |
| 151 | +```typescript |
| 152 | +setCacheSize(size: number): void |
| 153 | +``` |
| 154 | + |
| 155 | +#### Parameters |
| 156 | +- `size` - Maximum number of entries to cache |
| 157 | + |
| 158 | +#### Example |
| 159 | +```javascript |
| 160 | +import { setCacheSize } from '@devmehq/phone-number-validator-js' |
| 161 | + |
| 162 | +// Limit cache to 50 entries |
| 163 | +setCacheSize(50) |
| 164 | + |
| 165 | +// Increase cache for better performance (uses more memory) |
| 166 | +setCacheSize(200) |
| 167 | + |
| 168 | +// Reduce cache for lower memory usage |
| 169 | +setCacheSize(10) |
| 170 | +``` |
| 171 | + |
| 172 | +## Type Definitions |
| 173 | + |
| 174 | +### PhoneNumber |
| 175 | + |
| 176 | +The `PhoneNumber` type is exported from `libphonenumber-js`. It represents a parsed phone number with various properties: |
| 177 | + |
| 178 | +```typescript |
| 179 | +interface PhoneNumber { |
| 180 | + country?: string |
| 181 | + countryCallingCode: string |
| 182 | + nationalNumber: string |
| 183 | + number: string |
| 184 | + // ... other properties |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +### GeocoderLocale |
| 189 | + |
| 190 | +Supported locales for geocoding: |
| 191 | + |
| 192 | +```typescript |
| 193 | +type GeocoderLocale = 'ar' | 'be' | 'bg' | 'bs' | 'de' | 'el' | 'en' | |
| 194 | + 'es' | 'fa' | 'fi' | 'fr' | 'hr' | 'hu' | 'hy' | 'id' | 'it' | 'iw' | |
| 195 | + 'ja' | 'ko' | 'nl' | 'pl' | 'pt' | 'ro' | 'ru' | 'sq' | 'sr' | 'sv' | |
| 196 | + 'th' | 'tr' | 'uk' | 'vi' | 'zh' | 'zh_Hant' |
| 197 | +``` |
| 198 | +
|
| 199 | +### CarrierLocale |
| 200 | +
|
| 201 | +Supported locales for carrier information: |
| 202 | +
|
| 203 | +```typescript |
| 204 | +type CarrierLocale = 'ar' | 'be' | 'en' | 'fa' | 'ko' | 'ru' | 'uk' | |
| 205 | + 'zh' | 'zh_Hant' |
| 206 | +``` |
| 207 | +
|
| 208 | +## Locales |
| 209 | +
|
| 210 | +### Locale Fallback |
| 211 | +
|
| 212 | +When a requested locale is not available for a specific phone number, the library automatically falls back to English (`'en'`). |
| 213 | +
|
| 214 | +### Available Locales by Region |
| 215 | +
|
| 216 | +#### Geocoding Locales |
| 217 | +- **Arabic** (`ar`): Middle East regions |
| 218 | +- **Chinese Simplified** (`zh`): Mainland China |
| 219 | +- **Chinese Traditional** (`zh_Hant`): Hong Kong, Taiwan |
| 220 | +- **English** (`en`): Default, worldwide coverage |
| 221 | +- **German** (`de`): Germany, Austria, Switzerland |
| 222 | +- **Spanish** (`es`): Spain, Latin America |
| 223 | +- **French** (`fr`): France, French-speaking regions |
| 224 | +- And many more... |
| 225 | +
|
| 226 | +#### Carrier Locales |
| 227 | +- **Arabic** (`ar`): Middle East carriers |
| 228 | +- **Chinese** (`zh`): Chinese carriers |
| 229 | +- **English** (`en`): Default, worldwide carriers |
| 230 | +- **Korean** (`ko`): Korean carriers |
| 231 | +- **Russian** (`ru`): Russian carriers |
| 232 | +
|
| 233 | +## Performance Considerations |
| 234 | +
|
| 235 | +### Caching Strategy |
| 236 | +
|
| 237 | +The library uses [tiny-lru](https://www.npmjs.com/package/tiny-lru) for optimal performance: |
| 238 | +
|
| 239 | +1. **First Lookup**: Loads data from disk (slower, ~10-50ms) |
| 240 | +2. **Subsequent Lookups**: Retrieves from cache (<1ms) |
| 241 | +3. **Cache Eviction**: Automatic LRU eviction when limit reached |
| 242 | +
|
| 243 | +### Memory Management |
| 244 | +
|
| 245 | +For long-running applications: |
| 246 | +
|
| 247 | +```javascript |
| 248 | +import { setCacheSize, clearCache, getCacheSize } from '@devmehq/phone-number-validator-js' |
| 249 | + |
| 250 | +// Option 1: Limit cache size |
| 251 | +setCacheSize(50) // Balance between performance and memory |
| 252 | + |
| 253 | +// Option 2: Periodic cache clearing |
| 254 | +setInterval(() => { |
| 255 | + if (getCacheSize() > 80) { |
| 256 | + clearCache() |
| 257 | + } |
| 258 | +}, 3600000) // Check every hour |
| 259 | + |
| 260 | +// Option 3: Dynamic adjustment based on memory |
| 261 | +const checkMemory = () => { |
| 262 | + const usage = process.memoryUsage() |
| 263 | + if (usage.heapUsed > 100 * 1024 * 1024) { // 100MB |
| 264 | + setCacheSize(10) // Reduce cache |
| 265 | + } else { |
| 266 | + setCacheSize(100) // Normal cache |
| 267 | + } |
| 268 | +} |
| 269 | +``` |
| 270 | + |
| 271 | +## Error Handling |
| 272 | + |
| 273 | +All functions handle invalid inputs gracefully: |
| 274 | + |
| 275 | +```javascript |
| 276 | +import { geocoder, carrier, timezones } from '@devmehq/phone-number-validator-js' |
| 277 | + |
| 278 | +// All return null for invalid inputs |
| 279 | +geocoder(undefined) // null |
| 280 | +geocoder(null) // null |
| 281 | +carrier(undefined) // null |
| 282 | +timezones(undefined) // null |
| 283 | + |
| 284 | +// Invalid phone numbers |
| 285 | +const invalid = parsePhoneNumberFromString('invalid') |
| 286 | +geocoder(invalid) // null |
| 287 | +``` |
| 288 | + |
| 289 | +## Development vs Production |
| 290 | + |
| 291 | +In development mode (`NODE_ENV !== 'production'`), the library logs errors to help with debugging: |
| 292 | + |
| 293 | +```bash |
| 294 | +# Development mode - shows error logs |
| 295 | +yarn test |
| 296 | + |
| 297 | +# Production mode - suppresses error logs |
| 298 | +NODE_ENV=production yarn test |
| 299 | +``` |
| 300 | + |
| 301 | +## Thread Safety |
| 302 | + |
| 303 | +The library is thread-safe for read operations. However, cache management operations (`setCacheSize`, `clearCache`) should be synchronized in multi-threaded environments. |
0 commit comments