diff --git a/CHANGELOG.md b/CHANGELOG.md index 01f8747..eaaaa77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ) diff --git a/README.md b/README.md index 988525a..9dec895 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/demo/pubspec.lock b/demo/pubspec.lock index b6c0b99..6b89017 100644 --- a/demo/pubspec.lock +++ b/demo/pubspec.lock @@ -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: @@ -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: @@ -137,7 +137,7 @@ packages: path: ".." relative: true source: path - version: "8.2.2" + version: "8.3.0" sky_engine: dependency: transitive description: flutter @@ -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: @@ -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" diff --git a/example/lib/main.dart b/example/lib/main.dart index ecc60be..efec3d1 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -38,7 +38,7 @@ void main(List 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(''); diff --git a/lib/src/formatting/phone_number_formatter.dart b/lib/src/formatting/phone_number_formatter.dart index 7dacc71..2c3529e 100644 --- a/lib/src/formatting/phone_number_formatter.dart +++ b/lib/src/formatting/phone_number_formatter.dart @@ -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; } @@ -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; } @@ -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, diff --git a/lib/src/phone_number.dart b/lib/src/phone_number.dart index c8c4a97..c7e5ee6 100644 --- a/lib/src/phone_number.dart +++ b/lib/src/phone_number.dart @@ -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); diff --git a/pubspec.lock b/pubspec.lock index a0dfd73..0f5c95a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,26 +5,31 @@ packages: dependency: transitive description: name: _fe_analyzer_shared - sha256: "0b2f2bd91ba804e53a61d757b986f89f1f9eaed5b11e4b2f5a2468d86d6c9fc7" + sha256: "45cfa8471b89fb6643fe9bf51bd7931a76b8f5ec2d65de4fb176dba8d4f22c77" url: "https://pub.dev" source: hosted - version: "67.0.0" + version: "73.0.0" + _macros: + dependency: transitive + description: dart + source: sdk + version: "0.3.2" analyzer: dependency: transitive description: name: analyzer - sha256: "37577842a27e4338429a1cbc32679d508836510b056f1eedf0c8d20e39c1383d" + sha256: "4959fec185fe70cce007c57e9ab6983101dbe593d2bf8bbfb4453aaec0cf470a" url: "https://pub.dev" source: hosted - version: "6.4.1" + version: "6.8.0" args: dependency: transitive description: name: args - sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596 + sha256: "7cf60b9f0cc88203c5a190b4cd62a99feea42759a7fa695010eb5de1c0b2252a" url: "https://pub.dev" source: hosted - version: "2.4.2" + version: "2.5.0" async: dependency: transitive description: @@ -45,10 +50,10 @@ packages: dependency: transitive description: name: collection - sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a + sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf url: "https://pub.dev" source: hosted - version: "1.18.0" + version: "1.19.0" convert: dependency: transitive description: @@ -61,18 +66,18 @@ packages: dependency: transitive description: name: coverage - sha256: "8acabb8306b57a409bf4c83522065672ee13179297a6bb0cb9ead73948df7c76" + sha256: c1fb2dce3c0085f39dc72668e85f8e0210ec7de05345821ff58530567df345a5 url: "https://pub.dev" source: hosted - version: "1.7.2" + version: "1.9.2" crypto: dependency: transitive description: name: crypto - sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab + sha256: ec30d999af904f33454ba22ed9a86162b35e52b44ac4807d1d93c288041d7d27 url: "https://pub.dev" source: hosted - version: "3.0.3" + version: "3.0.5" file: dependency: transitive description: @@ -85,10 +90,10 @@ packages: dependency: transitive description: name: frontend_server_client - sha256: "408e3ca148b31c20282ad6f37ebfa6f4bdc8fede5b74bc2f08d9d92b55db3612" + sha256: f64a0333a82f30b0cca061bc3d143813a486dc086b574bfb233b7c1372427694 url: "https://pub.dev" source: hosted - version: "3.2.0" + version: "4.0.0" glob: dependency: transitive description: @@ -109,10 +114,10 @@ packages: dependency: transitive description: name: http_parser - sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + sha256: "40f592dd352890c3b60fec1b68e786cefb9603e05ff303dbc4dda49b304ecdf4" url: "https://pub.dev" source: hosted - version: "4.0.2" + version: "4.1.0" io: dependency: transitive description: @@ -145,6 +150,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.0" + macros: + dependency: transitive + description: + name: macros + sha256: "0acaed5d6b7eab89f63350bccd82119e6c602df0f391260d0e32b5e23db79536" + url: "https://pub.dev" + source: hosted + version: "0.1.2-main.4" matcher: dependency: transitive description: @@ -157,18 +170,18 @@ packages: dependency: "direct main" description: name: meta - sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" + sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 url: "https://pub.dev" source: hosted - version: "1.12.0" + version: "1.15.0" mime: dependency: transitive description: name: mime - sha256: "2e123074287cc9fd6c09de8336dae606d1ddb88d9ac47358826db698c176a1f2" + sha256: "801fd0b26f14a4a58ccb09d5892c3fbdeff209594300a542492cf13fba9d247a" url: "https://pub.dev" source: hosted - version: "1.0.5" + version: "1.0.6" node_preamble: dependency: transitive description: @@ -221,10 +234,10 @@ packages: dependency: transitive description: name: shelf - sha256: ad29c505aee705f41a4d8963641f91ac4cee3c8fad5947e033390a7bd8180fa4 + sha256: e7dd780a7ffb623c57850b33f43309312fc863fb6aa3d276a754bb299839ef12 url: "https://pub.dev" source: hosted - version: "1.4.1" + version: "1.4.2" shelf_packages_handler: dependency: transitive description: @@ -237,26 +250,26 @@ packages: dependency: transitive description: name: shelf_static - sha256: a41d3f53c4adf0f57480578c1d61d90342cd617de7fc8077b1304643c2d85c1e + sha256: c87c3875f91262785dade62d135760c2c69cb217ac759485334c5857ad89f6e3 url: "https://pub.dev" source: hosted - version: "1.1.2" + version: "1.1.3" shelf_web_socket: dependency: transitive description: name: shelf_web_socket - sha256: "9ca081be41c60190ebcb4766b2486a7d50261db7bd0f5d9615f2d653637a84c1" + sha256: "073c147238594ecd0d193f3456a5fe91c4b0abbcc68bf5cd95b36c4e194ac611" url: "https://pub.dev" source: hosted - version: "1.0.4" + version: "2.0.0" source_map_stack_trace: dependency: transitive description: name: source_map_stack_trace - sha256: "84cf769ad83aa6bb61e0aa5a18e53aea683395f196a6f39c4c881fb90ed4f7ae" + sha256: c0713a43e323c3302c2abe2a1cc89aa057a387101ebd280371d6a6c9fa68516b url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" source_maps: dependency: transitive description: @@ -293,10 +306,10 @@ packages: dependency: transitive description: name: string_scanner - sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" + sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3" url: "https://pub.dev" source: hosted - version: "1.2.0" + version: "1.3.0" term_glyph: dependency: transitive description: @@ -309,26 +322,26 @@ packages: dependency: "direct dev" description: name: test - sha256: "7ee446762c2c50b3bd4ea96fe13ffac69919352bd3b4b17bac3f3465edc58073" + sha256: "713a8789d62f3233c46b4a90b174737b2c04cb6ae4500f2aa8b1be8f03f5e67f" url: "https://pub.dev" source: hosted - version: "1.25.2" + version: "1.25.8" test_api: dependency: transitive description: name: test_api - sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" + sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c" url: "https://pub.dev" source: hosted - version: "0.7.0" + version: "0.7.3" test_core: dependency: transitive description: name: test_core - sha256: "2bc4b4ecddd75309300d8096f781c0e3280ca1ef85beda558d33fcbedc2eead4" + sha256: "12391302411737c176b0b5d6491f466b0dd56d4763e347b6714efbaa74d7953d" url: "https://pub.dev" source: hosted - version: "0.6.0" + version: "0.6.5" typed_data: dependency: transitive description: @@ -341,10 +354,10 @@ packages: dependency: transitive description: name: vm_service - sha256: e7d5ecd604e499358c5fe35ee828c0298a320d54455e791e9dcf73486bc8d9f0 + sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" url: "https://pub.dev" source: hosted - version: "14.1.0" + version: "14.2.5" watcher: dependency: transitive description: @@ -357,18 +370,26 @@ packages: dependency: transitive description: name: web - sha256: "1d9158c616048c38f712a6646e317a3426da10e884447626167240d45209cbad" + sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062 + url: "https://pub.dev" + source: hosted + version: "1.0.0" + web_socket: + dependency: transitive + description: + name: web_socket + sha256: "3c12d96c0c9a4eec095246debcea7b86c0324f22df69893d538fcc6f1b8cce83" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.1.6" web_socket_channel: dependency: transitive description: name: web_socket_channel - sha256: "1d8e795e2a8b3730c41b8a98a2dff2e0fb57ae6f0764a1c46ec5915387d257b2" + sha256: "9f187088ed104edd8662ca07af4b124465893caf063ba29758f97af57e61da8f" url: "https://pub.dev" source: hosted - version: "2.4.4" + version: "3.0.1" webkit_inspection_protocol: dependency: transitive description: @@ -394,4 +415,4 @@ packages: source: hosted version: "3.1.2" sdks: - dart: ">=3.3.0 <4.0.0" + dart: ">=3.4.0 <4.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 87e8bcf..9fb5892 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: phone_numbers_parser description: Dart library for parsing phone numbers. Inspired by Google's libphonenumber and PhoneNumberKit for ios. -version: 8.3.0 +version: 9.0.0 homepage: https://github.com/cedvdb/phone_number_parser environment: diff --git a/test/phone_number_test.dart b/test/phone_number_test.dart index 5c83429..41555ae 100644 --- a/test/phone_number_test.dart +++ b/test/phone_number_test.dart @@ -244,11 +244,40 @@ void main() { }); group('format', () { - test('should format', () { + test('should format with the national format by default', () { String format(String phoneNumber) => PhoneNumber.parse(phoneNumber, destinationCountry: IsoCode.US) .formatNsn(); + var testNumber = ''; + expect(format(testNumber), equals('')); + testNumber = '2'; + expect(format(testNumber), equals('(2')); + testNumber = '20'; + expect(format(testNumber), equals('(20')); + testNumber = '202'; + expect(format(testNumber), equals('(202)')); + testNumber = '2025'; + expect(format(testNumber), equals('(202) 5')); + testNumber = '20255'; + expect(format(testNumber), equals('(202) 55')); + testNumber = '202555'; + expect(format(testNumber), equals('(202) 555')); + testNumber = '2025550'; + expect(format(testNumber), equals('(202) 555-0')); + testNumber = '20255501'; + expect(format(testNumber), equals('(202) 555-01')); + testNumber = '202555011'; + expect(format(testNumber), equals('(202) 555-011')); + testNumber = '2025550119'; + expect(format(testNumber), equals('(202) 555-0119')); + }); + + test('should format with the international format when specified', () { + String format(String phoneNumber) => + PhoneNumber.parse(phoneNumber, destinationCountry: IsoCode.US) + .formatNsn(format: NsnFormat.international); + var testNumber = ''; expect(format(testNumber), equals('')); testNumber = '2';