PiedTeam / NikeCloneTraining-BE-Project

0 stars 0 forks source link

[Research] Change to other library phone method #137

Closed lcaohoanq closed 2 months ago

lcaohoanq commented 3 months ago

Current using

lcaohoanq commented 3 months ago

Comparision lib phonenumber

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

1. [libphonenumber-js]

https://www.npmjs.com/package/libphonenumber-js

Comparision with Google's libphonenumber

  • Smaller footprint: 145 kB (65 kB code + 80 kB sufficient metadata) vs the original Google's 550 kB (350 kB code + 200 kB full metadata).
  • Can search for phone numbers in text (Google's autogenerated JavaScript port can't).
  • Aims at parsing and formatting people's phone numbers while skipping all other "special" cases like:
    • Emergency phone numbers like 911.
    • "Short codes": short SMS-only numbers like 12345.
    • Numbers starting with a *, like *555.
    • Alphabetic phone numbers like 1-800-GOT-MILK: people don't input their phone numbers like that, it's only used in advertisement.
    • "Two-in-one" phone numbers with "combined" extensions like (530) 583-6985 x302/x2303 that in fact represent two separate phone numbers, because the library can only return a single phone number when parsing a string.
    • Overall, doesn't support formatting non-"conventional" numbers like numbers with the "area code" omitted or "alternative" "short" numbers like Australian 13-smart numbers. For example, when dialing phone numbers within the same "area", people sometimes skip the "area code", and dial, say, 456-789 instead of (123) 456-789. Google's libphonenumber supports formatting such numbers (with "area code" omitted) because it is used for dialing on the Android phone operating system. Because libphonenumber-js isn't a phone operating system and is not used for actual dialing — only for inputting internationally-dialable personal phone numbers — it doesn't format such "short" phone numbers because it doesn't need to support those.
    • Any other "miscellaneous" cases that're considered irrelevant for the task.
  • Doesn't provide "geolocation" by a phone number.
  • Doesn't use hyphens or brackets when formatting international phone numbers, only whitespace (seems more logical this way).
  • Doesn't set .country to "001" when parsing "non-geographic" phone numbers (like mobile satellite communications services). Instead, .country is undefined in those cases, and a developer can call .isNonGeographic() method of the PhoneNumber instance to find out whether the parsed phone number is a "non-geographic" one.
  • Doesn't provide the equivalent of libphonenumber's formatNumberForMobileDialing() function that formats a number for dialing using a mobile phone within the country: dialing local numbers from a mobile phone is a bit more complicated in some countries like Brazil or Colombia where they require adding "carrier codes" when making a call. Since libphonenumber-js is not a dialing library (we're not Android phone operating system), it doesn't prepend any "carrier codes" when formatting phone numbers, though it does parse such "carrier codes" correctly.

Scope using

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';

Image

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()
}

2. [google-libphonenumber]

https://www.npmjs.com/package/google-libphonenumber

3. [awesome-phonenumber]

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

//

minhhy2801 commented 3 months ago

Find a solution of the issue only use 1 phone number to send message

lcaohoanq commented 2 months ago

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