diff --git a/README.md b/README.md index 75a727a..611a7f8 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,9 @@ addCommas('۸۲۳۳۴۵'); // 823,345 removeCommas('654,562'); // 654562 removeCommas('3,365.255'); // 3365.255 + +'11222'.addComma // 11,222 +'4,544.562'.removeComma // 4544.562 ``` - #### Converting Persian numbers to Arabic / English numbers and reverse - [source](https://github.com/persian-tools/dart-persian-tools/blob/master/lib/src/core/digits/methods.dart) diff --git a/example/example_commas_methods.dart b/example/example_commas_methods.dart index 3810942..4f708bb 100644 --- a/example/example_commas_methods.dart +++ b/example/example_commas_methods.dart @@ -6,4 +6,7 @@ void main() { print(removeCommas('654,562')); // 654562 print(removeCommas('3,365.255')); // 3365.255 + + print('11222'.addComma); // 11,222 + print('4,544.562'.removeComma); // 4544.562 } diff --git a/lib/src/core/commas/methods.dart b/lib/src/core/commas/methods.dart index 7b46fdb..3788523 100644 --- a/lib/src/core/commas/methods.dart +++ b/lib/src/core/commas/methods.dart @@ -33,3 +33,10 @@ num removeCommas(String number) { } return num.parse(number); } + +/// Extension wrapper for functions +extension Commas on String { + String get addComma => addCommas(this); + + num get removeComma => removeCommas(this); +} diff --git a/test/test_commas.dart b/test/test_commas.dart index b0804b2..43ebd17 100644 --- a/test/test_commas.dart +++ b/test/test_commas.dart @@ -21,5 +21,9 @@ void main() { expect(removeCommas('30,000,000'), equals(30000000)); expect(removeCommas('300'), equals(300)); }); + test('extension methods', () { + expect('522642'.addComma, equals('522,642')); + expect('56,555.978'.removeComma, equals(56555.978)); + }); }); }