1 | import Metadata from './metadata'
|
2 | import checkNumberLength from './helpers/checkNumberLength'
|
3 |
|
4 | export default function isPossiblePhoneNumber(input, options, metadata) {
|
5 |
|
6 | if (options === undefined) {
|
7 | options = {}
|
8 | }
|
9 |
|
10 | metadata = new Metadata(metadata)
|
11 |
|
12 | if (options.v2) {
|
13 | if (!input.countryCallingCode) {
|
14 | throw new Error('Invalid phone number object passed')
|
15 | }
|
16 | metadata.selectNumberingPlan(input.countryCallingCode)
|
17 | } else {
|
18 | if (!input.phone) {
|
19 | return false
|
20 | }
|
21 | if (input.country) {
|
22 | if (!metadata.hasCountry(input.country)) {
|
23 | throw new Error(`Unknown country: ${input.country}`)
|
24 | }
|
25 | metadata.country(input.country)
|
26 | } else {
|
27 | if (!input.countryCallingCode) {
|
28 | throw new Error('Invalid phone number object passed')
|
29 | }
|
30 | metadata.selectNumberingPlan(input.countryCallingCode)
|
31 | }
|
32 | }
|
33 |
|
34 | if (metadata.possibleLengths()) {
|
35 | return isPossibleNumber(input.phone || input.nationalNumber, metadata)
|
36 | } else {
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | if (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {
|
44 |
|
45 |
|
46 | return true
|
47 | } else {
|
48 | throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
|
49 | }
|
50 | }
|
51 | }
|
52 |
|
53 | export function isPossibleNumber(nationalNumber, metadata) {
|
54 | switch (checkNumberLength(nationalNumber, metadata)) {
|
55 | case 'IS_POSSIBLE':
|
56 | return true
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | default:
|
62 | return false
|
63 | }
|
64 | } |
\ | No newline at end of file |