catamphetamine / libphonenumber-js

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
https://catamphetamine.gitlab.io/libphonenumber-js/
MIT License
2.76k stars 218 forks source link

National prefix option will respect false #418

Closed PCkelley closed 2 years ago

PCkelley commented 2 years ago

Thank you for maintaining this library, it has been great to work with! I am currently working with the formatNational method on a phone number input project. I do not want the nationalNumber prefix to show in my project. I found documentation on setting the option for nationalPrefix to false

nationalPrefix: Boolean — Some phone numbers can be formatted both with national prefix and without it. In such cases the library defaults to "with national prefix" (for legacy reasons). Pass nationalPrefix: false option to force formatting without national prefix in such cases.

When looking into the code I realized that format.nationalPrefixIsOptionalWhenFormattingInNationalFormat() is always false when using a national number so passing in the option would never work. Then change I am submitting ot make this expression check for an || instead of && will make the formatter respect the nationalPrefix: false option

catamphetamine commented 2 years ago

No no no, it all works as intended.


    it('should format with national prefix when specifically instructed', () => {
        // With national prefix.
        formatNumber('88005553535', 'RU', 'NATIONAL').should.equal('8 (800) 555-35-35')
        // Without national prefix via an explicitly set option.
        formatNumber('88005553535', 'RU', 'NATIONAL', { nationalPrefix: false }).should.equal('800 555-35-35')
    })

The test passes so it works.

https://github.com/catamphetamine/libphonenumber-js/blob/4796f209b273265ab42a5216401a108f6b1d6c6d/source/format.test.js#L44-L49