Skip to content

Commit 005d047

Browse files
meabedrenovate[bot]claudeDev.me Team
authored
feat: Comprehensive improvements with tiny-lru caching (#371)
* chore: update release * chore: update pkgs * chore: update pkgs * chore(deps): update dependency @types/node to v22.13.2 (#269) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency prettier to v3.5.1 (#270) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.13.4 (#271) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.34.7 (#272) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore: update pkgs * fix(deps): update dependency libphonenumber-js to v1.11.20 (#277) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.34.8 (#278) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix(deps): update dependency bson to v6.10.3 (#279) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.13.5 (#280) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency prettier to v3.5.2 (#281) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency ts-jest to v29.2.6 (#282) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix(deps): update dependency libphonenumber-js to v1.12.4 (#283) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#284) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#285) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency prettier to v3.5.3 (#286) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.13.9 (#287) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix(deps): update dependency libphonenumber-js to v1.12.5 (#288) * chore(deps): update all non-major dependencies (#289) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency shelljs to ^0.9.0 (#290) * fix(deps): update dependency libphonenumber-js to v1.12.6 (#291) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency shelljs to v0.9.2 (#292) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.36.0 (#293) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore: update packages * chore(deps): update dependency @types/node to v22.13.11 (#294) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.37.0 (#295) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#296) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.13.14 (#297) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.38.0 (#298) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency ts-jest to v29.3.1 (#299) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#300) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.14.0 (#301) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency typescript to v5.8.3 (#302) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.14.1 (#303) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.40.0 (#304) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency ts-jest to v29.3.2 (#305) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix(deps): update dependency libphonenumber-js to v1.12.7 (#306) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @release-it/conventional-changelog to v10.0.1 (#307) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.0 (#310) * chore(deps): update dependency @types/node to v22.15.2 (#311) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#312) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.9 (#313) * chore(deps): update all non-major dependencies (#314) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.14 (#315) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix(deps): update all non-major dependencies (#316) * chore(deps): update dependency @types/node to v22.15.16 (#317) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#318) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency shelljs to ^0.10.0 (#319) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency release-it to v19 (#309) * chore(deps): update dependency @types/node to v22.15.18 (#320) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency ts-jest to v29.3.3 (#321) * chore(deps): update dependency ts-jest to v29.3.4 (#322) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.41.0 (#323) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.19 (#324) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.21 (#325) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.41.1 (#326) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.22 (#327) * chore(deps): update dependency @types/node to v22.15.23 (#328) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.24 (#329) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.26 (#330) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#331) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.29 (#332) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix(deps): update dependency bson to v6.10.4 (#333) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix(deps): update dependency libphonenumber-js to v1.12.9 (#334) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#335) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.31 (#336) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.43.0 (#338) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency ts-jest to v29.4.0 (#339) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.32 (#340) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.44.0 (#341) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency prettier to v3.6.0 (#342) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.33 (#343) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency prettier to v3.6.1 (#344) * chore(deps): update dependency rollup to v4.44.1 (#345) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency prettier to v3.6.2 (#346) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.15.34 (#347) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#348) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update jest monorepo to v30 (#337) * chore(deps): update dependency jest to v30.0.4 (#349) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.44.2 (#350) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.16.2 (#351) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.16.3 (#352) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.45.0 (#353) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix(deps): update dependency libphonenumber-js to v1.12.10 (#354) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#355) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency release-it to v19.0.4 (#356) * chore(deps): update dependency @types/node to v22.16.5 (#357) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency jest to v30.0.5 (#358) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.45.3 (#359) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.46.0 (#360) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.46.1 (#361) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update all non-major dependencies (#362) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency typescript to v5.9.2 (#363) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency ts-jest to v29.4.1 (#364) * chore(deps): update dependency @types/node to v22.17.1 (#365) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update actions/checkout action to v5 (#366) * fix(deps): update dependency libphonenumber-js to v1.12.11 (#367) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix(deps): update dependency libphonenumber-js to v1.12.12 (#368) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency @types/node to v22.17.2 (#369) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency rollup to v4.46.3 (#370) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * feat: Implement comprehensive improvements to phone-number-validator-js Major improvements include: - Enhanced error handling with proper logging in development mode - Integrated high-performance tiny-lru cache with configurable size limits - Eliminated code duplication by creating unified helper functions - Added comprehensive input validation and null checks - Improved TypeScript type safety with explicit return types - Added 27 new comprehensive test cases covering edge cases and performance - New API functions: clearCache(), getCacheSize(), setCacheSize() Performance improvements: - O(1) cache operations for get, set, and delete - LRU cache prevents repeated file reads - Performance tests show <1ms per lookup after initial cache - Configurable cache size to manage memory in long-running processes All changes are backward compatible with no breaking changes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Release 1.4.0-develop.0 * docs: Add comprehensive documentation for new features - Add detailed API.md with complete function reference - Update README with cache management examples and TypeScript usage - Add performance section highlighting tiny-lru benefits - Update CHANGELOG for v1.4.0 release - Add correct licensing URL (https://dev.me/license/phone-number-validator) - Document all new cache management functions - Add memory management best practices - Include locale information and fallback behavior 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * chore: update --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Claude <[email protected]> Co-authored-by: Dev.me Team <[email protected]>
1 parent 728b478 commit 005d047

File tree

11 files changed

+3142
-2426
lines changed

11 files changed

+3142
-2426
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
push:
66
paths:
77
- 'src/**'
8+
- '__tests__/**'
89
- 'package.json'
910
- 'yarn.lock'
1011
- 'release.config.js'
@@ -36,10 +37,9 @@ jobs:
3637
steps:
3738
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
3839

39-
- uses: actions/checkout@v4
40+
- uses: actions/checkout@v5
4041
with:
4142
fetch-depth: 30
42-
persist-credentials: false
4343

4444
- uses: FranzDiebold/github-env-vars-action@v2
4545

@@ -59,4 +59,7 @@ jobs:
5959
- name: Release
6060
if: github.ref == 'refs/heads/develop'
6161
run: |
62+
git config --global user.email "[email protected]"
63+
git config --global user.name "Dev.me Team"
64+
npm set //registry.npmjs.org/:_authToken $NPM_TOKEN
6265
node release.mjs

.github/workflows/release.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ jobs:
3030
steps:
3131
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
3232

33-
- uses: actions/checkout@v4
33+
- uses: actions/checkout@v5
3434
with:
3535
fetch-depth: 30
36-
persist-credentials: false
3736

3837
- uses: FranzDiebold/github-env-vars-action@v2
3938

@@ -52,4 +51,7 @@ jobs:
5251
5352
- name: Release
5453
run: |
54+
git config --global user.email "[email protected]"
55+
git config --global user.name "Dev.me Team"
56+
npm set //registry.npmjs.org/:_authToken $NPM_TOKEN
5557
node release.mjs

API.md

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
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.

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
# Changelog
22

3+
## v1.4.0 (Unreleased)
4+
### Features
5+
- Add high-performance LRU caching using tiny-lru library
6+
- Add cache management API: `clearCache()`, `getCacheSize()`, `setCacheSize()`
7+
- Add comprehensive input validation and error handling
8+
- Improve TypeScript type safety with explicit return types
9+
10+
### Improvements
11+
- Refactor code to eliminate duplication between geocoder and carrier functions
12+
- Optimize memory usage with configurable cache limits
13+
- Add 27 new comprehensive test cases
14+
- Enhance error logging in development mode
15+
- Performance improvements: <1ms lookups after initial cache
16+
17+
### Documentation
18+
- Add comprehensive API documentation
19+
- Add performance and memory management guides
20+
- Update README with cache management examples
21+
322
## v1.3.0
423
- Update dependencies
524

0 commit comments

Comments
 (0)