phone numbers parser
dart library for parsing phone numbers. inspired by google’s libphonenumber and phonenumberkit for ios.
the advantage of this lib instead of libphonenumber is that it instantly supports all platforms (no need for channeling).
features
- find phone numbers in a text
- validate a phone number
- find the region of a phone number
- phone number parsing
- a light parser for size aware applications
- supports easthern arabic digits
- uses best-in-class metadata from google’s libphonenumber project.
parsers
there are two parsers: lightphoneparser and phone parser
lightphoneparser:
- smaller size footprint (more will be tree shaken)
- uses mainly length information for validation
- fast
phoneparser:
- more accurate
- more utilities
- bigger size footprint
- more computationally intensive
- uses pattern matching
usage phoneparser
final parser = phoneparser();
// creation
final frphone = parser.parseraw('+33 655 5705 76');
final frphone1 = parser.parsewithisocode('fr', '655 5705 76');
final frphone2 = parser.parsewithdialcode('33', '655 5705 76');
final frphone3 = parser.parsewithisocode('fr', '0655 5705 76');
final allsame =
frphone == frphone1 && frphone == frphone2 && frphone == frphone3;
print(allsame); // true
// validation
print(parser.validate(frphone1)); // true
print(parser.validate(frphone1, phonenumbertype.mobile)); // true
print(parser.validate(frphone1, phonenumbertype.fixedline)); // false
// changing the country
final esphone = parser.copywithisocode(frphone, 'es');
print(esphone.dialcode); // 34
print(esphone.isocode); // es
print(esphone.international); // '+34655570576'
// utils
final text = 'hey my phone number is: +33 939 876 218';
final found = parser.findpotentialphonenumbers(text);
print(text.substring(found.first.start, found.first.end));
usage lightphoneparser
final parser = lightphoneparser();
// creation
final frphone = parser.parsewithisocode('fr', '+33 655 5705 76');
final frphone1 = parser.parsewithisocode('fr', '655 5705 76');
final frphone2 = parser.parsewithisocode('fr', '655 5705 76');
final frphone3 = parser.parsewithisocode('fr', '0655 5705 76');
final allsame =
frphone == frphone1 && frphone == frphone2 && frphone == frphone3;
print(allsame); // true
// validation
print(parser.validate(frphone1)); // true
print(parser.validate(frphone1, phonenumbertype.mobile)); // true
print(parser.validate(frphone1, phonenumbertype.fixedline)); // false
// changing the country
final esphone = parser.copywithisocode(frphone, 'es');
print(esphone.dialcode); // 34
print(esphone.isocode); // es
print(esphone.international); // '+34655570576'
// utils
final text = 'hey my phone number is: +33 939 876 218';
final found = parser.findpotentialphonenumbers(text);
print(text.substring(found.first.start, found.first.end));
migration to 1.0.0
1.0.0 introduces two parsers:
- phoneparser: heavy, more accurate and more computationally intensive (relies on pattern matching)
- lightphoneparser: light, somewhat accurate and less computationally intensive (relies on length)
if a light validation based on length suffice, lightphoneparser can be used, assuming you don’t import
the other parser, that will decrease the library size footprint.
with this change a few breaking changes had to be made
before:
final frphone = phonenumber.fromraw('+33 655 5705 76');
final frphone1 = phonenumber.fromisocode('fr', '655 5705 76');
final valid = frphone.validate();
after:
final frphone = phoneparser.fromraw('+33 655 5705 76')
final frphone1 = lightphoneparser.fromisocode('fr', '655 5705 76');
final valid = phoneparser.validate(frphone);
demo
the phone number input packages has a demo that uses this parser: https://cedvdb.github.io/phone_form_field/
Comments are closed.