From 1c716c7cd06f5f327f5af720003ee6784a05c43d Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 15 May 2024 16:24:34 +0700 Subject: [PATCH 1/8] feat: add is valid phone number --- lib/ExpensiMark.js | 10 +++++++++- lib/str.d.ts | 5 +++++ lib/str.js | 12 ++++++++++++ package-lock.json | 9 +++++++++ package.json | 1 + 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index 21f7081b..e20cf21e 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -178,7 +178,7 @@ export default class ExpensiMark { { name: 'reportMentions', - regex: /(?$1', }, @@ -202,6 +202,14 @@ export default class ExpensiMark { return match; } const phoneRegex = new RegExp(`^@${Constants.CONST.REG_EXP.PHONE_PART}$`); + const mention = g2.slice(1); + const metionWithoutSMSDomain = Str.removeSMSDomain(mention); + if ( + !Str.isValidMention(match) || + (new RegExp(`^${Constants.CONST.REG_EXP.PHONE_PART}$`).test(metionWithoutSMSDomain) && !Str.isValidPhoneNumber(metionWithoutSMSDomain)) + ) { + return match; + } return `${g1}${g2}${phoneRegex.test(g2) ? `@${Constants.CONST.SMS.DOMAIN}` : ''}`; }, rawInputReplacement: (match, g1, g2) => { diff --git a/lib/str.d.ts b/lib/str.d.ts index d703d8bf..f4f30487 100644 --- a/lib/str.d.ts +++ b/lib/str.d.ts @@ -485,6 +485,11 @@ declare const Str: { * Check for whether a phone number is valid according to E.164 standard. * @param phone */ + isValidPhoneNumber(phone: string): boolean; + /** + * Check for whether a phone number is valid. + * @param phone + */ isValidE164Phone(phone: string): boolean; /** * Check for whether a phone number is valid in different formats/standards. For example: diff --git a/lib/str.js b/lib/str.js index f9978b4e..d9ce6f6b 100644 --- a/lib/str.js +++ b/lib/str.js @@ -1,5 +1,6 @@ /* eslint-disable no-control-regex */ import _ from 'underscore'; +import {parsePhoneNumber} from 'awesome-phonenumber'; import * as HtmlEntities from 'html-entities'; import * as Constants from './CONST'; import * as UrlPatterns from './Url'; @@ -930,6 +931,17 @@ const Str = { return Constants.CONST.SMS.E164_REGEX.test(phone); }, + /** + * Check for whether a phone number is valid. + * @param {String} phone + * + * @return {bool} + */ + + isValidPhoneNumber(phone) { + return parsePhoneNumber(this.removeSMSDomain(phone)).possible; + }, + /** * Check for whether a phone number is valid according to E.164 standard. * @param {String} phone diff --git a/package-lock.json b/package-lock.json index 615318d5..5003d033 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "awesome-phonenumber": "^5.4.0", "classnames": "2.5.0", "clipboard": "2.0.11", "html-entities": "^2.5.2", @@ -3563,6 +3564,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/awesome-phonenumber": { + "version": "5.11.0", + "resolved": "https://registry.npmjs.org/awesome-phonenumber/-/awesome-phonenumber-5.11.0.tgz", + "integrity": "sha512-25GfikMIo6CBQIqvjoewo4uiu5Ai7WqEC8gxesH3LDwCY43oEdkLaT15a+8adC7uWIJCGh+YQiBY5bjmDpoQcg==", + "engines": { + "node": ">=14" + } + }, "node_modules/axe-core": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/axe-core/-/axe-core-4.7.0.tgz", diff --git a/package.json b/package.json index 051a1584..440ee4d2 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "url": "git+ssh://git@github.com/Expensify/JS-Libs.git" }, "dependencies": { + "awesome-phonenumber": "^5.4.0", "classnames": "2.5.0", "clipboard": "2.0.11", "html-entities": "^2.5.2", From 647ffef04e7bce2c931f33671a0c1fbc70c4c7a5 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 15 May 2024 16:26:34 +0700 Subject: [PATCH 2/8] fix chore --- lib/ExpensiMark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index e20cf21e..25db3e5c 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -178,7 +178,7 @@ export default class ExpensiMark { { name: 'reportMentions', - regex: /(?$1', }, From 322d7b7687fb49ee9ca427305670d9a1e4687e9b Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 15 May 2024 16:31:13 +0700 Subject: [PATCH 3/8] fix comment position --- lib/str.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/str.d.ts b/lib/str.d.ts index f4f30487..e4eb8acb 100644 --- a/lib/str.d.ts +++ b/lib/str.d.ts @@ -482,12 +482,12 @@ declare const Str: { */ isValidPhone(phone: string): boolean; /** - * Check for whether a phone number is valid according to E.164 standard. + * Check for whether a phone number is valid. * @param phone */ isValidPhoneNumber(phone: string): boolean; /** - * Check for whether a phone number is valid. + * Check for whether a phone number is valid according to E.164 standard. * @param phone */ isValidE164Phone(phone: string): boolean; From 4e9cdb02aa46b886388489c4d5f1da1561f7d1dd Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 15 May 2024 17:51:58 +0700 Subject: [PATCH 4/8] remove useless code --- lib/str.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/str.js b/lib/str.js index d9ce6f6b..65007fe2 100644 --- a/lib/str.js +++ b/lib/str.js @@ -939,7 +939,7 @@ const Str = { */ isValidPhoneNumber(phone) { - return parsePhoneNumber(this.removeSMSDomain(phone)).possible; + return parsePhoneNumber(phone).possible; }, /** From fa064832b1880d4d89b1334ccc5eded71c62b91f Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 15 May 2024 18:10:37 +0700 Subject: [PATCH 5/8] rename variable --- lib/ExpensiMark.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index 25db3e5c..58234dba 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -198,18 +198,13 @@ export default class ExpensiMark { 'gim', ), replacement: (match, g1, g2) => { - if (!Str.isValidMention(match)) { - return match; - } - const phoneRegex = new RegExp(`^@${Constants.CONST.REG_EXP.PHONE_PART}$`); + const phoneNumberRegex = new RegExp(`^${Constants.CONST.REG_EXP.PHONE_PART}$`); const mention = g2.slice(1); const metionWithoutSMSDomain = Str.removeSMSDomain(mention); - if ( - !Str.isValidMention(match) || - (new RegExp(`^${Constants.CONST.REG_EXP.PHONE_PART}$`).test(metionWithoutSMSDomain) && !Str.isValidPhoneNumber(metionWithoutSMSDomain)) - ) { + if (!Str.isValidMention(match) || (phoneNumberRegex.test(metionWithoutSMSDomain) && !Str.isValidPhoneNumber(metionWithoutSMSDomain))) { return match; } + const phoneRegex = new RegExp(`^@${Constants.CONST.REG_EXP.PHONE_PART}$`); return `${g1}${g2}${phoneRegex.test(g2) ? `@${Constants.CONST.SMS.DOMAIN}` : ''}`; }, rawInputReplacement: (match, g1, g2) => { From 0c0c65c1d35b486ab5a639d555dd84ae288f7ef2 Mon Sep 17 00:00:00 2001 From: nkdengineer <161821005+nkdengineer@users.noreply.github.com> Date: Fri, 17 May 2024 17:07:50 +0700 Subject: [PATCH 6/8] Update lib/str.d.ts Co-authored-by: Benjamin Limpich --- lib/str.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/str.d.ts b/lib/str.d.ts index e4eb8acb..f48bc35a 100644 --- a/lib/str.d.ts +++ b/lib/str.d.ts @@ -478,7 +478,7 @@ declare const Str: { /** * Check for whether a phone number is valid. * @param phone - * @deprecated use isValidE164Phone to validate E.164 phone numbers or isValidPhoneFormat to validate phone numbers in general + * @deprecated use isValidE164Phone to validate E.164 phone numbers, isValidPhoneFormat to validate phone number format, or isValidPhoneNumber to validate phone numbers in general */ isValidPhone(phone: string): boolean; /** From 1369f15b372feb507ef01101518db950fd4dfe0e Mon Sep 17 00:00:00 2001 From: nkdengineer <161821005+nkdengineer@users.noreply.github.com> Date: Fri, 17 May 2024 17:08:30 +0700 Subject: [PATCH 7/8] Update lib/str.js Co-authored-by: Benjamin Limpich --- lib/str.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/str.js b/lib/str.js index 65007fe2..12571587 100644 --- a/lib/str.js +++ b/lib/str.js @@ -937,7 +937,6 @@ const Str = { * * @return {bool} */ - isValidPhoneNumber(phone) { return parsePhoneNumber(phone).possible; }, From 9192abef6e2d48db713d2e344ea2d93f8f737e04 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 17 May 2024 17:09:55 +0700 Subject: [PATCH 8/8] fix: typo --- lib/ExpensiMark.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index 58234dba..23ca7eb7 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -200,8 +200,8 @@ export default class ExpensiMark { replacement: (match, g1, g2) => { const phoneNumberRegex = new RegExp(`^${Constants.CONST.REG_EXP.PHONE_PART}$`); const mention = g2.slice(1); - const metionWithoutSMSDomain = Str.removeSMSDomain(mention); - if (!Str.isValidMention(match) || (phoneNumberRegex.test(metionWithoutSMSDomain) && !Str.isValidPhoneNumber(metionWithoutSMSDomain))) { + const mentionWithoutSMSDomain = Str.removeSMSDomain(mention); + if (!Str.isValidMention(match) || (phoneNumberRegex.test(mentionWithoutSMSDomain) && !Str.isValidPhoneNumber(mentionWithoutSMSDomain))) { return match; } const phoneRegex = new RegExp(`^@${Constants.CONST.REG_EXP.PHONE_PART}$`);