or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

examples-formatting.mdfinding.mdindex.mdnumber-from.mdparsing.mdregion-utilities.md
tile.json

finding.mddocs/

Phone Number Finding

Text scanning functionality to locate and extract phone numbers from unstructured text with configurable leniency settings, region preferences, and performance controls for processing large text blocks.

Capabilities

Find Numbers

Scans text to find and extract phone numbers, returning detailed match information including positions and parsed number objects.

/**
 * Find phone numbers in text.
 * 
 * If the text is expected to have phone numbers for a certain region code,
 * the option defaultRegionCode can be set. Phone numbers without the
 * international prefix + will then be found too.
 * 
 * Leniency can be specified using the leniency option, in which case
 * maxTries needs to be set too.
 * 
 * @param text Text to search for phone numbers
 * @param options Configuration options for the search
 * @returns Array of PhoneNumberMatch objects
 */
function findNumbers(
  text: string,
  options?: FindNumbersOptions
): PhoneNumberMatch[];

interface FindNumbersOptions {
  /** A default region code, to find local (non-e164) formatted phone numbers */
  defaultRegionCode?: string;
  
  /** Leniency options for validation */
  leniency?: FindNumbersLeniency;
  
  /** The maximum number of invalid numbers to try before giving up on the text */
  maxTries?: number;
}

type FindNumbersLeniency = 
  | 'possible'  // Phone numbers accepted are possible, but not necessarily valid
  | 'valid';    // Phone numbers accepted are possible and valid

Usage Examples:

import { findNumbers } from "awesome-phonenumber";

// Basic phone number finding
const text = "Call me at 0707123456 or email me at user@example.com";
const matches = findNumbers(text, { defaultRegionCode: "SE" });

console.log(matches.length); // 1
console.log(matches[0].text); // "0707123456"
console.log(matches[0].start); // 11
console.log(matches[0].end); // 21

// Find international numbers without region code
const intlText = "US: +1-555-123-4567, UK: +44-20-7946-0958";
const intlMatches = findNumbers(intlText);
console.log(intlMatches.length); // 2

// Using strict validation
const strictMatches = findNumbers("Maybe call 555-HELP or 0707123456", {
  defaultRegionCode: "SE",
  leniency: "valid",
  maxTries: 10
});

// Process found numbers
strictMatches.forEach(match => {
  if (match.phoneNumber.valid) {
    console.log(`Found: ${match.phoneNumber.number.international}`);
    console.log(`Type: ${match.phoneNumber.type}`);
  }
});

Result Types

PhoneNumberMatch

Contains information about a phone number found in text.

interface PhoneNumberMatch {
  /** The raw string found in the text */
  text: string;
  
  /** The parsed phone number object */
  phoneNumber: ParsedPhoneNumber;
  
  /** Start offset of the found number in the original text */
  start: number;
  
  /** End offset of the found number in the original text */
  end: number;
}

FindNumbersLeniency

Controls how strict the validation should be when finding numbers.

type FindNumbersLeniency = 
  /** Phone numbers accepted are possible, but not necessarily valid */
  | 'possible'
  /** Phone numbers accepted are possible and valid */
  | 'valid';

Advanced Usage Patterns

Processing Large Text Blocks

import { findNumbers } from "awesome-phonenumber";

function extractPhoneNumbers(largeText: string, regionCode: string) {
  const matches = findNumbers(largeText, {
    defaultRegionCode: regionCode,
    leniency: "valid",
    maxTries: 100  // Prevent infinite loops on malformed text
  });
  
  return matches
    .filter(match => match.phoneNumber.valid)
    .map(match => ({
      number: match.phoneNumber.number.e164,
      type: match.phoneNumber.type,
      position: { start: match.start, end: match.end },
      originalText: match.text
    }));
}

// Example usage
const text = `
  Customer contacts:
  - John: 0707123456
  - Jane: +1-555-987-6543  
  - Bob: 020-8123-4567
`;

const found = extractPhoneNumbers(text, "SE");
console.log(found);

Multi-Region Text Processing

import { findNumbers } from "awesome-phonenumber";

function findNumbersMultiRegion(text: string, regions: string[]) {
  const allMatches = new Map<string, any[]>();
  
  // Try each region to capture local format numbers
  regions.forEach(region => {
    const matches = findNumbers(text, { 
      defaultRegionCode: region,
      leniency: "possible"
    });
    allMatches.set(region, matches);
  });
  
  // Also find international numbers
  const intlMatches = findNumbers(text);
  allMatches.set("international", intlMatches);
  
  return allMatches;
}

const multiRegionText = "US: 555-1234, UK: 020 7946 0958, SE: 070-123 45 67";
const results = findNumbersMultiRegion(multiRegionText, ["US", "GB", "SE"]);

Performance Considerations

When processing large amounts of text or implementing real-time search:

  • Use maxTries to prevent performance issues with malformed text
  • Consider leniency: "valid" for higher precision with better performance
  • Process text in chunks for very large documents
  • Cache region-specific configurations for repeated operations