@foodhubglobal/phone

中文文档
0.1.12发布于 2026/08/10SEE LICENSE IN LICENSETypeScriptESM + CJS近 30 天下载 2210

Phone number parsing, formatting and validation utilities built on libphonenumber-js, with TypeScript types and dual CommonJS/ES module builds for React Native, React and Node.js.

原始使用文档
文档目录13 节
  1. Installation
  2. Usage in Different Environments
  3. 1. React Native App
  4. 2. React.js (Web Application)
  5. 3. Node.js (API/Server Layer)
  6. 4. Pure JavaScript Project (Browser/Vanilla JS)
  7. Modules
  8. Phone Module
  9. Quick Reference: Import Methods
  10. API Reference
  11. Phone Module Functions
  12. Contributing
  13. License

@foodhubglobal/phone

Phone number parsing, formatting and validation utilities built on libphonenumber-js, with TypeScript types and dual CommonJS/ES module builds.

Runs anywhere JavaScript does — React Native, React, Node.js and the browser. The package has no React or React Native dependency.

Installation

npm install @foodhubglobal/phone
# or
yarn add @foodhubglobal/phone

Usage in Different Environments

This library is compatible with multiple JavaScript/TypeScript environments. The phone module is a pure utility library that works in any JavaScript runtime.

1. React Native App

// App.tsx or any component file
import { 
  formatPhoneNumber, 
  validatePhoneNumber,
  formatAsYouType 
} from '@foodhubglobal/phone';

// Usage in React Native component
import React, { useState } from 'react';
import { TextInput, View, Text } from 'react-native';

function PhoneInput() {
  const [phone, setPhone] = useState('');

  return (
    <View>
      <TextInput
        value={phone}
        onChangeText={(text) => setPhone(formatAsYouType(text, 'US'))}
        placeholder="Enter phone number"
      />
      {validatePhoneNumber(phone, 'US') && (
        <Text>Formatted: {formatPhoneNumber(phone, 'US')}</Text>
      )}
    </View>
  );
}

2. React.js (Web Application)

// Component.tsx or any React component
import { 
  formatPhoneNumber, 
  validatePhoneNumber,
  formatAsYouType 
} from '@foodhubglobal/phone';

// Usage in React component
import React, { useState } from 'react';

function PhoneInput() {
  const [phone, setPhone] = useState('');

  return (
    <div>
      <input
        type="tel"
        value={phone}
        onChange={(e) => setPhone(formatAsYouType(e.target.value, 'US'))}
        placeholder="Enter phone number"
      />
      {validatePhoneNumber(phone, 'US') && (
        <p>Formatted: {formatPhoneNumber(phone, 'US')}</p>
      )}
    </div>
  );
}

3. Node.js (API/Server Layer)

CommonJS (require)

// server.js or api.js
const { 
  formatPhoneNumber, 
  validatePhoneNumber,
  parsePhone 
} = require('@foodhubglobal/phone');

// Usage in Express.js API route
const express = require('express');
const app = express();

app.post('/validate-phone', (req, res) => {
  const { phone, country } = req.body;
  
  if (validatePhoneNumber(phone, country)) {
    const formatted = formatPhoneNumber(phone, country);
    res.json({ valid: true, formatted });
  } else {
    res.status(400).json({ valid: false, error: 'Invalid phone number' });
  }
});

// Usage with detailed parsing
app.get('/parse-phone/:phone', (req, res) => {
  const result = parsePhone(req.params.phone, req.query.country);
  res.json(result);
});

ES Modules (import) - Node.js 12+ or with "type": "module"

// server.mjs or package.json has "type": "module"
import { 
  formatPhoneNumber, 
  validatePhoneNumber,
  parsePhone 
} from '@foodhubglobal/phone';

// Usage in Express.js API route (ES Modules)
import express from 'express';
const app = express();

app.post('/validate-phone', (req, res) => {
  const { phone, country } = req.body;
  
  if (validatePhoneNumber(phone, country)) {
    const formatted = formatPhoneNumber(phone, country);
    res.json({ valid: true, formatted });
  } else {
    res.status(400).json({ valid: false, error: 'Invalid phone number' });
  }
});

TypeScript in Node.js

// api.ts or server.ts
import { 
  formatPhoneNumber, 
  validatePhoneNumber,
  parsePhone,
  type CountryCode 
} from '@foodhubglobal/phone';

// Usage in Express.js API route (TypeScript)
import express, { Request, Response } from 'express';
const app = express();

app.post('/validate-phone', (req: Request, res: Response) => {
  const { phone, country }: { phone: string; country?: CountryCode } = req.body;
  
  if (validatePhoneNumber(phone, country)) {
    const formatted = formatPhoneNumber(phone, country);
    res.json({ valid: true, formatted });
  } else {
    res.status(400).json({ valid: false, error: 'Invalid phone number' });
  }
});

4. Pure JavaScript Project (Browser/Vanilla JS)

HTML with Script Tag (Browser)

<!-- Note: You'll need to bundle this first or use a CDN/bundler -->
<!DOCTYPE html>
<html>
<head>
  <title>Phone Formatter</title>
</head>
<body>
  <input type="tel" id="phoneInput" placeholder="Enter phone number">
  <button onclick="formatPhone()">Format</button>
  <p id="result"></p>

  <script type="module">
    // If using ES modules in browser
    import { formatPhoneNumber, validatePhoneNumber } from './node_modules/@foodhubglobal/phone/lib/module/index.js';
    
    window.formatPhone = function() {
      const input = document.getElementById('phoneInput');
      const result = document.getElementById('result');
      const phone = input.value;
      
      if (validatePhoneNumber(phone, 'US')) {
        result.textContent = `Formatted: ${formatPhoneNumber(phone, 'US')}`;
      } else {
        result.textContent = 'Invalid phone number';
      }
    };
  </script>
</body>
</html>

Bundled JavaScript (Webpack/Rollup/Vite)

// app.js - Works with any bundler (Webpack, Rollup, Vite, etc.)
import { 
  formatPhoneNumber, 
  validatePhoneNumber 
} from '@foodhubglobal/phone';

// Usage
function formatPhone(phoneNumber) {
  if (validatePhoneNumber(phoneNumber, 'US')) {
    return formatPhoneNumber(phoneNumber, 'US');
  }
  return 'Invalid phone number';
}

// Export for use in other files
export { formatPhone };

Node.js Script (CLI/Tooling)

// script.js - Run with: node script.js
const { formatPhoneNumber, validatePhoneNumber } = require('@foodhubglobal/phone');

// CLI usage
const phoneNumber = process.argv[2];
const country = process.argv[3] || 'US';

if (validatePhoneNumber(phoneNumber, country)) {
  console.log(`Valid! Formatted: ${formatPhoneNumber(phoneNumber, country)}`);
} else {
  console.log('Invalid phone number');
  process.exit(1);
}

Modules

Phone Module

A comprehensive phone number formatting and validation module built on top of libphonenumber-js. Provides utilities for formatting, validating, and parsing phone numbers from around the world.

Installation

The phone module is included in the main package. No additional installation required.

Usage

import { 
  formatPhoneNumber, 
  formatInternational, 
  validatePhoneNumber,
  formatAsYouType 
} from '@foodhubglobal/phone';

Examples

Format Phone Numbers

import { formatPhoneNumber, formatInternational, formatNational } from '@foodhubglobal/phone';

// E.164 format (default)
// With country code parameter (recommended for national numbers)
formatPhoneNumber('2125551234', 'US'); // => '+12125551234'

// Without country code (works when number includes country prefix)
formatPhoneNumber('+12125551234'); // => '+12125551234'
formatPhoneNumber('+442071234567'); // => '+442071234567'

// Fallback: Without country code for national number (returns original phoneNumber)
formatPhoneNumber('2125551234'); // => '2125551234' (returns original if parsing fails)

// With fallback parameter
formatPhoneNumber('invalid', undefined, 'N/A'); // => 'N/A' (returns fallback)
formatPhoneNumber('invalid'); // => 'invalid' (returns original phoneNumber)

// International format
formatInternational('2125551234', 'US'); // => '+1 212 555 1234'
formatInternational('02071234567', 'GB'); // => '+44 20 7123 4567'
formatInternational('+12125551234'); // => '+1 212 555 1234'
formatInternational('invalid', undefined, 'N/A'); // => 'N/A' (returns fallback)

// National format
formatNational('2125551234', 'US'); // => '(212) 555-1234'
formatNational('02071234567', 'GB'); // => '020 7123 4567'
formatNational('+12125551234'); // => '(212) 555-1234'
formatNational('invalid', undefined, 'N/A'); // => 'N/A' (returns fallback)

Important: For national numbers (without country code prefix like +1), always provide the countryCode parameter for accurate results.

Error Handling: All format functions now support an optional fallback parameter. If parsing fails:

  • With fallback provided: returns the fallback value
  • Without fallback: returns the original phoneNumber (for format functions) or undefined (for extract/get functions)

Validate Phone Numbers

import { validatePhoneNumber, isPossibleNumber } from '@foodhubglobal/phone';

validatePhoneNumber('+12125551234'); // => true
validatePhoneNumber('2125551234', 'US'); // => true
validatePhoneNumber('123', 'US'); // => false

isPossibleNumber('2125551234', 'US'); // => true
isPossibleNumber('123', 'US'); // => false

As-You-Type Formatting

import { formatAsYouType } from '@foodhubglobal/phone';

// With country code parameter (recommended)
formatAsYouType('212', 'US'); // => '(212)'
formatAsYouType('212555', 'US'); // => '(212) 555'
formatAsYouType('2125551234', 'US'); // => '(212) 555-1234'

// Without country code (works when input includes country prefix)
formatAsYouType('+1212'); // => '+1 212'
formatAsYouType('+442071234567'); // => '+44 20 7123 4567'

// Use in TextInput for real-time formatting
<TextInput
  value={phoneNumber}
  onChangeText={(text) => setPhoneNumber(formatAsYouType(text, 'US'))}
/>

Parse Phone Numbers

import { parsePhone } from '@foodhubglobal/phone';

// With country code parameter (recommended for national numbers)
const result1 = parsePhone('2125551234', 'US');
// => {
//   isValid: true,
//   isPossible: true,
//   country: 'US',
//   countryCallingCode: '1',
//   nationalNumber: '2125551234',
//   internationalFormat: '+1 212 555 1234',
//   nationalFormat: '(212) 555-1234',
//   e164Format: '+12125551234'
// }

// Without country code (works when number includes country prefix)
const result2 = parsePhone('+12125551234');
// => { ... same structure as above ... }

// Fallback: Without country code for national number (returns error object)
const result3 = parsePhone('2125551234');
// => {
//   isValid: false,
//   isPossible: false
// }

Get Country Information

import { 
  getCountryFromPhoneNumber, 
  getCallingCode, 
  getAvailableCountries 
} from '@foodhubglobal/phone';

getCountryFromPhoneNumber('+12125551234'); // => 'US'
getCountryFromPhoneNumber('+442071234567'); // => 'GB'
getCountryFromPhoneNumber('invalid', 'US'); // => 'US' (returns fallback)

getCallingCode('US'); // => '1'
getCallingCode('GB'); // => '44'
getCallingCode('IN'); // => '91'
getCallingCode('INVALID', 'N/A'); // => 'N/A' (returns fallback)

getAvailableCountries(); // => ['US', 'GB', 'CA', 'AU', 'IN', ...]

Extract Phone Numbers

import { extractPhoneNumber } from '@foodhubglobal/phone';

extractPhoneNumber('Call me at +12125551234'); // => '+12125551234'
extractPhoneNumber('2125551234', 'US'); // => '+12125551234'
extractPhoneNumber('No phone here', undefined, 'N/A'); // => 'N/A' (returns fallback)
extractPhoneNumber('No phone here'); // => undefined (no fallback provided)

Number Type Detection

import { 
  getNumberType, 
  isMobileNumber, 
  isFixedLineNumber 
} from '@foodhubglobal/phone';

getNumberType('+12125551234'); // => 'MOBILE' | 'FIXED_LINE' | 'VOIP' | etc.
isMobileNumber('+12125551234'); // => true | false
isFixedLineNumber('+12125551234'); // => true | false

Additional Utilities

import { 
  normalizePhoneNumber,
  formatIncomplete,
  getNationalNumber,
  getCallingCodeFromNumber,
  isSameNumber,
  isValidForCountry,
  formatRFC3966,
  format
} from '@foodhubglobal/phone';

// Normalize phone number
normalizePhoneNumber('(212) 555-1234', 'US'); // => '+12125551234'
normalizePhoneNumber('invalid', undefined, 'N/A'); // => 'N/A' (returns fallback)

// Format incomplete numbers
formatIncomplete('212', 'US'); // => '(212)'

// Get national number
getNationalNumber('+12125551234'); // => '2125551234'
getNationalNumber('invalid', undefined, 'N/A'); // => 'N/A' (returns fallback)
getNationalNumber('invalid'); // => undefined (no fallback provided)

// Get calling code from number
getCallingCodeFromNumber('+12125551234'); // => '1'
getCallingCodeFromNumber('invalid', 'N/A'); // => 'N/A' (returns fallback)
getCallingCodeFromNumber('invalid'); // => undefined (no fallback provided)

// Compare two numbers
isSameNumber('+12125551234', '(212) 555-1234', 'US'); // => true

// Validate for specific country
isValidForCountry('2125551234', 'US'); // => true

// RFC3966 format
formatRFC3966('2125551234', 'US'); // => 'tel:+1-212-555-1234'
formatRFC3966('invalid', undefined, 'N/A'); // => 'N/A' (returns fallback)

// Custom format
format('2125551234', 'E.164', 'US'); // => '+12125551234'
format('2125551234', 'INTERNATIONAL', 'US'); // => '+1 212 555 1234'
format('invalid', 'E.164', undefined, 'N/A'); // => 'N/A' (returns fallback)

Supported Countries

The phone module supports all countries supported by libphonenumber-js. Use getAvailableCountries() to get the full list of supported country codes.

Best Practices

When to provide countryCode parameter:

  • ✅ Always provide when working with national numbers (e.g., '2125551234')
  • ✅ Optional when the number includes a country code prefix (e.g., '+12125551234')
  • ✅ Recommended for all formatting and validation functions to ensure accuracy

Example:

// ✅ Good: National number with country code
formatPhoneNumber('2125551234', 'US'); // => '+12125551234'

// ✅ Good: International format with prefix
formatPhoneNumber('+12125551234'); // => '+12125551234'

// ⚠️ May fail: National number without country code
formatPhoneNumber('2125551234'); // => undefined (ambiguous)

Troubleshooting: Validation works in Staging but not Production

If phone validation behaves differently in production than in staging, check the following:

  1. Pass countryCode in both environments
    National numbers (without a + prefix) require a country code. If prod uses a different config or API and omits countryCode, validation will fail. Prefer always passing it:
    validatePhoneNumber(phone, countryCode).

  2. Country code format
    Use ISO 3166-1 alpha-2 (e.g. 'US', 'GB'). The library normalizes lowercase (e.g. 'us' → 'US'). Ensure prod is not sending a different format (e.g. numeric or 3-letter).

  3. Input normalization
    The library trims whitespace and normalizes common Unicode digits to ASCII. If prod receives numbers from another system (e.g. with leading/trailing spaces or non-ASCII digits), they are normalized before validation. If you still see differences, log the exact phoneNumber and countryCode in prod and compare to staging.

  4. Dependency version
    Ensure staging and prod resolve the same version of @foodhubglobal/phone (and thus libphonenumber-js). Lockfiles and install scripts should be identical so production does not get an older build.

TypeScript Support

The module is fully typed with TypeScript. All functions include comprehensive type definitions and JSDoc comments for better IDE support, including detailed parameter descriptions and fallback behavior documentation.

import type { CountryCode, NumberFormat, NumberType } from '@foodhubglobal/phone';

Quick Reference: Import Methods

Depending on your environment, use the appropriate import method:

EnvironmentCommonJS (require)ES Modules (import)
Node.js✅ require('@foodhubglobal/phone')✅ import from '@foodhubglobal/phone'
React.js✅ require('@foodhubglobal/phone') (ESM preferred)✅ import from '@foodhubglobal/phone'
React Native✅ require('@foodhubglobal/phone') (ESM preferred)✅ import from '@foodhubglobal/phone'
Pure JavaScript✅ require(...)✅ import from ...

Note: These utilities are pure JavaScript functions. The package has no React or React Native dependency, so it can be used in any JavaScript environment. In React.js and React Native projects, bundlers such as Metro and common React build setups typically support both CommonJS (require) and ES modules (import); ES modules are recommended for new code, but CommonJS remains fully functional in these environments.

IDE Features:

  • Auto-completion for all functions
  • Inline documentation with JSDoc comments
  • Type checking for parameters and return values
  • Examples in tooltips

API Reference

Phone Module Functions

FunctionDescriptionParametersReturns
formatPhoneNumber(phone, country?, fallback?)Format in E.164 formatphone: string, country?: CountryCode, fallback?: stringstring | undefined
formatInternational(phone, country?, fallback?)Format in international formatphone: string, country?: CountryCode, fallback?: stringstring | undefined
formatNational(phone, country?, fallback?)Format in national formatphone: string, country?: CountryCode, fallback?: stringstring | undefined
validatePhoneNumber(phone, country?)Validate phone numberphone: string, country?: CountryCodeboolean
isPossibleNumber(phone, country?)Check if number is possiblephone: string, country?: CountryCodeboolean
formatAsYouType(phone, country?)Format as user typesphone: string, country?: CountryCodestring
parsePhone(phone, country?)Parse and get detailed infophone: string, country?: CountryCodeobject
getCountryFromPhoneNumber(phone, fallback?)Extract country from numberphone: string (must include country prefix), fallback?: CountryCodeCountryCode | undefined
getCallingCode(country, fallback?)Get calling code for countrycountry: CountryCode, fallback?: stringstring | undefined
getAvailableCountries()Get all supported countriesNoneCountryCode[]
extractPhoneNumber(text, country?, fallback?)Extract number from texttext: string, country?: CountryCode, fallback?: stringstring | undefined
getNumberType(phone, country?)Get number typephone: string, country?: CountryCodeNumberType | undefined
isMobileNumber(phone, country?)Check if mobile numberphone: string, country?: CountryCodeboolean
isFixedLineNumber(phone, country?)Check if fixed-line numberphone: string, country?: CountryCodeboolean
normalizePhoneNumber(phone, country?, fallback?)Normalize to E.164phone: string, country?: CountryCode, fallback?: stringstring | undefined
formatIncomplete(phone, country?)Format incomplete numberphone: string, country?: CountryCodestring
getNationalNumber(phone, country?, fallback?)Get national numberphone: string, country?: CountryCode, fallback?: stringstring | undefined
getCallingCodeFromNumber(phone, fallback?)Get calling code from numberphone: string (must include country prefix), fallback?: stringstring | undefined
isSameNumber(phone1, phone2, country?)Compare two numbersphone1: string, phone2: string, country?: CountryCodeboolean
isValidForCountry(phone, country)Validate for countryphone: string, country: CountryCodeboolean
formatRFC3966(phone, country?, fallback?)Format in RFC3966phone: string, country?: CountryCode, fallback?: stringstring | undefined
format(phone, formatType, country?, fallback?)Format with custom formatphone: string, formatType: NumberFormat, country?: CountryCode, fallback?: stringstring | undefined

Note:

  • The country? parameter is optional but highly recommended for national numbers (without country code prefix). When omitted for national numbers, functions may return undefined or error objects due to ambiguity. For numbers with country code prefix (e.g., +1...), the parameter is optional and the country is auto-detected.
  • The fallback? parameter is optional and provides error handling:
    • Format functions (formatPhoneNumber, formatInternational, formatNational, formatRFC3966, format, normalizePhoneNumber): If parsing fails and fallback is provided, returns fallback; otherwise returns the original phoneNumber.
    • Extract/Get functions (extractPhoneNumber, getNationalNumber, getCallingCodeFromNumber, getCountryFromPhoneNumber, getCallingCode): If parsing fails and fallback is provided, returns fallback; otherwise returns undefined.
    • Parse function (parsePhone): Returns an object with isValid: false, isPossible: false when parsing fails (never returns null).

Contributing

License

Proprietary. Copyright (c) 2025-2026 Foodhub. All rights reserved.

This package is licensed, not sold. You may install it and use it as a dependency within your own applications. You may not modify it, redistribute it on a standalone basis, or reverse engineer it. See LICENSE for the full terms.


Made with create-react-native-library

版本与发布记录

@foodhubglobal/phone 共发布 1 个版本,首次发布于 2026/08/10;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.1.12latest2026/08/103 个月前

关于 @foodhubglobal/phone 的常见问题

@foodhubglobal/phone 是什么?

Phone number parsing, formatting and validation utilities built on libphonenumber-js, with TypeScript types and dual CommonJS/ES module builds for React Native, React and Node.js.

如何安装 @foodhubglobal/phone?

在项目目录运行 npm install @foodhubglobal/phone,或使用 pnpm add / yarn add / bun add。

@foodhubglobal/phone 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@foodhubglobal/phone 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@foodhubglobal/phone 可以商用吗?

@foodhubglobal/phone 使用 SEE LICENSE IN LICENSE 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@foodhubglobal/phone 的下载量有多少?

根据 npm 官方下载统计,@foodhubglobal/phone 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,210 次,日均约 74 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@foodhubglobal/phone 还在维护吗?

最新版本 0.1.12 发布于 2026/08/10(3 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/08/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。