ngxsmk-tel-input
Version:
Angular international telephone input (intl-tel-input UI + libphonenumber-js validation). ControlValueAccessor. SSR-safe.
112 lines • 4.18 kB
TypeScript
import { type CountryCode } from 'libphonenumber-js';
import * as i0 from "@angular/core";
/**
* Result of phone number parsing.
*/
export interface ParseResult {
/** E.164 formatted phone number (e.g., +14155550123) or null if invalid */
e164: string | null;
/** National format phone number or null if invalid */
national: string | null;
/** Whether the phone number is valid */
isValid: boolean;
}
/**
* Enhanced parse result with invalid country code detection.
*/
export interface ParseWithInvalidResult extends ParseResult {
/** Whether the input appears to be an invalid international number */
isInvalidInternational: boolean;
}
/**
* Service for parsing and validating phone numbers using libphonenumber-js.
* Provides caching for improved performance and enhanced validation features.
*
* @example
* ```typescript
* constructor(private telService: NgxsmkTelInputService) {}
*
* validatePhone(input: string, country: CountryCode) {
* const result = this.telService.parse(input, country);
* return result.isValid;
* }
* ```
*/
export declare class NgxsmkTelInputService {
private parseCache;
private parseWithInvalidCache;
private validationCache;
private readonly CACHE_SIZE_LIMIT;
/**
* Parses a phone number string and returns formatted results.
* Results are cached for performance.
*
* @param input - The phone number string to parse (can include formatting)
* @param iso2 - ISO 3166-1 alpha-2 country code (e.g., 'US', 'GB')
* @returns ParseResult with E.164 format, national format, and validity status
*
* @example
* ```typescript
* const result = service.parse('202-555-1234', 'US');
* // result.e164 = '+12025551234'
* // result.national = '(202) 555-1234'
* // result.isValid = true
* ```
*/
parse(input: string, iso2: CountryCode): ParseResult;
/**
* Validates whether a phone number string is valid for the given country.
* Results are cached for performance.
*
* @param input - The phone number string to validate
* @param iso2 - ISO 3166-1 alpha-2 country code
* @returns true if the phone number is valid, false otherwise
*
* @example
* ```typescript
* const isValid = service.isValid('202-555-1234', 'US'); // true
* const isInvalid = service.isValid('123', 'US'); // false
* ```
*/
isValid(input: string, iso2: CountryCode): boolean;
/**
* Sets a value in the cache, implementing LRU eviction when cache is full.
* @param cache - The cache Map to update
* @param key - The cache key
* @param value - The value to cache
*/
private setCacheValue;
/**
* Clears all caches. Useful for memory management or testing.
*/
clearCache(): void;
/**
* Checks if the input appears to be an international number with an invalid country code.
* This helps detect cases like "1123456789" where "11" is not a valid country code.
*
* @param input - The phone number string to check
* @returns true if the input appears to be an invalid international number
* @private
*/
private isInvalidInternationalNumber;
/**
* Enhanced parse method that detects invalid international numbers.
* This is useful for providing better error messages to users.
* Results are cached for performance.
*
* @param input - The phone number string to parse
* @param iso2 - ISO 3166-1 alpha-2 country code
* @returns ParseWithInvalidResult with additional invalid country code detection
*
* @example
* ```typescript
* const result = service.parseWithInvalidDetection('1123456789', 'US');
* // result.isInvalidInternational = true (because "11" is not a valid country code)
* // result.isValid = false
* ```
*/
parseWithInvalidDetection(input: string, iso2: CountryCode): ParseWithInvalidResult;
static ɵfac: i0.ɵɵFactoryDeclaration<NgxsmkTelInputService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<NgxsmkTelInputService>;
}
//# sourceMappingURL=ngxsmk-tel-input.service.d.ts.map