From 1891209b0539af577363dfe67ff2d0ba5a87f644 Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Thu, 2 May 2024 12:25:37 +0400 Subject: [PATCH 1/5] Update development metadata --- development/src/phone-hooks/metadata/countries.json | 4 ++-- development/src/phone-hooks/metadata/validations.json | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/development/src/phone-hooks/metadata/countries.json b/development/src/phone-hooks/metadata/countries.json index 5f37dd1..f57b22a 100644 --- a/development/src/phone-hooks/metadata/countries.json +++ b/development/src/phone-hooks/metadata/countries.json @@ -1173,7 +1173,7 @@ "xk", "Kosovo", "383", - "+383 (...) ... ..." + "+383 (...) ... ... ..." ], [ "kw", @@ -1899,7 +1899,7 @@ "sg", "Singapore", "65", - "+65 (....) ... ...." + "+65 (....) .... ..." ], [ "sx", diff --git a/development/src/phone-hooks/metadata/validations.json b/development/src/phone-hooks/metadata/validations.json index 04c8367..58a4eaa 100644 --- a/development/src/phone-hooks/metadata/validations.json +++ b/development/src/phone-hooks/metadata/validations.json @@ -181,7 +181,7 @@ ], "cu": [ "^\\d{6,10}$", - "^[27]\\d{6,7}|[34]\\d{5,7}|63\\d{6}|(?:5|8\\d\\d)\\d{7}$" + "^(?:[2-7]|8\\d\\d)\\d{7}|[2-47]\\d{6}|[34]\\d{5}$" ], "cv": [ "^\\d{7}$", @@ -577,7 +577,7 @@ ], "mx": [ "^\\d{10,11}$", - "^1(?:(?:22|44|7[27]|87|99)[1-9]|65[0-689])\\d{7}|(?:1(?:[01]\\d|2[13-9]|[35][1-9]|4[0-35-9]|6[0-46-9]|7[013-689]|8[1-69]|9[1-8])|[2-9]\\d)\\d{8}$" + "^1(?:(?:22|44|7[27]|87|9[69])[1-9]|65[0-689])\\d{7}|(?:1(?:[01]\\d|2[13-9]|[35][1-9]|4[0-35-9]|6[0-46-9]|7[013-689]|8[1-69]|9[1-578])|[2-9]\\d)\\d{8}$" ], "my": [ "^\\d{8,10}$", @@ -825,7 +825,7 @@ ], "tm": [ "^\\d{8}$", - "^[1-6]\\d{7}$" + "^(?:[1-6]\\d|71)\\d{6}$" ], "tn": [ "^\\d{8}$", @@ -912,8 +912,8 @@ "^(?:[2-6]|8\\d{5})\\d{4}|[78]\\d{6}|[68]\\d{5}$" ], "xk": [ - "^\\d{8,9}$", - "^[23]\\d{7,8}|(?:4\\d\\d|[89]00)\\d{5}$" + "^\\d{8,12}$", + "^2\\d{7,8}|3\\d{7,11}|(?:4\\d\\d|[89]00)\\d{5}$" ], "ye": [ "^\\d{7,9}$", From 96217bffa43c23d839d7356279ffb8c56df782e0 Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Thu, 2 May 2024 12:27:14 +0400 Subject: [PATCH 2/5] Overload the `getFormattedNumber` utility --- src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index eb68e77..a10f362 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,8 +36,9 @@ export const cleanInput = (input: any, pattern: string) => { return Array.from(pattern, c => input[0] === c || slots.has(c) ? input.shift() || c : c); } -export const getFormattedNumber = (rawValue: any, pattern: string) => { +export const getFormattedNumber = (rawValue: any, pattern?: string) => { /** Returns the reformatted input value based on the given pattern */ + pattern = pattern || getMetadata(rawValue)?.[3] || ""; return displayFormat(cleanInput(rawValue, pattern.replaceAll(/\d/g, ".")).join("")); } From 5c1ca6746af2e2d3f9cdedc1841832a78d24c1c9 Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Thu, 2 May 2024 12:30:38 +0400 Subject: [PATCH 3/5] Check the overload `getFormattedNumber` --- tests/utils.test.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/utils.test.tsx b/tests/utils.test.tsx index 07b8ecf..7d013d1 100644 --- a/tests/utils.test.tsx +++ b/tests/utils.test.tsx @@ -8,9 +8,11 @@ describe("Verifying the basic functionality", () => { const metadata = getMetadata(rawValue); const formattedNumber = getFormattedNumber(rawValue, (metadata as any)[3]); + const formattedNumberOverloaded = getFormattedNumber(rawValue); const parsedPhoneNumber = parsePhoneNumber(formattedNumber); const rawPhoneNumber = getRawValue(formattedNumber); + assert(formattedNumber === formattedNumberOverloaded); assert(formattedNumber !== null && formattedNumber === "+1 (702) 123 4567"); assert(parsedPhoneNumber !== null && parsedPhoneNumber.countryCode === 1); assert(parsedPhoneNumber.areaCode === "702" && parsedPhoneNumber.phoneNumber === "1234567"); From 1f2e50832cc2295be450f1f5c310e09740087e11 Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Thu, 2 May 2024 12:31:57 +0400 Subject: [PATCH 4/5] Update the usage section --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2f3696c..c5cb0bb 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ opposite. You can use the [development](./development) to test and develop your ```jsx import {getFormattedNumber, getMetadata, parsePhoneNumber, useMask} from "react-phone-hooks"; +getFormattedNumber("440201111111"); // +44 (02) 011 11111 getMetadata("440201111111"); // ["gb", "United Kingdom", "44", "+44 (..) ... ....."] -getFormattedNumber("440201111111", "+44 (..) ... ....."); // +44 (02) 011 11111 parsePhoneNumber("+44 (02) 011 11111"); // {countryCode: 44, areaCode: "02", phoneNumber: "01111111", isoCode: "gb"} const PhoneInput = (props) => { From f1cff9e692a528235e636ea7dbe3a141467b5dde Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Thu, 2 May 2024 12:32:42 +0400 Subject: [PATCH 5/5] Upgrade the version to `0.1.3` --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 25be4b9..55f9aba 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.2", + "version": "0.1.3", "name": "react-phone-hooks", "description": "React hooks and utility functions for parsing and validating phone numbers.", "keywords": [