From 2597f1538ed9a76fdfd1450106b952691b5ff8e1 Mon Sep 17 00:00:00 2001 From: iman Date: Mon, 14 Oct 2024 13:18:59 +0330 Subject: [PATCH 1/2] feat [digits]: add p2e and e2p functions. add documentation for p2e e2p. add tests for p2e e2p. --- README.md | 27 ++++++++++++++++----------- digit/digit_test.go | 31 ++++++++++++++++++++++++++++++- digit/english_to_persian.go | 28 ++++++++++++++++++++++++++++ digit/persian_to_english.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 digit/english_to_persian.go create mode 100644 digit/persian_to_english.go diff --git a/README.md b/README.md index eca2878..7132d80 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,10 @@ - [x] Digits - [x] Validate Bank card number. - [x] Find Bank's name by Card number. -- [X] Check Iranian Sheba(IBAN) validation and recognize bank information by sheba code. -- [X] Add and remove commas to numbers. -- [X] Find city and province name by national code(code-e Melli). -- [X] Validate Iranian national number(code-e Melli). +- [x] Check Iranian Sheba(IBAN) validation and recognize bank information by sheba code. +- [x] Add and remove commas to numbers. +- [x] Find city and province name by national code(code-e Melli). +- [x] Validate Iranian national number(code-e Melli). #### How to use it? @@ -40,7 +40,7 @@ And pass it ##### Bills ```go -result := bills.GetBillType(params) // تلفن ثابت +result := bills.GetBillType(params) // تلفن ثابت amount := bills.GetCurrency(params) //120000 barcode := bills.GetBarCode(params) // 111775320014000012070160 verify := bills.VerifyBillID(params) //true @@ -50,9 +50,11 @@ verify := bills.VerifyBillID(params) //true ##### Digits ```go -num2wordFa := digits.DigitToWord("۱۵۶۷۸۹") // صد پنجاه و شش هزار هفتصد هشتاد و نه -num2wordEn := digits.DigitToWord("156789") // صد پنجاه و شش هزار هفتصد هشتاد و نه -Negative := digits.DigitToWord("-156789") // منفی صد پنجاه و شش هزار هفتصد هشتاد و نه +num2wordFa := digits.DigitToWord("۱۵۶۷۸۹") // صد پنجاه و شش هزار هفتصد هشتاد و نه +num2wordEn := digits.DigitToWord("156789") // صد پنجاه و شش هزار هفتصد هشتاد و نه +Negative := digits.DigitToWord("-156789") // منفی صد پنجاه و شش هزار هفتصد هشتاد و نه +englishDigits := PersianToEnglish("۱۲۳۴۵۶۷۸۹۰") // 1234567890 +persianDigits := EnglishToPersian("1234567890") // ۱۲۳۴۵۶۷۸۹۰ ``` #### Bank @@ -71,6 +73,7 @@ bank,error := CardInfo("6219861034529007") // saman, nil ``` ###### Check Iranian Sheba + The types of results are : ```go @@ -99,11 +102,12 @@ sheba := shebaCode.IsSheba() // { false } addComma := digits.AddCommas(14555478854) removeComma := digits.RemoveCommas("4,555,522,212,12") -fmt.Printf("\n ADD COMMA : %v \n", addComma) // 14,555,478,854 -fmt.Printf("\n REMOVE COMMA : %v \n", removeComma)// 455552221212 +fmt.Printf("\n ADD COMMA : %v \n", addComma) // 14,555,478,854 +fmt.Printf("\n REMOVE COMMA : %v \n", removeComma)// 455552221212 ``` ###### Get Place and city By NationalID + ```go getPlaceByIranNationalId := city.GetPlaceByIranNationalId("0499370899") fmt.Printf("\n Result : %v \n", getPlaceByIranNationalId) @@ -111,10 +115,11 @@ fmt.Printf("\n Result : %v \n", getPlaceByIranNationalId) ``` ###### Validate Iranian national number(code-e Melli) + ```go verifyIranianNationalId := national_id.Validate("0067749828") verifyIranianNationalIdFalse := national_id.Validate("0684159415") fmt.Printf("\n Validate NationalID : %v \n", verifyIranianNationalId) // true fmt.Printf("\n Validate NationalIDFalse : %v \n", verifyIranianNationalIdFalse) // false -``` \ No newline at end of file +``` diff --git a/digit/digit_test.go b/digit/digit_test.go index c3b2246..952c5c6 100644 --- a/digit/digit_test.go +++ b/digit/digit_test.go @@ -1,6 +1,8 @@ package digit -import "testing" +import ( + "testing" +) func TestDigitToWord(t *testing.T) { num2word1 := DigitToWord("۱۵۶۷۸۹") @@ -37,3 +39,30 @@ func TestRemoveCommas(t *testing.T) { } } + +func TestEnglishToPersian(t *testing.T) { + // expect number 1234567890 to turn into ۱۲۳۴۵۶۷۸۹۰ + persianDigits := EnglishToPersian("1234567890") + if persianDigits != "۱۲۳۴۵۶۷۸۹۰" { + t.Errorf("Wrong Converting English Digits To Persian: %v", persianDigits) + } + + // expect misc characters to stay the same + persianDigitsWithMisc := EnglishToPersian("1!2@3#4$5%6^7&8*9(0)-=") + if persianDigitsWithMisc != "۱!۲@۳#۴$۵%۶^۷&۸*۹(۰)-=" { + t.Errorf("Wrong Converting Persian Digits To English: %v", persianDigitsWithMisc) + } +} + +func TestPersianToEnglish(t *testing.T) { + // expect number 1234567890 to turn into ۱۲۳۴۵۶۷۸۹۰ + englishDigits := PersianToEnglish("۱۲۳۴۵۶۷۸۹۰") + if englishDigits != "1234567890" { + t.Errorf("Wrong Converting Persian Digits To English: %v", englishDigits) + } + // expect misc characters to stay the same + englishDigitsWithMisc := PersianToEnglish("۱!۲@۳#۴$۵%۶^۷&۸*۹(۰)-=") + if englishDigitsWithMisc != "1!2@3#4$5%6^7&8*9(0)-=" { + t.Errorf("Wrong Converting Persian Digits To English: %v", englishDigitsWithMisc) + } +} diff --git a/digit/english_to_persian.go b/digit/english_to_persian.go new file mode 100644 index 0000000..9c9c89c --- /dev/null +++ b/digit/english_to_persian.go @@ -0,0 +1,28 @@ +package digit + +import "strings" + +var englishToPersianMap = map[rune]rune{ + '0': '۰', + '1': '۱', + '2': '۲', + '3': '۳', + '4': '۴', + '5': '۵', + '6': '۶', + '7': '۷', + '8': '۸', + '9': '۹', +} + +func EnglishToPersian(input string) string { + var result strings.Builder + for _, char := range input { + if engChar, found := englishToPersianMap[char]; found { + result.WriteRune(engChar) + } else { + result.WriteRune(char) + } + } + return result.String() +} diff --git a/digit/persian_to_english.go b/digit/persian_to_english.go new file mode 100644 index 0000000..7e3a1c3 --- /dev/null +++ b/digit/persian_to_english.go @@ -0,0 +1,28 @@ +package digit + +import "strings" + +var persianToEnglishMap = map[rune]rune{ + '۰': '0', + '۱': '1', + '۲': '2', + '۳': '3', + '۴': '4', + '۵': '5', + '۶': '6', + '۷': '7', + '۸': '8', + '۹': '9', +} + +func PersianToEnglish(input string) string { + var result strings.Builder + for _, char := range input { + if engChar, found := persianToEnglishMap[char]; found { + result.WriteRune(engChar) + } else { + result.WriteRune(char) + } + } + return result.String() +} From 9a2dc2b6a8c69561d08f148c6f72f093859dc1e1 Mon Sep 17 00:00:00 2001 From: iman Date: Mon, 14 Oct 2024 13:25:26 +0330 Subject: [PATCH 2/2] chore [digit]: rename a variable name in EglishToPersian --- digit/english_to_persian.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/digit/english_to_persian.go b/digit/english_to_persian.go index 9c9c89c..382644f 100644 --- a/digit/english_to_persian.go +++ b/digit/english_to_persian.go @@ -18,8 +18,8 @@ var englishToPersianMap = map[rune]rune{ func EnglishToPersian(input string) string { var result strings.Builder for _, char := range input { - if engChar, found := englishToPersianMap[char]; found { - result.WriteRune(engChar) + if persChar, found := englishToPersianMap[char]; found { + result.WriteRune(persChar) } else { result.WriteRune(char) }