hexorx / e164

Tools for working with phone numbers in the e164 format.
github.com/hexorx/e164
MIT License
39 stars 10 forks source link

E164.parse gives preference to discontinued number blocks #5

Closed tokengeek closed 12 years ago

tokengeek commented 12 years ago

When using E164.parse with "+" formatted numbers the country_code for 21 countries is incorrect.

These relate to three digit codes that the the first two digits match a discontinued code.

For example for a number in the Czech Republic (420):

E164.parse("+420298888888") # => ["42", "02", "98888888"]

"42" is 'previously assigned to [Czechoslovakia] until its breakup. Czech Republic and Slovakia used this common code until 1 March 1997.'

I've added test cases to be added in to spec/e164_spec.rb

  describe "#parse" do
    it 'should not give preference to discontinued number blocks' do
      E164.parse("+420298888888")[0].should == "420"
    end

    CountryCodes.each do |code, data|
      it "should pick up the correct country code for #{data[:description]} when in + format" do
        # Create a false number in + format because the code provided is correct
        fake_number = "+#{code}1234567"
        E164.parse(fake_number)[0].should == code
      end
    end
  end

Running this against 69f6236 gives 22 failures. 1 for the special Czech Republic case and 21 for all the cases where the parsing seems to pick up on a shorter code first.

List of affected Countries:

hexorx commented 12 years ago

So it looks like the codes 37, 38 and 42 should be removed when it is finding the country code. Does that sound right?

tokengeek commented 12 years ago

I think that's right. I think it's unlikely you would want to retain the old data in order to match numbers which are no longer in use.

hexorx commented 12 years ago

Thank you @scsmith for the fix.