UNPKG

2.63 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = isPossiblePhoneNumber;
7exports.isPossibleNumber = isPossibleNumber;
8
9var _metadata = _interopRequireDefault(require("./metadata"));
10
11var _checkNumberLength = _interopRequireDefault(require("./helpers/checkNumberLength"));
12
13function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
14
15function isPossiblePhoneNumber(input, options, metadata) {
16 /* istanbul ignore if */
17 if (options === undefined) {
18 options = {};
19 }
20
21 metadata = new _metadata["default"](metadata);
22
23 if (options.v2) {
24 if (!input.countryCallingCode) {
25 throw new Error('Invalid phone number object passed');
26 }
27
28 metadata.selectNumberingPlan(input.countryCallingCode);
29 } else {
30 if (!input.phone) {
31 return false;
32 }
33
34 if (input.country) {
35 if (!metadata.hasCountry(input.country)) {
36 throw new Error("Unknown country: ".concat(input.country));
37 }
38
39 metadata.country(input.country);
40 } else {
41 if (!input.countryCallingCode) {
42 throw new Error('Invalid phone number object passed');
43 }
44
45 metadata.selectNumberingPlan(input.countryCallingCode);
46 }
47 }
48
49 if (metadata.possibleLengths()) {
50 return isPossibleNumber(input.phone || input.nationalNumber, metadata);
51 } else {
52 // There was a bug between `1.7.35` and `1.7.37` where "possible_lengths"
53 // were missing for "non-geographical" numbering plans.
54 // Just assume the number is possible in such cases:
55 // it's unlikely that anyone generated their custom metadata
56 // in that short period of time (one day).
57 // This code can be removed in some future major version update.
58 if (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {
59 // "Non-geographic entities" did't have `possibleLengths`
60 // due to a bug in metadata generation process.
61 return true;
62 } else {
63 throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
64 }
65 }
66}
67
68function isPossibleNumber(nationalNumber, metadata) {
69 //, isInternational) {
70 switch ((0, _checkNumberLength["default"])(nationalNumber, metadata)) {
71 case 'IS_POSSIBLE':
72 return true;
73 // This library ignores "local-only" phone numbers (for simplicity).
74 // See the readme for more info on what are "local-only" phone numbers.
75 // case 'IS_POSSIBLE_LOCAL_ONLY':
76 // return !isInternational
77
78 default:
79 return false;
80 }
81}
82//# sourceMappingURL=isPossibleNumber_.js.map
\No newline at end of file