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

Lack of phone number extension support is a deal breaker. #129

Closed
infoeon opened this issue Sep 13, 2017 · 14 comments
Closed

Lack of phone number extension support is a deal breaker. #129

infoeon opened this issue Sep 13, 2017 · 14 comments

Comments

@infoeon
Copy link

infoeon commented Sep 13, 2017

While phone number extensions (eg ext1234 or x1234) may not make sense when working with ordinary consumers, websites that deal with business or enterprise customers require this functionality. There's a large developer base out there that works only with B2B and B2E development that need to support extensions.

@catamphetamine
Copy link
Owner

Why not store the extension as a separate field in the database and then output the formatted number + "ext. XXXX".

@ghost
Copy link

ghost commented Dec 6, 2017

You could probably also wrap your calls to libphonenumber-js and cut "x1234" or "ext1234" off the end of your inputs/data values before calling it. That way you could store it in the database however you want and/or allow whatever input format you want.

@jpickwell
Copy link

We store the extension in a separate column in the database, but part of what I like about libphonenumber is the "as you type" feature and the ability to extract the extension. I'm assuming, since you dropped extension support, that this feature also doesn't support them.

@ghost
Copy link

ghost commented Jan 19, 2018

@jpickwell I am not familiar with the Android/Java libphonenumber. I searched briefly but I couldn't find any support for this feature. Are you saying that this ability exists in the Java version of the library and you want it in the JavaScript version as well?

With regards to my own advice, I was just saying that it seems like you can do this yourself pretty easily by intercepting calls to libphonenumber-js wherever you use asYouType...

const libphonenumberJs = require("libphonenumber-js")
const asYouType = libphonenumberJs.asYouType;
// Raw input from your onChange event...
var rawInput = '5554443333#1234';
// '#' could be any string, i.e. 'ext', 'x' or you could split on multiple tokens...
var parts = rawInput.split('#');
var phonePart = parts[0];
var phoneFormatted = new asYouType('US').input(phonePart);
var phoneExtension = parts[1].trim();
console.log('Phone: ' + phoneFormatted); // "Phone: (555) 444-3333"
console.log('__Ext: ' + phoneExtension); // "__Ext: 1234"
'OK';

Run it with RunKit

@jpickwell
Copy link

@waynebloss, google-libphonenumber has this (and it's a 1:1 with Google's own JS library). https://ruimarinho.github.io/google-libphonenumber/#google-libphonenumber-methods-i18nphonenumbersphonenumber

@ghost
Copy link

ghost commented Jan 19, 2018

@jpickwell Ahhh ok, thank you.

It seems like the author of this library (libphonenumber-js) has a strong opinion about this since the readme says "Doesn't parse phone numbers with extensions...".

So, someone would probably have to fork this project to do it.

@catamphetamine
Copy link
Owner

catamphetamine commented Jan 20, 2018

So, someone would probably have to fork this project to do it.

Most likely no one ever will.
Still, if anyone comes here, here are a couple of hyperlinks.

Here's a link to Google's demo illustrating phone number extensions parsing:
https://libphonenumber.appspot.com/phonenumberparser?number=2134567890+ext+123&country=US

The phone number parsing code is in this file:
https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js

The regular expression for parsing extensions:
https://github.com/googlei18n/libphonenumber/blob/ed6b1ff4995ff562294aa00cdb718fa7888a0986/javascript/i18n/phonenumbers/phonenumberutil.js#L759-L786

libphonenumber-js drops that part from VALID_PHONE_NUMBER_PATTERN

const VALID_PHONE_NUMBER =
'[' + PLUS_CHARS + ']{0,1}' +
'(?:' +
'[' + VALID_PUNCTUATION + ']*' +
'[' + VALID_DIGITS + ']' +
'){3,}' +
'[' +
VALID_PUNCTUATION +
VALID_DIGITS +
']*'

And the original Google's library has it:
https://github.com/googlei18n/libphonenumber/blob/ed6b1ff4995ff562294aa00cdb718fa7888a0986/javascript/i18n/phonenumbers/phonenumberutil.js#L811-L818

As you type is implemented in this file:
https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/asyoutypeformatter.js
Maybe it also contains some extension logic (though it doesn't contain the word "extension").

@catamphetamine
Copy link
Owner

Ok, fuck you, if it's really that simple then I'll try it.

@ghost
Copy link

ghost commented Jan 20, 2018

@catamphetamine Thanks for your code and for doing the extra work to find out how to make this change (and possibly implementing it)!

It would be nice to easily capture the extension without having a separate input.

@catamphetamine
Copy link
Owner

catamphetamine commented Jan 20, 2018

Well, I got it parsed and formatted.
Not so sure about tampering with as you type - do not want.
A new version will be released after I fix code coverage supporting extensions in parse() and format().
Is your use case demanding as you type or will you get away with parse and format.

@ghost
Copy link

ghost commented Jan 20, 2018

I am not using this library yet, but I was hoping to. Perhaps @jpickwell will answer.

@catamphetamine
Copy link
Owner

So original Google's library only supports extensions when parsing and formatting, and not when using "as you type" formatter.
Therefore the latest libphonenumber-js version supports phone number extensions the same way Google's library does.
There are some other changes along the way, see the CHANGELOG and the updated README.

@jpickwell
Copy link

I made an assumption that the "as you type" formatter handled extensions.

@edshen17
Copy link

edshen17 commented Mar 18, 2024

just a note, for anyone who wants to do phone extensions for AsYouType, you can do something like this:

class CustomPhoneFormatter extends AsYouType {
  constructor() {
    super(`US`)
  }

  input(text: string) {
    const inputText = text
    const parsedNumber = parsePhoneNumberFromString(inputText, `US`)

    if (parsedNumber && parsedNumber.ext) {
      return parsedNumber.formatNational()
    }

    return super.input(inputText)
  }
}

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

No branches or pull requests

4 participants