Skip to content

Commit

Permalink
feat(WordsToNumber): implemented the method body
Browse files Browse the repository at this point in the history
  • Loading branch information
payam-zahedi committed May 29, 2021
1 parent ae9ef62 commit 2c40641
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
21 changes: 19 additions & 2 deletions lib/src/core/words_to_number/words_to_number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
25 changes: 20 additions & 5 deletions test/test_words_to_number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ void main() {
),
'٤٥۰۰۰۰',
);
expect(
wordsToNumberString(
'نه صد و هشتاد و هفت',
digits: DigitLocale.ar,
),
'۹۸۷',
);
});
test('test wordsToNumber function: with ordinal words', () {
expect(
Expand Down Expand Up @@ -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'),
);
});
});
Expand Down

0 comments on commit 2c40641

Please sign in to comment.