The ShortNumberInfo class provides utilities for working with short numbers including emergency services, toll-free numbers, premium rate services, and other special service codes. This is essential for applications that need to handle emergency numbers, service hotlines, or validate short codes.
Utility class for short number operations. Uses singleton pattern - access via getInstance().
/**
* Utility class for short number operations and validation
* @singleton Access via ShortNumberInfo.getInstance()
*/
class ShortNumberInfo {
/**
* Get the singleton instance of ShortNumberInfo
* @returns {ShortNumberInfo} The singleton instance
*/
static getInstance(): ShortNumberInfo;
}Identify and validate emergency service numbers for different regions.
/**
* Test whether the short number can be used to connect to emergency services when dialed from the given region.
* @param {string} number - The short number to test (as string)
* @param {string} regionCode - ISO 3166-1 alpha-2 region code where the call is placed
* @returns {boolean} True if the number connects to emergency services
*/
connectsToEmergencyNumber(number: string, regionCode: string): boolean;
/**
* Test whether a short number is an emergency number that matches exactly.
* @param {string} number - The short number to test (as string)
* @param {string} regionCode - ISO 3166-1 alpha-2 region code where the call is placed
* @returns {boolean} True if the number is an exact emergency number match
*/
isEmergencyNumber(number: string, regionCode: string): boolean;Usage Examples:
const ShortNumberInfo = require('google-libphonenumber').ShortNumberInfo;
const shortInfo = ShortNumberInfo.getInstance();
// Emergency number detection
console.log(shortInfo.connectsToEmergencyNumber('911', 'US')); // => true
console.log(shortInfo.connectsToEmergencyNumber('112', 'US')); // => true
console.log(shortInfo.connectsToEmergencyNumber('999', 'GB')); // => true
console.log(shortInfo.connectsToEmergencyNumber('911', 'GB')); // => false
console.log(shortInfo.isEmergencyNumber('911', 'US')); // => true
console.log(shortInfo.isEmergencyNumber('9111', 'US')); // => false (not exact)Validate whether short numbers are properly formatted and valid for specific regions.
/**
* Test whether a short number is a possible number.
* @param {PhoneNumber} number - The parsed short number to validate
* @returns {boolean} True if the short number is possible
*/
isPossibleShortNumber(number: PhoneNumber): boolean;
/**
* Test whether a short number is a possible number when dialed from the given region.
* @param {PhoneNumber} number - The parsed short number to validate
* @param {string} regionDialingFrom - ISO 3166-1 alpha-2 region code where call is placed
* @returns {boolean} True if the short number is possible for the region
*/
isPossibleShortNumberForRegion(number: PhoneNumber, regionDialingFrom: string): boolean;
/**
* Test whether a short number is valid.
* @param {PhoneNumber} number - The parsed short number to validate
* @returns {boolean} True if the short number is valid
*/
isValidShortNumber(number: PhoneNumber): boolean;
/**
* Test whether a short number matches a valid pattern in a region.
* @param {PhoneNumber} number - The parsed short number to validate
* @param {string} regionDialingFrom - ISO 3166-1 alpha-2 region code where call is placed
* @returns {boolean} True if the short number is valid for the region
*/
isValidShortNumberForRegion(number: PhoneNumber, regionDialingFrom: string): boolean;Usage Examples:
const { PhoneNumberUtil, ShortNumberInfo } = require('google-libphonenumber');
const phoneUtil = PhoneNumberUtil.getInstance();
const shortInfo = ShortNumberInfo.getInstance();
// Parse and validate short numbers
const shortNumber = phoneUtil.parse('123456', 'FR');
console.log(shortInfo.isPossibleShortNumber(shortNumber)); // => true
console.log(shortInfo.isPossibleShortNumberForRegion(shortNumber, 'FR')); // => true
console.log(shortInfo.isValidShortNumber(shortNumber)); // => depends on FR patterns
console.log(shortInfo.isValidShortNumberForRegion(shortNumber, 'FR')); // => depends on FR patternsDetermine the cost category and service type of short numbers.
/**
* Get the expected cost category for a short number in a region.
* @param {PhoneNumber} number - The parsed short number
* @param {string} regionDialingFrom - ISO 3166-1 alpha-2 region code where call is placed
* @returns {ShortNumberCost} The cost category (TOLL_FREE, STANDARD_RATE, PREMIUM_RATE, UNKNOWN_COST)
*/
getExpectedCostForRegion(number: PhoneNumber, regionDialingFrom: string): ShortNumberCost;
/**
* Get the expected cost category for a short number across all possible regions.
* @param {PhoneNumber} number - The parsed short number
* @returns {ShortNumberCost} The cost category (TOLL_FREE, STANDARD_RATE, PREMIUM_RATE, UNKNOWN_COST)
*/
getExpectedCost(number: PhoneNumber): ShortNumberCost;
/**
* Check if a short number is carrier-specific in a region.
* @param {PhoneNumber} number - The parsed short number
* @param {string} regionDialingFrom - ISO 3166-1 alpha-2 region code where call is placed
* @returns {boolean} True if the number is carrier-specific
*/
isCarrierSpecificForRegion(number: PhoneNumber, regionDialingFrom: string): boolean;
/**
* Check if a short number is carrier-specific.
* @param {PhoneNumber} number - The parsed short number
* @returns {boolean} True if the number is carrier-specific
*/
isCarrierSpecific(number: PhoneNumber): boolean;Get example short numbers for testing and documentation purposes.
/**
* Get an example short number for a region.
* @param {string} regionCode - ISO 3166-1 alpha-2 region code
* @returns {string} Example short number for the region, or empty string if none available
*/
getExampleShortNumber(regionCode: string): string;
/**
* Get an example short number for a specific cost category in a region.
* @param {string} regionCode - ISO 3166-1 alpha-2 region code
* @param {ShortNumberCost} cost - The cost category to get an example for
* @returns {string} Example short number for the cost category, or empty string if none available
*/
getExampleShortNumberForCost(regionCode: string, cost: ShortNumberCost): string;const { ShortNumberInfo } = require('google-libphonenumber');
function isEmergencyCall(numberString, userRegion) {
const shortInfo = ShortNumberInfo.getInstance();
// Check if it's an emergency number
if (shortInfo.connectsToEmergencyNumber(numberString, userRegion)) {
console.log(`${numberString} is an emergency number in ${userRegion}`);
return true;
}
return false;
}
// Test various emergency numbers
isEmergencyCall('911', 'US'); // => true
isEmergencyCall('112', 'DE'); // => true
isEmergencyCall('999', 'GB'); // => true
isEmergencyCall('000', 'AU'); // => trueconst { PhoneNumberUtil, ShortNumberInfo } = require('google-libphonenumber');
function analyzeShortNumber(numberString, region) {
const phoneUtil = PhoneNumberUtil.getInstance();
const shortInfo = ShortNumberInfo.getInstance();
try {
const number = phoneUtil.parse(numberString, region);
if (shortInfo.isValidShortNumberForRegion(number, region)) {
const cost = shortInfo.getExpectedCostForRegion(number, region);
const isCarrierSpecific = shortInfo.isCarrierSpecificForRegion(number, region);
console.log(`Number: ${numberString}`);
console.log(`Cost: ${cost}`);
console.log(`Carrier Specific: ${isCarrierSpecific}`);
return { valid: true, cost, isCarrierSpecific };
} else {
console.log(`${numberString} is not a valid short number in ${region}`);
return { valid: false };
}
} catch (error) {
console.error(`Error parsing ${numberString}:`, error.message);
return { valid: false, error: error.message };
}
}
// Analyze different short numbers
analyzeShortNumber('411', 'US'); // Directory assistance
analyzeShortNumber('611', 'US'); // Repair service
analyzeShortNumber('800', 'US'); // May not be valid as short numberThe ShortNumberCost enum defines the following cost categories:
enum ShortNumberCost {
/** Free to call */
TOLL_FREE = 0,
/** Standard rate applies */
STANDARD_RATE = 1,
/** Premium rate applies (higher cost) */
PREMIUM_RATE = 2,
/** Cost category unknown */
UNKNOWN_COST = 3
}Different regions have different emergency numbers:
Common service short numbers vary by region:
Some short numbers only work on specific mobile networks or carriers. Use isCarrierSpecific() to identify these numbers.