Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The geez to num converter is fixed my way. Check it out! #4

Merged
merged 18 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
100 changes: 49 additions & 51 deletions lib/src/geez_to_number.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'constants.dart';
import "dart:math";

/// A utility extension for converting Geez numbers to Arabic numbers.
///
Expand Down Expand Up @@ -26,27 +27,12 @@ extension GeezToArabicConvertor on String {
return result;
}

// Split the String into individual characters.
List<String> 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<String> _splitNumbersStrings(String string) {
List<String> 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,
Expand All @@ -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<String> 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<String> elfyosh = ['፻፼፼፼', '፼፼፼', '፻፼፼', '፼፼', '፻፼', '፼', '፻', ''];
List<String> ando = ['፩', '፪', '፫', '፬', '፭', '፮', '፯', '፰', '፱'];
List<String> 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;
}
}
14 changes: 12 additions & 2 deletions test/geez_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}),
Expand Down
Loading