Skip to content

Commit ef14b7f

Browse files
committed
Export IsNumberMatchWithNumbers and IsNumberMatchWithOneNumber.
Both corresponding methods in libphonenumber (Java) are public, which allows to pass PhoneNumber objects with an explicitly set region. Thus we export the corresponding functions here, too.
1 parent cb44396 commit ef14b7f

File tree

3 files changed

+104
-9
lines changed

3 files changed

+104
-9
lines changed

matcher.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func ContainsOnlyValidXChars(number *PhoneNumber, candidate string) bool {
3030
// This is the carrier code case, in which the 'X's
3131
// always precede the national significant number.
3232
index++
33-
if isNumberMatchWithOneNumber(number, candidate[index:]) != NSN_MATCH {
33+
if IsNumberMatchWithOneNumber(number, candidate[index:]) != NSN_MATCH {
3434
return false
3535
}
3636
// This is the extension sign case, in which the 'x'

phonenumbers.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -3072,7 +3072,7 @@ func buildNationalNumberForParsing(
30723072
// Returns NO_MATCH otherwise.
30733073
// For example, the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH.
30743074
// The numbers +1 345 657 1234 and 345 657 are a NO_MATCH.
3075-
func isNumberMatchWithNumbers(firstNumberIn, secondNumberIn *PhoneNumber) MatchType {
3075+
func IsNumberMatchWithNumbers(firstNumberIn, secondNumberIn *PhoneNumber) MatchType {
30763076
// Make copies of the phone number so that the numbers passed in are not edited.
30773077
var firstNumber, secondNumber *PhoneNumber
30783078
firstNumber = &PhoneNumber{}
@@ -3159,14 +3159,14 @@ func isNationalNumberSuffixOfTheOther(firstNumber, secondNumber *PhoneNumber) bo
31593159
func IsNumberMatch(firstNumber, secondNumber string) MatchType {
31603160
firstNumberAsProto, err := Parse(firstNumber, UNKNOWN_REGION)
31613161
if err == nil {
3162-
return isNumberMatchWithOneNumber(firstNumberAsProto, secondNumber)
3162+
return IsNumberMatchWithOneNumber(firstNumberAsProto, secondNumber)
31633163
} else if err != ErrInvalidCountryCode {
31643164
return NOT_A_NUMBER
31653165
}
31663166

31673167
secondNumberAsProto, err := Parse(secondNumber, UNKNOWN_REGION)
31683168
if err == nil {
3169-
return isNumberMatchWithOneNumber(secondNumberAsProto, firstNumber)
3169+
return IsNumberMatchWithOneNumber(secondNumberAsProto, firstNumber)
31703170
} else if err != ErrInvalidCountryCode {
31713171
return NOT_A_NUMBER
31723172
}
@@ -3180,19 +3180,19 @@ func IsNumberMatch(firstNumber, secondNumber string) MatchType {
31803180
if err != nil {
31813181
return NOT_A_NUMBER
31823182
}
3183-
return isNumberMatchWithNumbers(&firstNumberProto, &secondNumberProto)
3183+
return IsNumberMatchWithNumbers(&firstNumberProto, &secondNumberProto)
31843184
}
31853185

31863186
// Takes two phone numbers and compares them for equality. This is a
31873187
// convenience wrapper for IsNumberMatch(PhoneNumber, PhoneNumber). No
31883188
// default region is known.
3189-
func isNumberMatchWithOneNumber(
3189+
func IsNumberMatchWithOneNumber(
31903190
firstNumber *PhoneNumber, secondNumber string) MatchType {
31913191
// First see if the second number has an implicit country calling
31923192
// code, by attempting to parse it.
31933193
secondNumberAsProto, err := Parse(secondNumber, UNKNOWN_REGION)
31943194
if err == nil {
3195-
return isNumberMatchWithNumbers(firstNumber, secondNumberAsProto)
3195+
return IsNumberMatchWithNumbers(firstNumber, secondNumberAsProto)
31963196
}
31973197
if err != ErrInvalidCountryCode {
31983198
return NOT_A_NUMBER
@@ -3209,7 +3209,7 @@ func isNumberMatchWithOneNumber(
32093209
if err != nil {
32103210
return NOT_A_NUMBER
32113211
}
3212-
match := isNumberMatchWithNumbers(
3212+
match := IsNumberMatchWithNumbers(
32133213
firstNumber, secondNumberWithFirstNumberRegion)
32143214
if match == EXACT_MATCH {
32153215
return NSN_MATCH
@@ -3223,7 +3223,7 @@ func isNumberMatchWithOneNumber(
32233223
if err != nil {
32243224
return NOT_A_NUMBER
32253225
}
3226-
return isNumberMatchWithNumbers(firstNumber, secondNumberProto)
3226+
return IsNumberMatchWithNumbers(firstNumber, secondNumberProto)
32273227
}
32283228
}
32293229

phonenumbers_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,101 @@ func TestSetItalianLeadinZerosForPhoneNumber(t *testing.T) {
827827
}
828828
}
829829

830+
func TestIsNumberMatchWithNumbers(t *testing.T) {
831+
tcs := []struct {
832+
num1 string
833+
reg1 string
834+
num2 string
835+
reg2 string
836+
expected MatchType
837+
}{
838+
{
839+
"+49 721 123456", "DE", "0049 721 123456", "DE", EXACT_MATCH,
840+
},
841+
{
842+
"721 123456", "DE", "721 123456", "DE", EXACT_MATCH,
843+
},
844+
{
845+
"+49 721 123456", "DE", "0721-123456", "DE", EXACT_MATCH,
846+
},
847+
{
848+
"+49 721 123456", "DE", "0049 721-123456", "DE", EXACT_MATCH,
849+
},
850+
{
851+
"+49 721 123456", "DE", "0049 0721 123456", "DE", EXACT_MATCH,
852+
},
853+
{
854+
"721 123456", "DE", "+49 721 123456", "", EXACT_MATCH,
855+
},
856+
{
857+
"0721-123456", "DE", "+49 721 123456", "DE", EXACT_MATCH,
858+
},
859+
{
860+
"721123456", "ES", "+34 721 123456", "ES", EXACT_MATCH,
861+
},
862+
{
863+
"0721 123456", "DE", "+49 721 123456", "ZZ", EXACT_MATCH,
864+
},
865+
{
866+
"0721-123456", "", "+49 721 123456", "DE", NO_MATCH,
867+
},
868+
{
869+
"0721-123456", "ES", "+49 721 123456", "DE", NO_MATCH,
870+
},
871+
{
872+
"0721 123456", "ES", "0721 123456", "DE", NO_MATCH,
873+
},
874+
{
875+
"123456", "DE", "0721 123456", "DE", SHORT_NSN_MATCH,
876+
},
877+
{
878+
"0721 123456", "", "123456", "", SHORT_NSN_MATCH,
879+
},
880+
}
881+
882+
for _, tc := range tcs {
883+
p1, _ := Parse(tc.num1, tc.reg1)
884+
p2, _ := Parse(tc.num2, tc.reg2)
885+
result := IsNumberMatchWithNumbers(p1, p2)
886+
if result != tc.expected {
887+
t.Errorf(`"%s"(%s) == "%s"(%s) returned %d when expecting %d`, tc.num1, tc.reg1, tc.num2, tc.reg2, result, tc.expected)
888+
}
889+
}
890+
}
891+
892+
func TestIsNumberMatchWithOneNumber(t *testing.T) {
893+
tcs := []struct {
894+
num1 string
895+
reg1 string
896+
num2 string
897+
expected MatchType
898+
}{
899+
{
900+
"+49 721 123456", "DE", "+49721123456", EXACT_MATCH,
901+
},
902+
{
903+
"+49 721 123456", "DE", "0049 721 123456", NSN_MATCH,
904+
},
905+
{
906+
"6502530000", "US", "1-650-253-0000", NSN_MATCH,
907+
},
908+
{
909+
"123456", "DE", "+49 0721 123456", SHORT_NSN_MATCH,
910+
},
911+
{
912+
"0721 123456", "ES", "+43 721 123456", NO_MATCH,
913+
},
914+
}
915+
916+
for _, tc := range tcs {
917+
p1, _ := Parse(tc.num1, tc.reg1)
918+
result := IsNumberMatchWithOneNumber(p1, tc.num2)
919+
if result != tc.expected {
920+
t.Errorf(`"%s"(%s) == "%s" returned %d when expecting %d`, tc.num1, tc.reg1, tc.num2, result, tc.expected)
921+
}
922+
}
923+
}
924+
830925
////////// Copied from java-libphonenumber
831926
/**
832927
* Unit tests for PhoneNumberUtil.java

0 commit comments

Comments
 (0)