UNPKG

2.93 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.isValidPhoneNumber = exports.isPhoneNumberControlChar = exports.isPhoneNumberSeparatorChar = void 0;
4// Regex that holds the characters used to separate segments of a phone number
5var separatorCharRe = /[-. ]/;
6// Regex that specifies any delimiter char that allows us to treat the number as
7// a phone number rather than just any other number that could appear in text.
8var hasDelimCharsRe = /[-. ()]/;
9// "Pause" and "Wait" control chars
10var controlCharRe = /[,;]/;
11// Over the years, many people have added to this regex, but it should have been
12// split up by country. Maybe one day we can break this down.
13var mostPhoneNumbers = /(?:(?:(?:(\+)?\d{1,3}[-. ]?)?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4})|(?:(\+)(?:9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)[-. ]?(?:\d[-. ]?){6,12}\d+))([,;]+[0-9]+#?)*/;
14// Regex for Japanese phone numbers
15var japanesePhoneRe = /(0([1-9]-?[1-9]\d{3}|[1-9]{2}-?\d{3}|[1-9]{2}\d{1}-?\d{2}|[1-9]{2}\d{2}-?\d{1})-?\d{4}|0[789]0-?\d{4}-?\d{4}|050-?\d{4}-?\d{4})/;
16// Combined regex
17var validPhoneNumberRe = new RegExp("^".concat(mostPhoneNumbers.source, "|").concat(japanesePhoneRe.source, "$"));
18/**
19 * Determines if the character is a phone number separator character (i.e.
20 * '-', '.', or ' ' (space))
21 */
22function isPhoneNumberSeparatorChar(char) {
23 return separatorCharRe.test(char);
24}
25exports.isPhoneNumberSeparatorChar = isPhoneNumberSeparatorChar;
26/**
27 * Determines if the character is a control character in a phone number. Control
28 * characters are as follows:
29 *
30 * - ',': A 1 second pause. Useful for dialing extensions once the main phone number has been reached
31 * - ';': A "wait" that waits for the user to take action (tap something, for instance on a smart phone)
32 */
33function isPhoneNumberControlChar(char) {
34 return controlCharRe.test(char);
35}
36exports.isPhoneNumberControlChar = isPhoneNumberControlChar;
37/**
38 * Determines if the given phone number text found in a string is a valid phone
39 * number.
40 *
41 * Our state machine parser is simplified to grab anything that looks like a
42 * phone number, and this function confirms the match.
43 */
44function isValidPhoneNumber(phoneNumberText) {
45 // We'll only consider the match as a phone number if there is some kind of
46 // delimiter character (a prefixed '+' sign, or separator chars).
47 //
48 // Accepts:
49 // (123) 456-7890
50 // +38755233976
51 // Does not accept:
52 // 1234567890 (no delimiter chars - may just be a random number that's not a phone number)
53 var hasDelimiters = phoneNumberText.charAt(0) === '+' || hasDelimCharsRe.test(phoneNumberText);
54 return hasDelimiters && validPhoneNumberRe.test(phoneNumberText);
55}
56exports.isValidPhoneNumber = isValidPhoneNumber;
57//# sourceMappingURL=phone-number-utils.js.map
\No newline at end of file