Skip to content

Commit

Permalink
Done with Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devnla committed Jun 16, 2024
0 parents commit f7f0e4e
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/devnla/mm_phone

go 1.22.2
116 changes: 116 additions & 0 deletions phone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package mmphone

import (
"regexp"
"strings"
)

const (
OOREDOO = "Ooredoo"
ATOM = "Telenor"
MPT = "MPT"
MYTEL = "MyTel"
UNKNOWN = "Unknown"

GSM_TYPE = "GSM"
WCDMA_TYPE = "WCDMA"
CDMA_450_TYPE = "CDMA 450 MHz"
CDMA_800_TYPE = "CDMA 800 MHz"
)

var (
OoredooRegex = regexp.MustCompile(`^(09|\+?959)9(7|6)\d{7}$`)
AtomRegex = regexp.MustCompile(`^(09|\+?959)7(9|8|7)\d{7}$`)
MptRegex = regexp.MustCompile(`^(09|\+?959)(5\d{6}|4\d{7,8}|2\d{6,8}|3\d{7,8}|6\d{6}|8\d{6}|7\d{7}|9(0|1|9)\d{5,6})$`)
MyTelRegex = regexp.MustCompile(`^(09|\+?959)6([5-9])\d{7}$`)
MyanmarPhoneRegex = regexp.MustCompile(`^(09|\+?950?9|\+?95950?9)\d{7,9}$`)
)

type MyanmarPhone struct{}

func NewMyanmarPhone() *MyanmarPhone {
return &MyanmarPhone{}
}

func (mm *MyanmarPhone) IsValidMyanmarPhone(phoneNumber string) bool {
phoneNumber = mm.SanitizePhoneNumber(phoneNumber)
return MyanmarPhoneRegex.MatchString(phoneNumber)
}

func (mm *MyanmarPhone) SanitizePhoneNumber(phoneNumber string) string {
phoneNumber = mm.convertBurmeseNumerals(phoneNumber)
phoneNumber = strings.TrimSpace(phoneNumber)
phoneNumber = strings.ReplaceAll(phoneNumber, " ", "")
phoneNumber = strings.ReplaceAll(phoneNumber, "-", "")
phoneNumber = strings.ReplaceAll(phoneNumber, "+", "")

if matched, _ := regexp.MatchString(`^\+?950?9\d+$`, phoneNumber); matched {
phoneNumber = strings.Replace(phoneNumber, "959", "09", 1)
phoneNumber = strings.Replace(phoneNumber, "9509", "09", 1)
}

return phoneNumber
}

func (mm *MyanmarPhone) convertBurmeseNumerals(phoneNumber string) string {
var converted strings.Builder
for _, r := range phoneNumber {
if r >= '၀' && r <= '၉' {
converted.WriteRune('0' + (r - '၀'))
} else {
converted.WriteRune(r)
}
}
return converted.String()
}

func (mm *MyanmarPhone) GetTelecomName(phoneNumber string) string {
if !mm.IsValidMyanmarPhone(phoneNumber) {
return UNKNOWN
}

phoneNumber = mm.SanitizePhoneNumber(phoneNumber)

switch {
case OoredooRegex.MatchString(phoneNumber):
return OOREDOO
case AtomRegex.MatchString(phoneNumber):
return ATOM
case MptRegex.MatchString(phoneNumber):
return MPT
case MyTelRegex.MatchString(phoneNumber):
return MYTEL
default:
return UNKNOWN
}
}

func (mm *MyanmarPhone) GetPhoneNetworkType(phoneNumber string) string {
if !mm.IsValidMyanmarPhone(phoneNumber) {
return UNKNOWN
}

phoneNumber = mm.SanitizePhoneNumber(phoneNumber)

wcdmaRe := regexp.MustCompile(`^(09|\+?959)(55\d{5}|25[2-4]\d{6}|26\d{7}|4(4|5|6)\d{7})$`)
cdma450Re := regexp.MustCompile(`^(09|\+?959)(8\d{6}|6\d{6}|49\d{6})$`)
cdma800Re := regexp.MustCompile(`^(09|\+?959)(3\d{7}|73\d{6}|91\d{6})$`)

switch {
case OoredooRegex.MatchString(phoneNumber), AtomRegex.MatchString(phoneNumber), MyTelRegex.MatchString(phoneNumber):
return GSM_TYPE
case MptRegex.MatchString(phoneNumber):
switch {
case wcdmaRe.MatchString(phoneNumber):
return WCDMA_TYPE
case cdma450Re.MatchString(phoneNumber):
return CDMA_450_TYPE
case cdma800Re.MatchString(phoneNumber):
return CDMA_800_TYPE
default:
return GSM_TYPE
}
default:
return UNKNOWN
}
}
111 changes: 111 additions & 0 deletions phone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package mmphone

import (
"testing"
)

func TestSanitizePhoneNumber(t *testing.T) {
tests := []struct {
input string
expected string
}{
{" 09 777 123456 ", "09777123456"},
{"+95 9 777 123456", "09777123456"},
{"9595 777 123456", "095777123456"},
{"၀၉ ၇၇၇ ၁၂၃၄၅၆", "09777123456"},
}

mm := NewMyanmarPhone()

for _, test := range tests {
result := mm.SanitizePhoneNumber(test.input)
if result != test.expected {
t.Errorf("SanitizePhoneNumber(%s) = %s; expected %s", test.input, result, test.expected)
}
}
}

func TestIsValidMyanmarPhone(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"09777123456", true},
{"+959777123456", true},
{"၀၉၇၇၇၁၂၃၄၅၆", true},
{"123456", false},
{"+959812345", false},
}

mm := NewMyanmarPhone()

for _, test := range tests {
result := mm.IsValidMyanmarPhone(test.input)
if result != test.expected {
t.Errorf("IsValidMyanmarPhone(%s) = %v; expected %v", test.input, result, test.expected)
}
}
}

func TestGetTelecomName(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"09977123456", OOREDOO},
{"+959775541794", ATOM},
{"၀၉၅၁၂၃၄၅၆", MPT},
{"09156123456", UNKNOWN},
{"09678123456", MYTEL},
}

mm := NewMyanmarPhone()

for _, test := range tests {
result := mm.GetTelecomName(test.input)
if result != test.expected {
t.Errorf("GetTelecomName(%s) = %s; expected %s", test.input, result, test.expected)
}
}
}

func TestGetPhoneNetworkType(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"09777123456", GSM_TYPE},
{"+959781234567", GSM_TYPE},
{"၀၉၅၅၁၂၃၄၆", WCDMA_TYPE},
{"0949123456", CDMA_450_TYPE},
{"0937123456", CDMA_800_TYPE},
}

mm := NewMyanmarPhone()

for _, test := range tests {
result := mm.GetPhoneNetworkType(test.input)
if result != test.expected {
t.Errorf("GetPhoneNetworkType(%s) = %s; expected %s", test.input, result, test.expected)
}
}
}

func TestConvertBurmeseNumerals(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"၀၉၇၇၇၁၂၃၄၅၆", "09777123456"},
{"၉၅၉၅၇၇၇၁၂၃၄၅၆", "9595777123456"},
}

mm := NewMyanmarPhone()

for _, test := range tests {
result := mm.convertBurmeseNumerals(test.input)
if result != test.expected {
t.Errorf("convertBurmeseNumerals(%s) = %s; expected %s", test.input, result, test.expected)
}
}
}

0 comments on commit f7f0e4e

Please sign in to comment.