搜尋套件、儲存庫與 APP 分類

GitHub 儲存庫cedvdb/phone_numbers_parser

phone_numbers_parser

Dart 電話號碼解析器,基於 libphonenumber 和 PhoneNumberKit。

phone_numbers_parser 專案封面
星標
71
分支
47
最近推送(UTC)
2026年9月1日
專案狀態
未封存

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

Phone Numbers Parser

Dart library for parsing phone numbers. Inspired by Google's libphonenumber and PhoneNumberKit for ios.

The advantage of this lib instead of libphonenumber is that it instantly supports all platforms (no need for channeling).

Features

  • Validation: Validate based on type (mobile, fixed line, voip)
  • Formatting: Format phone number for a specific country
  • Phone ranges: find all phone numbers in a range of phone numbers
  • Find phone numbers in a text
  • Supports eastern arabic digits
  • Uses best-in-class metadata from Google's libPhoneNumber project.

Demo

demo

Contributing

Please read this to upgrade the metadata

Usage

Use the class PhoneNumber as a starting point

import 'package:phone_numbers_parser/phone_numbers_parser.dart';

void main(List<String> arguments) {
  final frPhone0 = PhoneNumber.parse('+33 655 5705 76');
  // raw caller in france calling another person in france
  final frPhone1 =
      PhoneNumber.parse('0 655 5705 76', callerCountry: IsoCode.FR);
  // us calling to france
  final frPhone2 =
      PhoneNumber.parse('011 33 655-5705-76', callerCountry: IsoCode.US);
  final frPhone3 =
      PhoneNumber.parse('011 33 655 5705 76', destinationCountry: IsoCode.FR);
  final isAllEqual =
      frPhone0 == frPhone1 && frPhone0 == frPhone2 && frPhone0 == frPhone3;
  print(frPhone1);
  print('all raw same: $isAllEqual');

  // validation
  final valid = frPhone1.isValid();
  final validMobile = frPhone1.isValid(type: PhoneNumberType.mobile);
  final validFixed = frPhone1.isValid(type: PhoneNumberType.fixedLine);
  print('valid: $valid'); // true
  print('valid mobile: $validMobile'); // true
  print('valid fixed line: $validFixed'); // false

  // utils
  final text =
      'hey my phone number is: +33 939 876 218, but you can call me on +33 939 876 999 too';
  final found = PhoneNumber.findPotentialPhoneNumbers(text);
  print('found: $found');
}

validation

final valid = frPhone1.validate();
final validMobile = frPhone1.validate(type: PhoneNumberType.mobile);
final validFixed = frPhone1.validate(type: PhoneNumberType.fixedLine);
print('valid: $valid'); // true
print('valid mobile: $validMobile'); // true
print('valid fixed line: $validFixed'); // false
Formatting

Formatting is region specific, so the formats will vary by iso code to accommodate for local formats.

final phoneNumber =
    PhoneNumber.parse('2025550119', destinationCountry: IsoCode.US);
final formattedNsn = phoneNumber.formatNsn();
print('formatted: $formattedNsn'); // (202) 555-0119
Range
print('Ranges:');
final first = PhoneNumber.parse('+33 655 5705 00');
final last = PhoneNumber.parse('+33 655 5705 03');
final range = PhoneNumber.getRange(first, last);

print('Count: ${range.count}');
print('Expand: ${range.expandRange().join(',')}');

if (first > last) {
  print("this shouldn't be.");
}

final one = PhoneNumber.parse('+33 655 5705 01');
final two = PhoneNumber.parse('+33 655 5705 02');

if (one.isAdjacentTo(two)) {
  print('We are together');
}
if (one.isSequentialTo(two)) {
  print('$two comes after $one');
}

/// treat the phone no. like an int
final three = two + 1;
print('Its still a phone No. $three');
two - 1 == one;
final another = one + 2;
print('$another == $three');
cedvdb GitHub avatar
GitHub 作者

cedvdb ↗

Developer by day and developer by night. I'm passionate about architecture. Designing code bases that reflect the business domain I'm working on.

語言Dart

使用的依賴

依賴清單 4 項

如何選擇適合專案的儲存庫?

先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。

  1. 透過儲存庫名稱、作者或描述搜尋,再依技術分類縮小範圍。
  2. 綜合星標、分支與最近推送比較專案;星標反映社群關注,不保證品質。
  3. 使用前在 GitHub 確認 README、授權條款與 Issues。

來源與維護

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗