Closed lcaohoanq closed 2 months ago
Criteria | libphonenumber-js | google-libphonenumber | awesome-phonenumber |
---|---|---|---|
Release date | March 13, 2020 | Nov 12, 2014 | Jun 30, 2015 |
Lastest release | 1.11.3 | 3.2.34 | 6.9.0 |
Size and Dependencies | Files: 786 Unpacked Sized: 8.27MB |
Files: 6 Unpacked Sized: 606KB |
Files: 8 Unpacked Sized: 561KB |
Last update | 4 days ago | 5 months ago | 10 days ago |
Total Commit | 430 | 489 | 291 |
Total Contribute | 1 | 13 | 6 |
Downloads | 2,432,236 ~ 5,399,009 | 400.000 ~ 1.100.000 | 141.000 ~ 330.000 |
Documentation | Well-documented | Well-documented | Well-documented |
Compatibility | Node.js, browser | Node.js, browser | Node.js, browser |
TypeScript Support | Yes | Yes | Yes |
Licensing | MIT | MIT & Apache 2.0 | MIT & Apache 2.0 |
Because this is the rewrite version of google-libphonenumber in Js code, the author mainly point shouw use the comparision between both of them, which is better
In the current project, the libphonenumber-js are used at validation.ts
for checking the user phonenumber at middleware checkEmailOrPhone
, we just checking the phonenumber from Vietnam format only, if need more, can add more the region code that dependencies provide
type.d.cts
export type CountryCode = 'AC' | 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AO' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AX' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BL' | 'BM' | 'BN' | 'BO' | 'BQ' | 'BR' | 'BS' | 'BT' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CC' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CW' | 'CX' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'EH' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HN' | 'HR' | 'HT' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IO' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KP' | 'KR' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MF' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NF' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PM' | 'PR' | 'PS' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SJ' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SX' | 'SY' | 'SZ' | 'TA' | 'TC' | 'TD' | 'TG' | 'TH' | 'TJ' | 'TK' | 'TL' | 'TM' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WF' | 'WS' | 'XK' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW';
validation.ts
import parsePhoneNumberFromString, { CountryCode, parsePhoneNumber } from 'libphonenumber-js'
export function isValidPhoneNumberForCountry( phone_number: string, country: CountryCode | undefined ) { const phoneNumber = parsePhoneNumberFromString(phone_number, { defaultCountry: country }) if (!phoneNumber) { return false } if (phoneNumber.country !== country) { return false } return phoneNumber.isValid() }
> user.middlewares.ts
```bash
export const checkEmailOrPhone = (
req: Request,
res: Response,
next: NextFunction
) => {
const body = req.body as ParamsDictionary
const email_phone = body.email_phone
if (validator.isEmail(email_phone)) {
req.body.email = email_phone
req.body.type = 'email'
} else if (isValidPhoneNumberForCountry(email_phone, 'VN')) {
req.body.phone_number = email_phone
req.body.type = 'phone_number'
} else {
next(
new ErrorEntity({
message: USER_MESSAGES.UNPROCESSABLE_ENTITY,
status: HTTP_STATUS.UNPROCESSABLE_ENTITY,
data: { field: { msg: USER_MESSAGES.FIELD_ERROR_FORMAT } }
})
)
}
delete req.body.email_phone
next()
}
https://www.npmjs.com/package/awesome-phonenumber Google's libphonenumber pre-compiled with the closure compiler
Action | awesome-phonenumber 2.56.0 (lib 8.12.29) | google-libphonenumber 3.2.22 (lib 8.12.27) | libphonenumber-js 1.9.23 (lib -) |
---|---|---|---|
Load library first time | 11.0 ms ✅ | 29.67 ms | 32.87 ms |
Parse first phone number | 4.3 ms | 4.01 ms | 3.43 ms ✅ |
⇒ Load + parse first number | 15.3 ms ✅ | 33.68 ms | 36.3 ms |
Parse second phone number | 0.78 ms ✅ | 0.97 ms | 0.92 ms |
Increased memory usage | 5.12 M ✅ | 9.99 M | 5.86 M |
node_modules size | 296 K ✅ | 600 K | 7.6 M |
node_modules files | 8 | 7 ✅ | 653 |
Find a solution of the issue only use 1 phone number to send message
Find a solution of the issue only use 1 phone number to send message
cc: @minhhy2801
Hmm, seem something fail with the Twilio registeration or verification, i will fix now
Current using