diff --git a/README.md b/README.md index f811471..053df8c 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ phonenumbers [![Build Status](https://travis-ci.org/nyaruka/phonenumbers.svg?br golang port of Google's libphonenumber, forked from [libphonenumber from ttacon](https://github.com/ttacon/libphonenumber) which in turn is a port of the original [Java library](https://github.com/googlei18n/libphonenumber/tree/master/java/libphonenumber/src/com/google/i18n/phonenumbers). -You can see a live demo of the number parsing of the master branch of this library at [https://phonenumbers.temba.io/](https://phonenumbers.temba.io) +You can see a live demo of the number parsing of the master branch of this library at [https://phonenumbers.temba.io/](https://phonenumbers.temba.io) Compare results with the official [Google Java version](https://rawgit.com/googlei18n/libphonenumber/master/javascript/i18n/phonenumbers/demo-compiled.html). This fork fixes quite a few bugs and more closely follows the official Java implementation. It also adds the `buildmetadata` cmd to allow for rebuilding the metadata protocol buffers, country code to region maps and timezone prefix maps. We keep this library up to date with the upstream Google repo as metadata changes take place, usually no more than a few days behind official Google releases. diff --git a/builder.go b/builder.go index 02f0bea..074af10 100644 --- a/builder.go +++ b/builder.go @@ -512,7 +512,7 @@ func setPossibleLengthsGeneralDesc(generalDesc *PhoneNumberDesc, metadataId stri } else { populatePossibleLengthSets([]*PhoneNumberDescE{data.ShortCode}, lengths, localOnlyLengths) if len(localOnlyLengths) > 0 { - panic(fmt.Errorf("Found local-only lengths in short-number metadata")) + panic(fmt.Errorf("found local-only lengths in short-number metadata")) } } setPossibleLengths(lengths, localOnlyLengths, nil, generalDesc) diff --git a/cmd/buildmetadata/main.go b/cmd/buildmetadata/main.go index ad386ac..efeb391 100644 --- a/cmd/buildmetadata/main.go +++ b/cmd/buildmetadata/main.go @@ -211,7 +211,7 @@ func writeIntStringArrayMap(path string, varName string, prefixMap map[int][]str // we write our key / value pairs as a varint of the difference of the previous prefix // and a uint16 of the value index last := 0 - intBuf := make([]byte, 6, 6) + intBuf := make([]byte, 6) for _, key := range keys { // first write our prefix diff := key - last @@ -369,7 +369,7 @@ func buildPrefixData(build *prefixBuild) { // we write our prefix / value pairs as a varint of the difference of the previous prefix // and a uint16 of the value index last := 0 - intBuf := make([]byte, 6, 6) + intBuf := make([]byte, 6) for _, prefix := range prefixes { value := mappings[prefix] valueIntern := internMappings[value] diff --git a/phonenumbers.go b/phonenumbers.go index 93ffe26..ec90bf0 100644 --- a/phonenumbers.go +++ b/phonenumbers.go @@ -375,24 +375,24 @@ var ( VALID_PHONE_NUMBER_PATTERN = regexp.MustCompile( "^(" + VALID_PHONE_NUMBER + "(?:" + EXTN_PATTERNS_FOR_PARSING + ")?)$") - NON_DIGITS_PATTERN = regexp.MustCompile("(\\D+)") - DIGITS_PATTERN = regexp.MustCompile("(\\d+)") + NON_DIGITS_PATTERN = regexp.MustCompile(`(\D+)`) + DIGITS_PATTERN = regexp.MustCompile(`(\d+)`) // The FIRST_GROUP_PATTERN was originally set to $1 but there are some // countries for which the first group is not used in the national // pattern (e.g. Argentina) so the $1 group does not match correctly. // Therefore, we use \d, so that the first group actually used in the // pattern will be matched. - FIRST_GROUP_PATTERN = regexp.MustCompile("(\\$\\d)") - NP_PATTERN = regexp.MustCompile("\\$NP") - FG_PATTERN = regexp.MustCompile("\\$FG") - CC_PATTERN = regexp.MustCompile("\\$CC") + FIRST_GROUP_PATTERN = regexp.MustCompile(`(\$\d)`) + NP_PATTERN = regexp.MustCompile(`\$NP`) + FG_PATTERN = regexp.MustCompile(`\$FG`) + CC_PATTERN = regexp.MustCompile(`\$CC`) // A pattern that is used to determine if the national prefix // formatting rule has the first group only, i.e., does not start // with the national prefix. Note that the pattern explicitly allows // for unbalanced parentheses. - FIRST_GROUP_ONLY_PREFIX_PATTERN = regexp.MustCompile("\\(?\\$1\\)?") + FIRST_GROUP_ONLY_PREFIX_PATTERN = regexp.MustCompile(`\(?\$1\)?`) REGION_CODE_FOR_NON_GEO_ENTITY = "001" ) @@ -1528,7 +1528,6 @@ func FormatInOriginalFormat(number *PhoneNumber, regionCallingFrom string) strin numFormatCopy.NationalPrefixFormattingRule = nil var numberFormats = []*NumberFormat{numFormatCopy} formattedNumber = FormatByPattern(number, NATIONAL, numberFormats) - break } rawInput = number.GetRawInput() // If no digit is inserted/removed/modified as a result of our @@ -2391,7 +2390,7 @@ func testNumberLength(number string, metadata *PhoneMetadata, numberType PhoneNu } // We skip the first element; we've already checked it. - for _, l := range possibleLengths[1:len(possibleLengths)] { + for _, l := range possibleLengths[1:] { if l == actualLength { return IS_POSSIBLE } @@ -2522,8 +2521,8 @@ func extractCountryCode(fullNumber, nationalNumber *Builder) int { return 0 } -var ErrTooShortAfterIDD = errors.New("Phone number had an IDD, but " + - "after this was not long enough to be a viable phone number.") +var ErrTooShortAfterIDD = errors.New("phone number had an IDD, but " + + "after this was not long enough to be a viable phone number") // Tries to extract a country calling code from a number. This method will // return zero if no country calling code is considered to be present. @@ -2860,8 +2859,8 @@ func setItalianLeadingZerosForPhoneNumber( var ( ErrInvalidCountryCode = errors.New("invalid country code") - ErrNotANumber = errors.New("The phone number supplied is not a number.") - ErrTooShortNSN = errors.New("The string supplied is too short to be a phone number.") + ErrNotANumber = errors.New("the phone number supplied is not a number") + ErrTooShortNSN = errors.New("the string supplied is too short to be a phone number") ) // Parses a string and fills up the phoneNumber. This method is the same @@ -2988,7 +2987,7 @@ func parseHelper( return nil } -var ErrNumTooLong = errors.New("The string supplied is too long to be a phone number.") +var ErrNumTooLong = errors.New("the string supplied is too long to be a phone number") // Converts numberToParse to a form that we can parse and write it to // nationalNumber if it is written in RFC3966; otherwise extract a possible @@ -3290,10 +3289,10 @@ func init() { } // Create our sync.Onces for each of our languages for carriers - for lang, _ := range carrierMapData { + for lang := range carrierMapData { carrierOnces[lang] = &sync.Once{} } - for lang, _ := range geocodingMapData { + for lang := range geocodingMapData { geocodingOnces[lang] = &sync.Once{} } } diff --git a/phonenumbers_test.go b/phonenumbers_test.go index 9c1efd2..a45fcb5 100644 --- a/phonenumbers_test.go +++ b/phonenumbers_test.go @@ -768,10 +768,10 @@ func TestGetLengthOfGeographicalAreaCode(t *testing.T) { } func TestGetCountryMobileToken(t *testing.T) { - if "1" != GetCountryMobileToken(GetCountryCodeForRegion("MX")) { + if GetCountryMobileToken(GetCountryCodeForRegion("MX")) != "1" { t.Error("Mexico should have a mobile token == \"1\"") } - if "" != GetCountryMobileToken(GetCountryCodeForRegion("SE")) { + if GetCountryMobileToken(GetCountryCodeForRegion("SE")) != "" { t.Error("Sweden should have a mobile token") } } @@ -838,13 +838,13 @@ func TestGetExampleNumberForNonGeoEntity(t *testing.T) { } func TestNormalizeDigitsOnly(t *testing.T) { - if "03456234" != NormalizeDigitsOnly("034-56&+a#234") { + if NormalizeDigitsOnly("034-56&+a#234") != "03456234" { t.Errorf("didn't fully normalize digits only") } } func TestNormalizeDiallableCharsOnly(t *testing.T) { - if "03*456+234" != normalizeDiallableCharsOnly("03*4-56&+a#234") { + if normalizeDiallableCharsOnly("03*4-56&+a#234") != "03*456+234" { t.Error("did not correctly remove non-diallable characters") } } diff --git a/serialize.go b/serialize.go index f938751..bbf8990 100644 --- a/serialize.go +++ b/serialize.go @@ -32,7 +32,7 @@ func loadPrefixMap(data string) (*intStringMap, error) { } // then our values - valueBytes := make([]byte, valueSize, valueSize) + valueBytes := make([]byte, valueSize) n, err := reader.Read(valueBytes) if uint32(n) < valueSize { return nil, fmt.Errorf("unable to read all values: %v", err) @@ -103,7 +103,7 @@ func loadIntStringArrayMap(data string) (*intStringArrayMap, error) { } // then our values - valueBytes := make([]byte, valueSize, valueSize) + valueBytes := make([]byte, valueSize) n, err := reader.Read(valueBytes) if uint32(n) < valueSize { return nil, fmt.Errorf("unable to read all values: %v", err) @@ -136,7 +136,7 @@ func loadIntStringArrayMap(data string) (*intStringArrayMap, error) { return nil, err } - keyValues := make([]string, valueCount, valueCount) + keyValues := make([]string, valueCount) for i := 0; i < int(valueCount); i++ { var valueIntern uint16 err = binary.Read(reader, binary.LittleEndian, &valueIntern)