Skip to content

Commit 3a1db3e

Browse files
committed
feat: add phone type
1 parent 219a87e commit 3a1db3e

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/utils/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,21 @@ export const luhnChecksumValidate = (
8585

8686
return sum % digits.length === 0;
8787
};
88+
89+
export const addPrefixForMobile = (number: string): string => {
90+
const pregix = "+30";
91+
const cleanedNumber = number.replace(/[\s\-()\.]/g, "");
92+
93+
// Check if the phone number starts with the Greek international prefix or '0030'
94+
if (cleanedNumber.startsWith(pregix) || cleanedNumber.startsWith("0030")) {
95+
return cleanedNumber;
96+
}
97+
98+
// If the phone number starts with '69' (mobile), add the international prefix
99+
if (cleanedNumber.startsWith("69")) {
100+
return pregix + cleanedNumber;
101+
}
102+
103+
// If the phone number doesn't match the expected formats, return the original phone number
104+
return number;
105+
};

src/validators/phone.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1+
type PhoneNumberType = "mobile" | "landline" | "";
2+
13
/**
24
* Validates Greek mobile and landline phone numbers.
35
*
46
* @param {string} value - The phone number to validate.
7+
* @param {PhoneNumberType} [type] - Optional. The type of phone number to validate ('mobile', 'landline', or ''). Defaults to '' (both).
58
* @returns {boolean} - Returns true if the phone number is valid, otherwise false.
69
*/
7-
export default (value: string): boolean => {
10+
export default (value: string, type: PhoneNumberType = ""): boolean => {
811
const cleanedValue = value.replace(/[\s\-()\.]/g, "");
912

1013
// Validate mobile numbers
1114
const mobileRegex = /^(\+30|0030)?69\d{8}$/;
12-
if (mobileRegex.test(cleanedValue)) {
13-
return true;
15+
if (type === "mobile" || type === "") {
16+
if (mobileRegex.test(cleanedValue)) {
17+
return true;
18+
}
1419
}
1520

1621
// Validate landline numbers with area codes
1722
const landlineRegex = /^(\+30|0030)?(2\d{9}|801\d{7})$/;
18-
if (landlineRegex.test(cleanedValue)) {
19-
return true;
23+
if (type === "landline" || type === "") {
24+
if (landlineRegex.test(cleanedValue)) {
25+
return true;
26+
}
2027
}
2128

2229
return false;

test/isPhone.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ describe.concurrent("isPhone", () => {
2020
it("00302101234567", () => {
2121
expect(isPhone("00302101234567")).toBe(true);
2222
});
23+
24+
it("00302101234567", () => {
25+
expect(isPhone("00302101234567", "mobile")).toBe(false);
26+
});
2327
});

0 commit comments

Comments
 (0)