Skip to content

Commit

Permalink
Overload getFormattedNumber utility function (GH-26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtyomVancyan authored May 2, 2024
2 parents 7919e8a + f1cff9e commit d175f23
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions development/src/phone-hooks/metadata/countries.json
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@
"xk",
"Kosovo",
"383",
"+383 (...) ... ..."
"+383 (...) ... ... ..."
],
[
"kw",
Expand Down Expand Up @@ -1899,7 +1899,7 @@
"sg",
"Singapore",
"65",
"+65 (....) ... ...."
"+65 (....) .... ..."
],
[
"sx",
Expand Down
10 changes: 5 additions & 5 deletions development/src/phone-hooks/metadata/validations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}$",
Expand Down Expand Up @@ -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}$",
Expand Down Expand Up @@ -825,7 +825,7 @@
],
"tm": [
"^\\d{8}$",
"^[1-6]\\d{7}$"
"^(?:[1-6]\\d|71)\\d{6}$"
],
"tn": [
"^\\d{8}$",
Expand Down Expand Up @@ -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}$",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(""));
}

Expand Down
2 changes: 2 additions & 0 deletions tests/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit d175f23

Please sign in to comment.