Skip to content

Commit

Permalink
feat(commas):added extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
EhsanAramide committed Apr 21, 2021
1 parent 1f83819 commit 4c42acd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions example/example_commas_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions lib/src/core/commas/methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions test/test_commas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
}

0 comments on commit 4c42acd

Please sign in to comment.