-
Notifications
You must be signed in to change notification settings - Fork 113
/
normalizer.ts
27 lines (25 loc) · 1.03 KB
/
normalizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { phoneNumberValidator } from ".";
/**
* Normalize a phone number to a standard format based on the provided token.
* This function is related to the {@link phoneNumberValidator} function to detect that the mobile number is valid or not
*
* @category Phone number
* @public
* @constructor
* @param {string} number phone number
* @param {string} token can be either `+98` or `0`
* @return String | never
* @example
* phoneNumberNormalizer("+989022002580", "0"); // 09022002580
* phoneNumberNormalizer("989022002580", "0"); // 09022002580
* phoneNumberNormalizer("09022002580", "0"); // 09022002580
* phoneNumberNormalizer("09022002580", "+98"); //+989022002580
* phoneNumberNormalizer("09802002580", "0"); // Error : this is not valid phone number
*/
export default function phoneNumberNormalizer(phoneNumber: string, token: "0" | "+98") {
if (!phoneNumberValidator(phoneNumber)) {
throw new Error("phone number is not valid");
}
const phoneSuffix = phoneNumber.split(/^(?:\+98|98|0098|0)/).pop();
return token + phoneSuffix;
}