-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: i18nify-go sdk with get_currency_symbol, convert_to_major & min…
…or_unit functionality (#187)
- Loading branch information
1 parent
e43a8cb
commit 283dce1
Showing
8 changed files
with
309 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/i18nify-go/modules/currency/conversion_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package currency | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertCurrency(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
currencyCode string | ||
amount interface{} | ||
minorExpectedValue float64 | ||
majorExpectedValue float64 | ||
expectedError string | ||
}{ | ||
{ | ||
name: "Valid USD Conversion", | ||
currencyCode: "USD", | ||
amount: 1234.0, | ||
minorExpectedValue: 123400, | ||
majorExpectedValue: 12.34, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Valid EUR Conversion", | ||
currencyCode: "EUR", | ||
amount: 4568, | ||
minorExpectedValue: 456800, | ||
majorExpectedValue: 45.68, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Valid GBP Conversion", | ||
currencyCode: "GBP", | ||
amount: 100.0, | ||
minorExpectedValue: 10000.00, | ||
majorExpectedValue: 1.00, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Valid Bahraini Dinar Conversion", | ||
currencyCode: "BHD", | ||
amount: 1000.0, | ||
minorExpectedValue: 1000000.00, | ||
majorExpectedValue: 1.00, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Valid Iraqi Dinar Conversion", | ||
currencyCode: "IQD", | ||
amount: 1000.0, | ||
minorExpectedValue: 1000000.00, | ||
majorExpectedValue: 1.00, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Valid Unidad Previsional Conversion", | ||
currencyCode: "UYW", | ||
amount: 10000.0, | ||
minorExpectedValue: 100000000.00, | ||
majorExpectedValue: 1.00, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Invalid Currency Code", | ||
currencyCode: "INR0", | ||
amount: 100.0, | ||
minorExpectedValue: 0, | ||
majorExpectedValue: 0, | ||
expectedError: "currency code 'INR0' not found", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Call both functions and capture errors | ||
majorActualValue, majorError := ConvertToMajorUnit(tt.currencyCode, tt.amount) | ||
minorActualValue, minorError := ConvertToMinorUnit(tt.currencyCode, tt.amount) | ||
|
||
// Compare the result values | ||
assert.InDelta(t, tt.majorExpectedValue, majorActualValue, 0.001, "unexpected major unit conversion") | ||
assert.InDelta(t, tt.minorExpectedValue, minorActualValue, 0.001, "unexpected minor unit conversion") | ||
|
||
// Check for errors | ||
if tt.expectedError != "" { | ||
// Ensure both major and minor conversions return the same error | ||
assert.Error(t, majorError) | ||
assert.Error(t, minorError) | ||
assert.EqualError(t, majorError, tt.expectedError) | ||
assert.EqualError(t, minorError, tt.expectedError) | ||
} else { | ||
// Ensure no errors are returned | ||
assert.NoError(t, majorError) | ||
assert.NoError(t, minorError) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package currency | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strconv" | ||
) | ||
|
||
// Converts given amount from minor (e.g., paise) to major unit (e.g., rupees) for a specified currency | ||
|
||
func ConvertToMajorUnit(code string, amount interface{}) (float64, error) { | ||
|
||
amountValue, err := ValidateAndConvertAmount(amount) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
currencyInfo, err := GetCurrencyInformation(code) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
minorUnit, err := strconv.ParseInt(currencyInfo.MinorUnit, 10, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("invalid minor unit for currency code '%s': %v", code, err) | ||
} | ||
|
||
minorUnitMultiplier := math.Pow(10, float64(minorUnit)) | ||
if minorUnitMultiplier <= 0 { | ||
return 0, fmt.Errorf("invalid minor unit multiplier for currency code '%s'", code) | ||
} | ||
|
||
majorUnitAmount := amountValue / minorUnitMultiplier | ||
|
||
return majorUnitAmount, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package currency | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strconv" | ||
) | ||
|
||
// Converts a given amount from major currency units (e.g., dollars) to minor units (e.g., cents) for a specified currency | ||
|
||
func ConvertToMinorUnit(code string, amount interface{}) (float64, error) { | ||
|
||
amountValue, err := ValidateAndConvertAmount(amount) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
currencyInfo, err := GetCurrencyInformation(code) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
minorUnit, err := strconv.ParseInt(currencyInfo.MinorUnit, 10, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("invalid minor unit for currency code '%s': %v", code, err) | ||
} | ||
|
||
minorUnitMultiplier := math.Pow(10, float64(minorUnit)) | ||
minorUnitAmount := amountValue * minorUnitMultiplier | ||
|
||
return minorUnitAmount, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package currency | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
// Validates and converts the input amount (float64, int, or string) to a float64, ensuring it is non-negative. | ||
|
||
func ValidateAndConvertAmount(amount interface{}) (float64, error) { | ||
var amountValue float64 | ||
switch v := amount.(type) { | ||
case float64: | ||
amountValue = v | ||
case int: | ||
amountValue = float64(v) | ||
case string: | ||
parsedAmount, err := strconv.ParseFloat(v, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("invalid amount value '%v': %v", v, err) | ||
} | ||
amountValue = parsedAmount | ||
default: | ||
return 0, fmt.Errorf("amount must be a number (float64, int, or string that can be parsed to float64), but got %v", reflect.TypeOf(amount)) | ||
} | ||
|
||
return amountValue, nil | ||
} |