Skip to content

Commit

Permalink
Bugfix / Argentinian transformRule mismatch $2 15-$3-$4 (#76)
Browse files Browse the repository at this point in the history
* removed the contain index check of the transform rule in order to support the rules that does not start with 1. ex: $2 15-$3-$4

* added test for format national phone number if transformRule does not start with \$1. example: (\$2 15-\$3-\$4)(AR), Argentina

* renamed test description
  • Loading branch information
DenisDoc authored Nov 10, 2024
1 parent 4a890bb commit 16e6c97
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/src/parsers/_national_number_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ abstract class NationalNumberParser {

var transformed = transformRule;
bool shouldContinueLoop(int i) =>
match.groupCount >= i &&
match.group(i) != null &&
transformed.contains('\$$i');
match.groupCount >= i && match.group(i) != null;
for (var i = 1; shouldContinueLoop(i); i++) {
transformed = transformed.replaceFirst('\$$i', match.group(i)!);
}
Expand Down
46 changes: 46 additions & 0 deletions test/phone_number_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,50 @@ void main() {
expect(zero.isSequentialTo(two), isFalse);
});
});
group(
'format national phone number if transformRule does not start with \$1. example: (\$2 15-\$3-\$4)(AR)',
() {
test('should format argentinian phone numbers', () {
String format(String phoneNumber) =>
PhoneNumber.parse(phoneNumber, destinationCountry: IsoCode.AR)
.formatNsn();
var testNumber = '';
expect(format(testNumber), equals(''));
testNumber = '5';
expect(format(testNumber), equals('5'));
testNumber = '54';
expect(format(testNumber), equals('54'));
testNumber = '549';
expect(format(testNumber), equals('549'));
testNumber = '5492';
expect(format(testNumber), equals('5492'));
testNumber = '54926';
expect(format(testNumber), equals('54926'));
testNumber = '549261';
expect(format(testNumber), equals('549261'));
testNumber = '5492615';
expect(format(testNumber), equals('5492615'));
testNumber = '54926153';
expect(format(testNumber), equals('54926153'));
testNumber = '549261532';
expect(format(testNumber), equals('549261532'));
testNumber = '5492615325';
expect(format(testNumber), equals('5492615325'));

testNumber = '54926153256';
expect(format(testNumber), equals('54926153256'));
testNumber = '549261532565';
expect(format(testNumber), equals('549261532565'));
testNumber = '5492615325656';
expect(format(testNumber), equals('261 15-532-5656'));
testNumber = '+5492615325656';
expect(format(testNumber), equals('261 15-532-5656'));
testNumber = '54 9 261-5325 656';
expect(format(testNumber), equals('261 15-532-5656'));
testNumber = '54-9-261-5325-656';
expect(format(testNumber), equals('261 15-532-5656'));
testNumber = '54.9.261.5325.656';
expect(format(testNumber), equals('261 15-532-5656'));
});
});
}

0 comments on commit 16e6c97

Please sign in to comment.