UNPKG

2.83 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = isValidNumber;
7
8var _metadata = _interopRequireDefault(require("./metadata"));
9
10var _matchesEntirely = _interopRequireDefault(require("./helpers/matchesEntirely"));
11
12var _getNumberType = _interopRequireDefault(require("./helpers/getNumberType"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
16/**
17 * Checks if a given phone number is valid.
18 *
19 * If the `number` is a string, it will be parsed to an object,
20 * but only if it contains only valid phone number characters (including punctuation).
21 * If the `number` is an object, it is used as is.
22 *
23 * The optional `defaultCountry` argument is the default country.
24 * I.e. it does not restrict to just that country,
25 * e.g. in those cases where several countries share
26 * the same phone numbering rules (NANPA, Britain, etc).
27 * For example, even though the number `07624 369230`
28 * belongs to the Isle of Man ("IM" country code)
29 * calling `isValidNumber('07624369230', 'GB', metadata)`
30 * still returns `true` because the country is not restricted to `GB`,
31 * it's just that `GB` is the default one for the phone numbering rules.
32 * For restricting the country see `isValidNumberForRegion()`
33 * though restricting a country might not be a good idea.
34 * https://github.com/googlei18n/libphonenumber/blob/master/FAQ.md#when-should-i-use-isvalidnumberforregion
35 *
36 * Examples:
37 *
38 * ```js
39 * isValidNumber('+78005553535', metadata)
40 * isValidNumber('8005553535', 'RU', metadata)
41 * isValidNumber('88005553535', 'RU', metadata)
42 * isValidNumber({ phone: '8005553535', country: 'RU' }, metadata)
43 * ```
44 */
45function isValidNumber(input, options, metadata) {
46 // If assigning the `{}` default value is moved to the arguments above,
47 // code coverage would decrease for some weird reason.
48 options = options || {};
49 metadata = new _metadata["default"](metadata); // This is just to support `isValidNumber({})`
50 // for cases when `parseNumber()` returns `{}`.
51
52 if (!input.country) {
53 return false;
54 }
55
56 metadata.selectNumberingPlan(input.country, input.countryCallingCode); // By default, countries only have type regexps when it's required for
57 // distinguishing different countries having the same `countryCallingCode`.
58
59 if (metadata.hasTypes()) {
60 return (0, _getNumberType["default"])(input, options, metadata.metadata) !== undefined;
61 } // If there are no type regexps for this country in metadata then use
62 // `nationalNumberPattern` as a "better than nothing" replacement.
63
64
65 var national_number = options.v2 ? input.nationalNumber : input.phone;
66 return (0, _matchesEntirely["default"])(national_number, metadata.nationalNumberPattern());
67}
68//# sourceMappingURL=validate_.js.map
\No newline at end of file