diff --git a/README.md b/README.md index 55a00e0..be39027 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,10 @@ print(number); // Outputs: 134 ``` # Contributions Contributions to the Geez SDK are welcomed and encouraged! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository. + +# Contributors +Thanks to the following people who have contributed to this project: + +Natinael Fikadu - [@mrnpro](https://github.com/mrnpro) + +Dagim Mesfin - [@DagmMesfin](https://github.com/DagmMesfin) diff --git a/lib/src/geez_to_number.dart b/lib/src/geez_to_number.dart index 77cf906..8430871 100644 --- a/lib/src/geez_to_number.dart +++ b/lib/src/geez_to_number.dart @@ -1,4 +1,5 @@ import 'constants.dart'; +import "dart:math"; /// A utility extension for converting Geez numbers to Arabic numbers. /// @@ -26,27 +27,12 @@ extension GeezToArabicConvertor on String { return result; } - // Split the String into individual characters. - List separatedGeezs = _splitNumbersStrings(this); - // Calculate the Arabic representation of the Geez number. - final arabicRepresentation = _driveArabicRepresentation(separatedGeezs); + final arabicRepresentation = _driveArabicRepresentation(this); return arabicRepresentation; } - /// Splits a string into individual characters. - /// - /// Returns a List of individual character strings. - List _splitNumbersStrings(String string) { - List splitedStrings = []; - for (var rune in string.runes) { - var character = String.fromCharCode(rune); - splitedStrings.add(character); - } - return splitedStrings; - } - /// Performs a quick reverse find operation on the `geezNumbers` map. /// /// Returns the corresponding Arabic numeric value of the Geez number if found, @@ -59,42 +45,54 @@ extension GeezToArabicConvertor on String { /// Calculates the Arabic representation of the Geez number. /// /// Returns the Arabic numeric representation of the Geez number. - int _driveArabicRepresentation(List separatedGeezs) { - int sum = 0; - num? indexToJump; - for (var i = 0; i < separatedGeezs.length; i++) { - // `indexToJump` is the next index we multiplied with the last index, - // so we don't have to check next time as we have already used it. - if (indexToJump == i) { - continue; + int _driveArabicRepresentation(String geezNum) { + List elfyosh = ['፻፼፼፼', '፼፼፼', '፻፼፼', '፼፼', '፻፼', '፼', '፻', '']; + List ando = ['፩', '፪', '፫', '፬', '፭', '፮', '፯', '፰', '፱']; + List asro = ['፲', '፳', '፴', '፵', '፶', '፷', '፸', '፹', '፺']; + int numeric = 0; + int expo = 14; + int mult = (pow(10, expo)).toInt(); + String beforeElf = ''; + String asrand = ''; + for(int i = 0; i < elfyosh.length; i++){ + if(i < elfyosh.length - 1){ + if(geezNum.contains(elfyosh[i])){ + beforeElf = geezNum.substring(0, geezNum.indexOf(elfyosh[i]) + elfyosh[i].length); + geezNum = geezNum.substring(geezNum.indexOf(elfyosh[i]) + elfyosh[i].length, geezNum.length); + if(beforeElf.length - elfyosh[i].length == 2){ + asrand = beforeElf.substring(0, 2); + numeric += ((((asro.indexOf(asrand[0]) + 1) * 10) + (ando.indexOf(asrand[1]) + 1)) * mult).toInt(); + } + else if(beforeElf.length - elfyosh[i].length == 1){ + asrand = beforeElf[0]; + if(ando.contains(asrand)){ + numeric += ((ando.indexOf(asrand) + 1) * mult).toInt(); + } + else if(asro.contains(asrand)){ + numeric += (((asro.indexOf(asrand) + 1) * 10) * mult).toInt(); + } + } + else{ + numeric += mult; + } + } + expo -= 2; + mult = (pow(10, expo)).toInt(); } - - // The current index Geez. - final currentGeez = separatedGeezs[i]; - final nextIndex = i + 1; - - String? nextGeez; - // Check if the index exists in the `separatedGeezs` List to prevent an index out of bounds exception. - if (nextIndex >= 0 && nextIndex < separatedGeezs.length) { - nextGeez = separatedGeezs[i + 1]; - } - - // Search the key using value and get the current ASCII value. - final asciiCurrent = geezNumbers.keys.firstWhere( - (key) => geezNumbers[key] == currentGeez, - orElse: () => -1); - - // Search the key using value and get the next ASCII value. - final asciiNext = hundredTo1M.keys - .firstWhere((key) => hundredTo1M[key] == nextGeez, orElse: () => -1); - - if (asciiNext != -1) { - indexToJump = i + 1; - sum += (asciiNext * asciiCurrent); - } else { - sum += asciiCurrent; + else if(i == elfyosh.length - 1){ + if(geezNum.length == 2){ + numeric += (((asro.indexOf(geezNum[0]) + 1) * 10) + (ando.indexOf(geezNum[1]) + 1)).toInt(); + } + else if(geezNum.length == 1){ + if(ando.contains(geezNum)){ + numeric += ((ando.indexOf(geezNum) + 1)).toInt(); + } + else if(asro.contains(geezNum)){ + numeric += ((asro.indexOf(geezNum) + 1) * 10).toInt(); + } + } } } - return sum; - } + return numeric; +} } diff --git a/test/geez_test.dart b/test/geez_test.dart index dd73f8e..6134a9e 100644 --- a/test/geez_test.dart +++ b/test/geez_test.dart @@ -33,15 +33,25 @@ void main() { expect(convertedToGeez, mockGeezNumbers[i]); } }), - test('Convert Geez to Number - Specific Number', () { + test('Convert Geez to Number - Specific Number #1', () { // Convert a specific Geez representation to number num number = '፻፴፬'.toArabic(); expect(number, 134); }), + test('Convert Geez to Number - Specific Number #2', () { + // Convert a specific Geez representation to number + num number = '፻፼፩'.toArabic(); + expect(number, 1000001); + }), + test('Convert Geez to Number - Specific Number #3', () { + // Convert a specific Geez representation to number + num number = '፼፺፰፻፸፮'.toArabic(); + expect(number, 19876); + }), test('Convert Geez to Number - Multiple Numbers', () { // Convert multiple Geez representations to numbers for (var i = 0; i < numbersList.length; i++) { - num convertedFromGeez = mockGeezNumber[i].toArabic(); + num convertedFromGeez = mockGeezNumbers[i].toArabic(); expect(convertedFromGeez, numbersList[i]); } }),