Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PhoneNumberRange and numerical operators on PhoneNumber #2

Closed
wants to merge 4 commits into from

Conversation

bsutton
Copy link
Contributor

@bsutton bsutton commented Sep 7, 2021

I have the requirement to manage large no.s of phone number ranges.

This PR includes a new class PhoneNumberRange which can store a contiguous range of phone numbers.

New operators have been added to the PhoneNumber class to facilitate implementation of a ranges and numeric comparison of phone numbers.

Small improvements to the description of exceptions to make locating the data that caused the problem easier.

Added unit tests for PhoneNumberRange and the new operators in PhoneNumber.

All unit tests are passing.

…iguous phonenumbers. Added methods to PhoneNumber to support comparing phone numbers numerically which is required when managing ranges of phone numbers.
…eption. This should make it easier to track the source of an error when processing lists of phone numbers. Minor grammatical improvements to exception descriptions.
@bsutton
Copy link
Contributor Author

bsutton commented Sep 7, 2021

Added exmaples.

@cedvdb
Copy link
Owner

cedvdb commented Sep 7, 2021

Nice, thank you. Could you give an use case for this ? An use case for knowing that a phone number is greater than another one or sequential. It might be obvious, like sorting, just to understand

@bsutton
Copy link
Contributor Author

bsutton commented Sep 7, 2021 via email

const PhoneNumberRange(
this.start,
this.end,
);
Copy link
Owner

@cedvdb cedvdb Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should support a backward range, if the starting range is greater than the end, maybe by swapping them if that's the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we only ever use forward ranges.
I think it might be safer if we throw if the try to supply a forward range.
If we just swap we might cause confusion when the range is printed.


var first = BigInt.parse(_number(start));
var last = BigInt.parse(_number(end));

Copy link
Owner

@cedvdb cedvdb Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having some trouble wrapping my head around some things here as it seems you have a future vision.

  • You used _number while there is an international property already. Since you added the leading zero check I'm wondering if that's intentional and you expect the _number property to change.
  • Your comment above the leading zero check is about national number but here you are using the international version, which (currently) would only make sens for checking for international prefix starting with 0, (eg 00).

Unless I'm mistaking, something seems off ? Maybe the idea is to expand ranges of phone numbers without their dial code and with their national prefix, but that's not what's happening here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going forward I would like to see support for national and local phone formats. The international format isn't particularly friendly for end users. National and local formats are much easier to read.

In anticipation of support for local and national codes I want to support leading zeros.

The international method includes a '+' symbol which is why I strip it off and relates to leading zeros.

I guess the questions here is would we ever support a local number without knowing the dial code.
If we won't then there is no point supporting leading zeros.

Copy link
Owner

@cedvdb cedvdb Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see support for national and local phone formats.

That's certainly possible. In another of your comment you mentioned formatting to its local format. I assumed a property like .local on the phone number class is what you had in mind. IE local would return the national number in its local form without the dial code

However, your method expand range would return PhoneNumbers right ? You can then use the .local property on those. The leading zero check would still be done on the _number which is starting with a dial code.

So, as far as I understand, it does not makes sens to use the leading zero check here as it is only confusing as it is currently misapplied. If the local format is added, it would still not make sens as it does not entail that the _number property will change. That's where I don't follow why it is used at all.

the questions here is would we ever support a local number without knowing the dial code.

By " without knowing the dial code " I assume that you mean that the library does not try to figure out where a phone number originates from (in comparison, parseRaw does). If that's the case, the library is not aware of the phone metadata, which means you cannot transform, validate or learn anything about the phone number. It is just a string. The only thing that could be used is the range but at that point it sounds more like string manipulation than phone number parsing. I can see an use case maybe though.

The international method includes a '+' symbol which is why I strip it off and relates to leading zeros.

Does it matter ? BigInt.parse('+45') == BigInt.parse('45')

As you can see I'm a bit reluctant to add code that is currently unnecessary, for the sake of readability. I'd prefer adding those once it is necessary.

@cedvdb
Copy link
Owner

cedvdb commented Sep 7, 2021

Thank you, that looks great.

I'm just hoping this is not used for phone marketing Spam ahah

@bsutton
Copy link
Contributor Author

bsutton commented Sep 7, 2021

And no, not used for spam. We sell phone numbers to customers. We don't make phone calls.

@bsutton
Copy link
Contributor Author

bsutton commented Sep 7, 2021 via email

@cedvdb
Copy link
Owner

cedvdb commented Sep 7, 2021

We are not using the same terminology indeed, so I'll be a bit clearer:

phone_numbers_parser dialCode = country code, for New Zealand it's 64. Which you say is not the correct name.
phone_numbers_parser international prefix is the 0011, well actually it's one of the following in the metadata
"internationalPrefix": "001[14-689]|14(?:1[14]|34|4[17]|[56]6|7[47]|88)0011",
national prefix is 0.

That property should be renamed to avoid confusion as it seems to be misleading. I will get on it asap.

@cedvdb
Copy link
Owner

cedvdb commented Sep 8, 2021

I'm having mixed results in my google searches. I initially used the name "dial Code" instead of country code because I found that "country code" was easily confused with the iso code.

The wikipedia article on country code states

The term country code frequently refers to ISO 3166-1 alpha-2 or international dialing codes, the E.164 country calling codes.

So it seems like "country code" can sometimes mean iso code (alpha-2 above) and other times the digits country calling code. Therefor might still be confused with the iso code.

Dial code is also used differently in different context, sometimes meaning the country code and sometimes meaning the country code + the area like here https://countrycode.org/australia. Which you said is actually telco specific.

I'm guessing "countryCallingCode" might be a better name for this to alleviate some of the possible confusions, alternatively "countryDialCode". I'm hoping someone like yourself who works in the field could help me pick the better name so I don't make the same mistake twice.

@bsutton
Copy link
Contributor Author

bsutton commented Sep 8, 2021 via email

@cedvdb
Copy link
Owner

cedvdb commented Sep 8, 2021

I added a new branch country_code where dial code was renamed to countryCallingCode. I had to change a lot of instance of the word dial code happens. Would it be possible to rebase on that one ? If it's too annoying I can help. Since your changes are mainly on an external file I don't think it will though.

The other properties did not change, as they align with google metadatas properties name. So there is

  • international prefix
  • country calling code
  • national prefix

As for the area code it is not yet added.

@bsutton
Copy link
Contributor Author

bsutton commented Sep 8, 2021 via email

@cedvdb
Copy link
Owner

cedvdb commented Sep 9, 2021

If we are going to make breaking changes then I suggest that we consider
the other properties and breakup of the data I outlined.

Totally. I'm not sure this breaking change is required. However it seems like the dialCode property was confusing. If you search for "country code australia" the first result that pops up in google is "dialing code": 61. Honnestly if we can get away with a breaking change I think it would be better, but not at the expense of being semantically wrong.

countryCallingCode it's too close to countryCode

I'm confused. Those two are the same now. Both are 61 for australia. So I'm not sure I understand what the complain is, I could have used "country code" I just believe it's too ambiguous with the alpha-2 code, adding calling in the middle makes it more obvious those are digits.

I'm using google metadata in the background and this library is based on the implementations found in libphonenumber and phoneNumberKit for ios. Those two libraries use the following properties names:

  • internationalPrefix
  • countryCode (countryCallingCode, previously dialCode)
  • nationalPrefix
  • id (isoCode here)

https://github.com/google/libphonenumber/blob/master/resources/PhoneNumberMetadata.xml

I would like to stay as close as possible to them when possible except if the property names are confusing. I believe internationalPrefix and nationalPrefix are not ambiguous, you even proposed them. So we can keep those.

They use the nsn for nationalSignificantNumbers, internally in their implementation. Currently the nsn of this library means: the remaining digits that are placed after the country code when calling internationally. This currently includes the AreaCode and the remaining digits.

As for the area code this would be a great addition. This data is not in the metadata from google though. However it seems like this data is available here: https://github.com/google/libphonenumber/tree/master/resources/geocoding/en so that's somewhere I could add the metadata for area code from. I would like to keep the build size not too large though, so how it will be added remains to be seen. Maybe a separate Geocoder class but it won't be part of the main parsing process as it is a niche requirement.

So we would have:

  • international prefix 0011
  • TBD country code 61
  • national Prefix 0
  • TBD remaining (currently nsn)

And a GeoCoder that further divide the remaining into:

  • area code
  • remaining from geocoding

As for CountryExitCode, I've seen exit code used to mean internationalPrefix, eg here: https://www.howtocallabroad.com/codes.html so to me it is still ambiguous. Especially for the layman as this code is used to enter another country.

@cedvdb
Copy link
Owner

cedvdb commented Sep 18, 2021

Hello, I took the liberty to pull the changes and fix my comments and merge it into dev as this was stalling a bit (my fault). If you see anything that you'd like let me know. I did not change anything beside my comments and added a section in the readme.

  • I also added a geocoder as a separate library as you seemed to want one. Unfortunately the dataset is huge and I don't see how it could be used anywhere else than on the backend. ( approx 300 000 lines separated in hundreds of files that I had to parse).

  • The semantic chosen is "countryCode" instead of "dialCode" and is now 1:1 with google metadata and are not likely to change anymore. Even though I find their naming confusing. Dial code is deprecated as it was semantically incorrect.

Phone numbers are not my area of expertise to begin with, so I'm also learning while building the library, thanks a lot for the directions.

@cedvdb cedvdb closed this Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants