Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add national formats #72

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 9.0.0
- Added a format parameter to choose between international and national format
- Format now uses the national format by default

## 8.3.0
- added support for remaining phoneNumberTypes (thanks @cottonman132 )

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ for local formats.
final phoneNumber =
PhoneNumber.parse('2025550119', destinationCountry: IsoCode.US);
final formattedNsn = phoneNumber.formatNsn();
print('formatted: $formattedNsn'); // 202-555-0119
print('formatted: $formattedNsn'); // (202) 555-0119
```

### Range
Expand Down
26 changes: 13 additions & 13 deletions demo/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.5"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.5"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -111,18 +111,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.12.0"
version: "1.15.0"
path:
dependency: transitive
description:
Expand All @@ -137,7 +137,7 @@ packages:
path: ".."
relative: true
source: path
version: "8.2.2"
version: "8.3.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -187,10 +187,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.7.2"
vector_math:
dependency: transitive
description:
Expand All @@ -203,10 +203,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "14.2.5"
sdks:
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main(List<String> arguments) {
final phoneNumber =
PhoneNumber.parse('2025550119', destinationCountry: IsoCode.US);
final formattedNsn = phoneNumber.formatNsn();
print('formatted: $formattedNsn'); // 202-555-0119
print('formatted: $formattedNsn'); // (202) 555-0119
print('international: ${phoneNumber.international}');
// Ranges
print('');
Expand Down
19 changes: 16 additions & 3 deletions lib/src/formatting/phone_number_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ import 'package:phone_numbers_parser/src/regex/match_entirely_extension.dart';
import '../metadata/models/phone_metadata_formats.dart';
import '../iso_codes/iso_code.dart';

enum NsnFormat {
national,
international,
}

class PhoneNumberFormatter {
/// format national number for international use
static String formatNsn(String nsn, IsoCode isoCode) {
static String formatNsn(
String nsn,
IsoCode isoCode, [
NsnFormat format = NsnFormat.national,
]) {
if (nsn.isEmpty) {
return nsn;
}
Expand All @@ -31,7 +40,9 @@ class PhoneNumberFormatter {
var transformRule = formatingRule.format;
// if there is an international format, we use it
final intlFormat = formatingRule.intlFormat;
if (intlFormat != null && intlFormat != 'NA') {
if (format == NsnFormat.international &&
intlFormat != null &&
intlFormat != 'NA') {
transformRule = intlFormat;
}

Expand Down Expand Up @@ -78,7 +89,9 @@ class PhoneNumberFormatter {
return missingDigits;
}

/// gets the matching format rule
/// gets the matching format rule,
/// if there is only one formatting rule return it,
/// else finds the formatting rule that better matches the phone number
static PhoneMetadataFormat? _getMatchingFormatRules({
required PhoneMetadataFormats formatingRules,
required String nsn,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/phone_number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class PhoneNumber {
);

/// formats the nsn, if no [isoCode] is provided the phone number region is used.
String formatNsn({IsoCode? isoCode}) =>
PhoneNumberFormatter.formatNsn(nsn, isoCode ?? this.isoCode);
String formatNsn({IsoCode? isoCode, NsnFormat format = NsnFormat.national}) =>
PhoneNumberFormatter.formatNsn(nsn, isoCode ?? this.isoCode, format);

@Deprecated('Use [formatNsn] instead')
String getFormattedNsn({IsoCode? isoCode}) => formatNsn(isoCode: isoCode);
Expand Down
Loading