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 fabc9ae..ed7af72 100644 --- a/lib/src/core/words_to_number/words_to_number.dart +++ b/lib/src/core/words_to_number/words_to_number.dart @@ -46,12 +46,29 @@ int? wordsToNumber(String words) { words = words.replaceAll(RegExp('مین\$', caseSensitive: false), ''); + /// removing the ordinal suffix + words = words.withoutOrdinalSuffix; + + return compute(tokenize(words)); } -String wordsToNumberString( +String? wordsToNumberString( String words, { DigitLocale digits = DigitLocale.en, bool addComma = false, }) { - return ''; + final computeNumbers = wordsToNumber(words); + if (computeNumbers == null) return null; + final addedCommasIfNeeded = + addComma ? computeNumbers.addComma : computeNumbers.toString(); + + switch (digits) { + case DigitLocale.fa: + return convertEnToFa(addedCommasIfNeeded); + case DigitLocale.ar: + return convertEnToAr(addedCommasIfNeeded); + case DigitLocale.en: + default: + return addedCommasIfNeeded; + } } diff --git a/test/test_words_to_number.dart b/test/test_words_to_number.dart index 08ee731..2f730d8 100644 --- a/test/test_words_to_number.dart +++ b/test/test_words_to_number.dart @@ -66,6 +66,13 @@ void main() { ), '٤٥۰۰۰۰', ); + expect( + wordsToNumberString( + 'نه صد و هشتاد و هفت', + digits: DigitLocale.ar, + ), + '۹۸۷', + ); }); test('test wordsToNumber function: with ordinal words', () { expect( @@ -98,15 +105,23 @@ void main() { ); expect(wordsToNumber('منفی سه هزارمین'), -3000); expect(wordsToNumber('منفی سه هزارم'), -3000); - expect(wordsToNumberString('منفی سه هزارمین'), isNot(equals('-3000'))); - expect(wordsToNumberString('منفی سه هزارمین').length, 5); + expect(wordsToNumberString('منفی سه هزارمین'), equals('-3000')); + expect(wordsToNumberString('منفی سه هزارمین')?.length, 5); expect(wordsToNumber('منفی سی اُم'), -30); expect(wordsToNumber('سی و سوم'), 33); }); - test('test wordsToNumber function: empty value', () { + test('test wordsToNumber function: empty & invalid value', () { + expect( + wordsToNumberString('', digits: DigitLocale.fa)?.addComma, + equals(null), + ); + expect( + wordsToNumber(''), + equals(null), + ); expect( - wordsToNumberString('', digits: DigitLocale.fa).addComma, - equals(''), + wordsToNumberString('متن بدون عدد', digits: DigitLocale.fa)?.addComma, + equals('0'), ); }); });