Skip to content

Commit

Permalink
7.0.1 (#36)
Browse files Browse the repository at this point in the history
* lock

* Enhanced the READ.ME ✅ (#32)

- Removed a redundant code line that was breaking the documentation format in pub.dev
- Added a notice for the breaking changes in version 7.0.0

* fix 35 local

---------

Co-authored-by: Moaz El-sawaf <43591891+moazelsawaf@users.noreply.github.com>
  • Loading branch information
cedvdb and moazelsawaf committed Feb 19, 2023
1 parent b119d6f commit 4e9e2cd
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 158 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
## 7.0.0+1
- readme

## 7.0.1

- Fix issue where international phone numbers would be transformed as if those were local ones.

## 7.0.0
This major introduces a more accurate parsing logic taking into account caller and destination country.
Expand Down
89 changes: 42 additions & 47 deletions README.md
Expand Up @@ -4,6 +4,9 @@ Dart library for parsing phone numbers. Inspired by Google's libphonenumber and

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

## Breaking Changes starting from version 7.0.0
- Removed most factory methods on PhoneNumber in favor of `parse()` method, e.g. removed `fromRaw()` and substituted it with `parse()`, check the READ.ME
- Renamed validate and validateWithLength to isValid and isValidateLength

## Features

Expand All @@ -20,11 +23,6 @@ The advantage of this lib instead of libphonenumber is that it instantly support

Use the class `PhoneNumber` as a starting point


```dart
// raw parsing
PhoneNumber.fromRaw('655 5705 76', )
```dart
import 'package:phone_numbers_parser/phone_numbers_parser.dart';
Expand Down Expand Up @@ -56,18 +54,18 @@ void main(List<String> arguments) {
'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

```dart
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
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
Expand All @@ -76,44 +74,41 @@ Formatting is region specific, so the formats will vary by iso code to accommoda
for local formats.

```dart
final phoneNumber =
PhoneNumber.parse('2025550119', destinationCountry: IsoCode.US);
final formattedNsn = phoneNumber.getFormattedNsn();
print('formatted: $formattedNsn'); // 202-555-0119/ 202-555-0119
final phoneNumber =
PhoneNumber.parse('2025550119', destinationCountry: IsoCode.US);
final formattedNsn = phoneNumber.getFormattedNsn();
print('formatted: $formattedNsn'); // 202-555-0119/ 202-555-0119
```

### Range

```dart
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');
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');
```


4 changes: 2 additions & 2 deletions lib/src/parsers/_international_prefix_parser.dart
@@ -1,9 +1,9 @@
import 'package:phone_number_metadata/phone_number_metadata.dart';

abstract class InternationalPrefixParser {
/// remove the international prefix of a phone number if present.
/// Removes the exit code from a phone number if present.
///
/// It expects a normalized [phoneNumber] without the country calling code.
/// It expects a normalized [phoneNumber].
/// if phone starts with + it is removed
/// if starts with 00 or 011
/// we consider those as internationalPrefix as
Expand Down
38 changes: 22 additions & 16 deletions lib/src/parsers/phone_parser.dart
@@ -1,12 +1,13 @@
import 'package:meta/meta.dart';
import 'package:phone_number_metadata/phone_number_metadata.dart';
import 'package:phone_numbers_parser/phone_numbers_parser.dart';
import 'package:phone_numbers_parser/src/parsers/_country_code_parser.dart';
import 'package:phone_numbers_parser/src/parsers/_international_prefix_parser.dart';
import 'package:phone_numbers_parser/src/parsers/_national_number_parser.dart';
import 'package:phone_numbers_parser/src/parsers/_text_parser.dart';
import 'package:phone_numbers_parser/src/utils/_metadata_finder.dart';
import 'package:phone_numbers_parser/src/utils/_metadata_matcher.dart';

import '../../phone_numbers_parser.dart';
import '../utils/_metadata_finder.dart';
import '../utils/_metadata_matcher.dart';
import '_country_code_parser.dart';
import '_international_prefix_parser.dart';
import '_national_number_parser.dart';
import '_text_parser.dart';

/// {@template phoneNumber}
/// Parses a phone number given caller or destination information.
Expand Down Expand Up @@ -57,17 +58,22 @@ abstract class PhoneParser {
withoutExitCode,
destinationMetadata.countryCode,
);
var nsn = NationalNumberParser.removeNationalPrefix(
national,
destinationMetadata,
);
nsn = NationalNumberParser.transformLocalNsnToInternationalUsingPatterns(
nsn,
destinationMetadata,
);
final containsCountryCode = national.length != withoutExitCode.length;
var nsn = national;
if (!containsCountryCode) {
nsn = NationalNumberParser.removeNationalPrefix(
national,
destinationMetadata,
);
nsn = NationalNumberParser.transformLocalNsnToInternationalUsingPatterns(
nsn,
destinationMetadata,
);
}

// at this point we have the phone number that has been processed, but
// if it is invalid, we remove the processing of the nsn part
// this allows for a better behavior in some input usages where the
// this allows for a better behavior in some widget input usages where the
// text is changed on validity.
final parsed = PhoneNumber(isoCode: destinationMetadata.isoCode, nsn: nsn);
if (parsed.isValid()) return parsed;
Expand Down

0 comments on commit 4e9e2cd

Please sign in to comment.