Normalize raw international phone number, to get the sanitized country code and phone number.
npm install node-phonenode-phone is a phone number normalizer for international phone numbers. It can normalize and sanitize any format of a raw phone number and extract the country-code and the phone number from that raw string. Doesn't matter what format it is in, it will extract the numbers and see if there is a country code in it, if yes, it will be parsed and returned accordingly along with the phone number.
Examples
``+12137091111 to { phone: 2137091111, countryCode: +1 } (213) 709 - 1111 to { phone: 2137091111, countryCode: +1 } 213-709-1111 to { phone: 2137091111, countryCode: +1 } +1 213-709-1111 to { phone: 2137091111, countryCode: +1 } 91 9820679597 to { phone: 9820679597, countryCode: +91 }2348188502204 to { phone: 8188502204, countryCode: +234 } ...`
sh
npm install node-phone --save
`
Getting Started
`javascript
const np = require('node-phone');np.normalizePhone('+12137091111'); // returns { phone: '2137091111', countryCode: '+1' }
np.normalizePhone('213-709-1111'); // returns { phone: '2137091111', countryCode: '+1' }
np.normalizePhone('91 9820679597'); // returns { phone: '9820679597', countryCode: '+91' }
np.normalizePhone('2348188502204'); // returns { phone: '8188502204', countryCode: '+234' }
np.normalizePhone('+1 213-709-1111'); // returns { phone: '2137091111', countryCode: '+1' }
np.normalizePhone('+55 229 989 83686'); // returns { phone: '22998983686', countryCode: '+55' }
`The default country is US (+1) .. if no country code is found in the raw string, we assume that it is US and will return the country code as +1
`javascript
np.normalizePhone('2137091111'); // returns { phone: '2137091111', countryCode: '+1' }
`Test
There is a dev dependencies on jest testing framework.. to run the tests..`sh
npm run test
`
Extra Info
Some countries might not be supported as they overlap with the US phones with area code.
Eg. SG 6598269711
This is actually a singapore number
`{ phone: '98269711', countryCode: '+65' }`, but it is a valid US number as well with area code.
So after sanitizing it will return,
`javascript
np.normalizePhone('6598269711'); // returns { phone: '6598269711', countryCode: '+1' }
``If any more countries found with overlap, let me know.
This project is licensed under the MIT license.