Skip to content

Commit

Permalink
improvement(WordsToNumber): added example and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
payam-zahedi committed May 29, 2021
1 parent 2c40641 commit d967c71
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 18 deletions.
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

- [x] [Removing ordinal suffixes](#removing-ordinal-suffixes---source)
- [x] [Adding ordinal suffixes](#adding-ordinal-suffixes---source)
- [ ] Converting Persian words to number
- [x] Converting Persian words to number
- [ ] Converting Persian numbers to word
- [x] [Adding and removing separator to / from numbers](#adding-and-removing-separator-to--from-numbers---source)
- [x] [Converting Persian numbers to Arabic / English numbers and reverse](#converting-persian-numbers-to-arabic--english-numbers-and-reverse---source)
Expand All @@ -36,7 +36,7 @@

now let's look at examples and how work with apis in package

- #### Adding Ordinal Suffixes - [source](https://github.com/persian-tools/dart-persian-tools/blob/master/lib/src/core/remove_ordinal_suffix/remove_ordinal_suffix.dart)
- #### Adding Ordinal Suffixes - [source](https://github.com/persian-tools/dart-persian-tools/blob/master/lib/src/core/add_ordinal_suffix/add_ordinal_suffix.dart)

```dart
var number = 'سی سه'; // or سی | شصت | پنجاه دو
Expand All @@ -46,7 +46,7 @@ addOrdinalSuffix(number); // سی سوم | سی اُم | شصتم | پنجاه
number.withOrdinalSuffix // ... like so
```

- #### Removing Ordinal Suffixes - [source](https://github.com/persian-tools/dart-persian-tools/blob/master/lib/src/core/add_ordinal_suffix/add_ordinal_suffix.dart)
- #### Removing Ordinal Suffixes - [source](https://github.com/persian-tools/dart-persian-tools/blob/master/lib/src/core/remove_ordinal_suffix/remove_ordinal_suffix.dart)

```dart
var number = 'چهل و سوم'; // سی سوم | سی اُم | شصتم | پنجاه دوم
Expand All @@ -56,6 +56,32 @@ removeOrdinalSuffix(number); // سی | شصت | پنجاه دو
number.withoutOrdinalSuffix; // ... like so
```

- #### Converting Persian words to number - [source](https://github.com/persian-tools/dart-persian-tools/blob/master/lib/src/core/words_to_number/words_to_number.dart)

```dart
final words = 'سه هزار دویست و دوازده';
/// use [wordsToNumber] method to convert [words] to int number
wordsToNumber(words); // 3212
/// use [wordsToNumberString] method to convert [words] to String
wordsToNumberString(words); // '3212' as String
/// [wordsToNumberString] also has two optional parameter
/// use [digit] optional parameter to convert the digits to specific local digits
/// use [addComma] to add comma between the every 3 digits
wordsToNumberString(
words,
digits: DigitLocale.fa,
addComma: true,
); // '۳,۲۱۲' as String
/// or you can easily use extension methods on String object
words.convertWordsToNumber(); // 3212
words.convertWordsToNumberString(); // '3212' as String
```

- #### Adding and removing separator to / from numbers - [source](https://github.com/persian-tools/dart-persian-tools/blob/master/lib/src/core/commas/methods.dart)

```dart
Expand Down
3 changes: 1 addition & 2 deletions example/example_vehicle_plate_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ void main() {

/// you can use create [Plate] with below String extension method
var motorPlate = '12345678'.createVehiclePlate;
var motorPlate = '12345678'.createVehiclePlate;
print(motorPlate.info.type); // Motorcycle
print(motorPlate.info.template); // 123-45678
print(motorPlate.info.province); // مرکز تهران
print(motorPlate.info.category); // null because its type is Motorcycle
print(motorPlate.isValid); // true

}
25 changes: 25 additions & 0 deletions example/example_words_to_number.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:persian_tools/persian_tools.dart';

void main() {
final words = 'سه هزار دویست و دوازده';

/// use [wordsToNumber] method to convert [words] to int number
print(wordsToNumber(words)); // 3212

/// use [wordsToNumberString] method to convert [words] to String
print(wordsToNumberString(words)); // '3212' as String

/// [wordsToNumberString] also has two optional parameter
/// use [digit] optional parameter to convert the digits to specific local digits
/// use [addComma] to add comma between the every 3 digits
print(wordsToNumberString(
words,
digits: DigitLocale.fa,
addComma: true,
)); // '۳,۲۱۲' as String

/// or you can easily use extension methods on String object
print(words.convertWordsToNumber()); // 3212

print(words.convertWordsToNumberString()); // '3212' as String
}
7 changes: 3 additions & 4 deletions lib/src/constants/words_to_number/constants.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const units = <String,int>{
const units = <String, int>{
'صفر': 0,
'یک': 1,
'دو': 2,
Expand Down Expand Up @@ -30,7 +30,7 @@ const units = <String,int>{
'نود': 90,
};

const ten = <String,int>{
const ten = <String, int>{
'صد': 100,
'یکصد': 100,
'دویست': 200,
Expand All @@ -43,15 +43,14 @@ const ten = <String,int>{
'نهصد': 900,
};

const magnitude = <String,int>{
const magnitude = <String, int>{
'هزار': 1000,
'میلیون': 1000000,
'بیلیون': 1000000000,
'میلیارد': 1000000000,
'تریلیون': 1000000000000,
};


const typoList = {
'شیش صد': 'ششصد',
'شش صد': 'ششصد',
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/remove_ordinal_suffix/remove_ordinal_suffix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ String removeOrdinalSuffix(String word) {
.replaceAll(RegExp('مین\$', caseSensitive: false), '')
.replaceAll(RegExp('(ام| اُم)\$', caseSensitive: false), '');
if (word.endsWith('سوم')) {
word = word.substring(0, word.length-3) + 'سه';
word = word.substring(0, word.length - 3) + 'سه';
} else if (word.endsWith('م')) {
word = word.substring(0, word.length-1);
word = word.substring(0, word.length - 1);
}

return word;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/vehicle_plate/classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class Plate {

extension VehiclePlate on String {
Plate get createVehiclePlate => Plate(plate: this);
}
}
11 changes: 11 additions & 0 deletions lib/src/core/words_to_number/words_to_number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:persian_tools/persian_tools.dart';
import 'package:persian_tools/src/constants/words_to_number/constants.dart';
import 'package:persian_tools/src/internal_methods.dart';

//ToDo : implementing fuzzy option
enum DigitLocale {
en,
fa,
Expand Down Expand Up @@ -72,3 +73,13 @@ String? wordsToNumberString(
return addedCommasIfNeeded;
}
}

extension WordsToNumber on String {
int? convertWordsToNumber() => wordsToNumber(this);

String? convertWordsToNumberString({
DigitLocale digits = DigitLocale.en,
bool addComma = false,
}) =>
wordsToNumberString(this, digits: digits, addComma: addComma);
}
11 changes: 5 additions & 6 deletions test/test_digits_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ void main() {
);
});
test('test convertEnToAr method', () {
expect(convertEnToAr('123456'),"۱۲۳٤٥٦");
expect(convertEnToAr('1234567891'),"۱۲۳٤٥٦۷۸۹۱");
expect(convertEnToAr('0'),"۰");
expect(convertEnToAr("123٤٥٦"),"۱۲۳٤٥٦");
expect(convertEnToAr(""),"");

expect(convertEnToAr('123456'), "۱۲۳٤٥٦");
expect(convertEnToAr('1234567891'), "۱۲۳٤٥٦۷۸۹۱");
expect(convertEnToAr('0'), "۰");
expect(convertEnToAr("123٤٥٦"), "۱۲۳٤٥٦");
expect(convertEnToAr(""), "");
});
test('test convertFaToEn method', () {
// just digits
Expand Down

0 comments on commit d967c71

Please sign in to comment.