From d967c71965022e8c4072cc133b16350c9b3d324d Mon Sep 17 00:00:00 2001 From: Payam Zahedi Date: Sat, 29 May 2021 10:55:18 +0430 Subject: [PATCH] improvement(WordsToNumber): added example and README.md --- README.md | 32 +++++++++++++++++-- example/example_vehicle_plate_classes.dart | 3 +- example/example_words_to_number.dart | 25 +++++++++++++++ .../constants/words_to_number/constants.dart | 7 ++-- .../remove_ordinal_suffix.dart | 4 +-- lib/src/core/vehicle_plate/classes.dart | 2 +- .../core/words_to_number/words_to_number.dart | 11 +++++++ test/test_digits_methods.dart | 11 +++---- 8 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 example/example_words_to_number.dart diff --git a/README.md b/README.md index 2dbefcb..4cf13ca 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 سی | شصت | پنجاه دو @@ -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 = 'چهل و سوم'; // سی سوم | سی اُم | شصتم | پنجاه دوم @@ -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 diff --git a/example/example_vehicle_plate_classes.dart b/example/example_vehicle_plate_classes.dart index ea161fe..1b965f5 100644 --- a/example/example_vehicle_plate_classes.dart +++ b/example/example_vehicle_plate_classes.dart @@ -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 - } diff --git a/example/example_words_to_number.dart b/example/example_words_to_number.dart new file mode 100644 index 0000000..e6bb702 --- /dev/null +++ b/example/example_words_to_number.dart @@ -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 +} diff --git a/lib/src/constants/words_to_number/constants.dart b/lib/src/constants/words_to_number/constants.dart index 99bd7a5..816e8f7 100644 --- a/lib/src/constants/words_to_number/constants.dart +++ b/lib/src/constants/words_to_number/constants.dart @@ -1,4 +1,4 @@ -const units = { +const units = { 'صفر': 0, 'یک': 1, 'دو': 2, @@ -30,7 +30,7 @@ const units = { 'نود': 90, }; -const ten = { +const ten = { 'صد': 100, 'یکصد': 100, 'دویست': 200, @@ -43,7 +43,7 @@ const ten = { 'نهصد': 900, }; -const magnitude = { +const magnitude = { 'هزار': 1000, 'میلیون': 1000000, 'بیلیون': 1000000000, @@ -51,7 +51,6 @@ const magnitude = { 'تریلیون': 1000000000000, }; - const typoList = { 'شیش صد': 'ششصد', 'شش صد': 'ششصد', diff --git a/lib/src/core/remove_ordinal_suffix/remove_ordinal_suffix.dart b/lib/src/core/remove_ordinal_suffix/remove_ordinal_suffix.dart index 954ec9c..46af92d 100644 --- a/lib/src/core/remove_ordinal_suffix/remove_ordinal_suffix.dart +++ b/lib/src/core/remove_ordinal_suffix/remove_ordinal_suffix.dart @@ -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; diff --git a/lib/src/core/vehicle_plate/classes.dart b/lib/src/core/vehicle_plate/classes.dart index 7dea0b2..e18f200 100644 --- a/lib/src/core/vehicle_plate/classes.dart +++ b/lib/src/core/vehicle_plate/classes.dart @@ -16,4 +16,4 @@ class Plate { extension VehiclePlate on String { Plate get createVehiclePlate => Plate(plate: this); -} \ No newline at end of file +} diff --git a/lib/src/core/words_to_number/words_to_number.dart b/lib/src/core/words_to_number/words_to_number.dart index ed7af72..10bee38 100644 --- a/lib/src/core/words_to_number/words_to_number.dart +++ b/lib/src/core/words_to_number/words_to_number.dart @@ -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, @@ -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); +} diff --git a/test/test_digits_methods.dart b/test/test_digits_methods.dart index 43df9df..c5744f5 100644 --- a/test/test_digits_methods.dart +++ b/test/test_digits_methods.dart @@ -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