@superdispatch/phones 0.25.2 → 0.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,83 +4,68 @@ import { DEFAULT_COUNTRY, getCountryCode, toCountryISO } from "../country-code-m
4
4
  var PLUS_SIGN = '+';
5
5
  var NON_PHONE_SYMBOLS_PATTERN = /[^+\d]/g;
6
6
  var PHONE_PATTERN = /^\+?\d+/g;
7
-
8
7
  function getPrefix(country) {
9
8
  return PLUS_SIGN + String(getCountryCode(toCountryISO(country)));
10
9
  }
11
-
12
10
  function trim(input) {
13
11
  return typeof input == 'string' ? input.trim() : undefined;
14
12
  }
15
-
16
13
  function normalize(input) {
17
14
  if (typeof input == 'string') {
18
15
  var matches = input.replace(NON_PHONE_SYMBOLS_PATTERN, '').match(PHONE_PATTERN);
19
16
  if (matches !== null && matches !== void 0 && matches[0]) return matches[0];
20
17
  }
21
-
22
18
  return '';
23
19
  }
24
-
25
20
  function normalizeNationalNumber(country, input) {
26
21
  var phone = normalize(input);
27
22
  var prefix = getPrefix(country);
28
-
29
23
  if (phone.startsWith(PLUS_SIGN)) {
30
24
  return phone.slice(prefix.length);
31
25
  }
32
-
33
26
  return phone;
34
27
  }
35
-
36
28
  export class PhoneService {
37
29
  static load() {
38
30
  return loadAPN().then(APN => new PhoneService(APN));
39
31
  }
40
-
41
32
  static checkPossibility(input) {
42
33
  return this.load().then(phoneService => phoneService.checkPossibility(input));
43
34
  }
44
-
45
35
  static validate(input, rules) {
46
36
  return this.load().then(phoneService => phoneService.validate(input, rules));
47
37
  }
48
-
49
38
  constructor(APN) {
50
39
  this.APN = APN;
51
40
  }
52
-
53
41
  createAPN(phone, country) {
54
42
  var asYouType = this.APN.getAsYouType(toCountryISO(country));
55
43
  asYouType.reset(normalize(phone));
56
44
  return asYouType.getPhoneNumber();
57
45
  }
58
-
59
46
  getJSON(phone, country) {
60
- return this.createAPN(phone, country).toJSON();
47
+ return this.createAPN(phone, country);
61
48
  }
62
-
63
49
  checkPossibility(input) {
64
50
  var phone = trim(input);
65
-
66
51
  if (!phone) {
67
52
  return 'unknown';
68
53
  }
69
-
70
- var apn = phone.startsWith(PLUS_SIGN) ? new this.APN(phone) : new this.APN(phone, DEFAULT_COUNTRY);
54
+ var apn = phone.startsWith(PLUS_SIGN) ? this.APN.parsePhoneNumber(phone) : this.APN.parsePhoneNumber(phone, {
55
+ regionCode: DEFAULT_COUNTRY
56
+ });
71
57
  var {
72
58
  valid,
73
59
  possible,
74
60
  possibility
75
- } = apn.toJSON(); // Avoid false positive short phone numbers.
61
+ } = apn;
76
62
 
63
+ // Avoid false positive short phone numbers.
77
64
  if (!valid && possible) {
78
65
  return 'invalid-number';
79
66
  }
80
-
81
67
  return possibility;
82
68
  }
83
-
84
69
  validate(input) {
85
70
  var {
86
71
  required,
@@ -90,40 +75,30 @@ export class PhoneService {
90
75
  tooShortMessage = 'Phone number is too short'
91
76
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
92
77
  var phone = trim(input);
93
-
94
78
  if (!phone) {
95
79
  if (required) {
96
80
  return requiredMessage;
97
81
  }
98
-
99
82
  return undefined;
100
83
  }
101
-
102
84
  switch (this.checkPossibility(phone)) {
103
85
  case 'is-possible':
104
86
  return undefined;
105
-
106
87
  case 'too-long':
107
88
  return tooLongMessage;
108
-
109
89
  case 'too-short':
110
90
  return tooShortMessage;
111
91
  }
112
-
113
92
  return invalidMessage;
114
93
  }
115
-
116
94
  deletePrefix(phone, country) {
117
95
  var prefix = getPrefix(country);
118
-
119
96
  if (phone.startsWith(PLUS_SIGN)) {
120
97
  var subNumber = phone.slice(prefix.length);
121
98
  return trim(subNumber);
122
99
  }
123
-
124
100
  return phone;
125
101
  }
126
-
127
102
  getInfo(phone) {
128
103
  var {
129
104
  regionCode,
@@ -134,82 +109,70 @@ export class PhoneService {
134
109
  } = this.getJSON(phone);
135
110
  var nationalNumber = '';
136
111
  var country = toCountryISO(regionCode);
137
-
138
112
  if (!internationalNumber) {
139
113
  nationalNumber = normalizeNationalNumber(country, input);
140
114
  } else {
141
115
  nationalNumber = this.deletePrefix(internationalNumber, country);
142
116
  }
143
-
144
117
  return {
145
118
  country,
146
119
  nationalNumber
147
120
  };
148
121
  }
149
-
150
122
  format(input) {
123
+ var _apn$number;
151
124
  var {
152
125
  fallback = '',
153
126
  format = 'e164',
154
127
  country: countryOption
155
128
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
156
129
  var phone = normalize(input);
157
-
158
130
  if (!phone) {
159
131
  return fallback;
160
132
  }
161
-
162
- var apn = this.createAPN(phone, countryOption);
163
- var country = countryOption || toCountryISO(apn.getRegionCode());
164
- var formatted = format === 'national' ? this.formatNationalNumber(apn, country) : apn.getNumber(format);
165
-
133
+ var apn = this.APN.parsePhoneNumber(phone, {
134
+ regionCode: countryOption
135
+ });
136
+ var country = countryOption || toCountryISO(apn.regionCode);
137
+ var international = (_apn$number = apn.number) === null || _apn$number === void 0 ? void 0 : _apn$number.international;
138
+ var formattedNumber = apn.number ? apn.number[format] : '';
139
+ var {
140
+ regionCode
141
+ } = apn;
142
+ var formatted = format === 'national' ? this.formatNationalNumber(country, international) : formattedNumber;
166
143
  if (!formatted) {
167
- return this.customFormat(apn, phone, format);
144
+ return this.customFormat(phone, format, regionCode);
168
145
  }
169
-
170
146
  return formatted;
171
147
  }
172
-
173
- formatNationalNumber(apn, country) {
174
- var internationalNumber = apn.getNumber('international');
175
-
148
+ formatNationalNumber(country, internationalNumber) {
176
149
  if (!internationalNumber) {
177
150
  return undefined;
178
151
  }
179
-
180
152
  return this.deletePrefix(internationalNumber, country);
181
153
  }
182
-
183
- customFormat(apn, phone, format) {
154
+ customFormat(phone, format, regionCode) {
184
155
  var formatted = '';
185
- var country = toCountryISO(apn.getRegionCode());
156
+ var country = toCountryISO(regionCode);
186
157
  var nationalNumber = normalizeNationalNumber(country, phone);
187
-
188
158
  if (format === 'national') {
189
159
  return nationalNumber;
190
160
  }
191
-
192
161
  var prefix = '';
193
162
  var separator = '';
194
-
195
163
  if (format === 'rfc3966') {
196
164
  prefix = 'tel:';
197
165
  separator = '-';
198
166
  }
199
-
200
167
  if (format === 'international') {
201
168
  separator = ' ';
202
169
  }
203
-
204
170
  formatted = prefix + getPrefix(country);
205
-
206
171
  if (nationalNumber) {
207
172
  formatted += separator + nationalNumber;
208
173
  }
209
-
210
174
  return formatted;
211
175
  }
212
-
213
176
  }
214
177
  export function usePhoneService() {
215
178
  var APN = getAPN();
@@ -20,10 +20,9 @@ export function PhoneText(_ref) {
20
20
  }
21
21
  export function SuspendedPhoneText(_ref2) {
22
22
  var {
23
- suspenseFallback = null
24
- } = _ref2,
25
- props = _objectWithoutProperties(_ref2, _excluded);
26
-
23
+ suspenseFallback = null
24
+ } = _ref2,
25
+ props = _objectWithoutProperties(_ref2, _excluded);
27
26
  return /*#__PURE__*/_jsx(Suspense, {
28
27
  fallback: suspenseFallback,
29
28
  children: /*#__PURE__*/_jsx(PhoneText, _objectSpread({}, props))
@@ -1,4 +1,4 @@
1
- import AwesomePhoneNumber, { PhoneNumber } from 'awesome-phonenumber';
1
+ import { PhoneNumberParseOptions, ParsedPhoneNumber, AsYouType, PhoneNumberTypes } from 'awesome-phonenumber';
2
2
  import { ForwardRefExoticComponent, RefAttributes, ReactNode, ReactElement } from 'react';
3
3
  import { BaseTextFieldProps, LinkProps } from '@material-ui/core';
4
4
 
@@ -11,8 +11,11 @@ declare function toCountryISO(value: unknown): CountryISO;
11
11
  declare function getCountryCode(country: CountryISO): number;
12
12
  declare function formatCountry(country: CountryISO): string;
13
13
 
14
- declare type APNType = AwesomePhoneNumber;
15
- declare type APNStatic = typeof AwesomePhoneNumber;
14
+ interface APNStatic {
15
+ parsePhoneNumber: (input: string, options?: PhoneNumberParseOptions) => ParsedPhoneNumber;
16
+ getAsYouType: (regionCode: string) => AsYouType;
17
+ getExample: (regionCode: string, type?: PhoneNumberTypes) => ParsedPhoneNumber;
18
+ }
16
19
 
17
20
  declare type PhoneNumberPossibility = 'is-possible' | 'invalid-country-code' | 'invalid-number' | 'too-long' | 'too-short' | 'unknown';
18
21
  interface PhoneNumberJSON {
@@ -23,6 +26,7 @@ interface PhoneNumberJSON {
23
26
  number: {
24
27
  input: string;
25
28
  international?: string;
29
+ national?: string;
26
30
  };
27
31
  }
28
32
  interface PhoneNumberInfo {
@@ -46,16 +50,16 @@ declare class PhoneService {
46
50
  static load(): Promise<PhoneService>;
47
51
  static checkPossibility(input: unknown): Promise<PhoneNumberPossibility>;
48
52
  static validate(input: unknown, rules?: PhoneValidationRules): Promise<string | undefined>;
49
- readonly APN: typeof PhoneNumber;
53
+ readonly APN: APNStatic;
50
54
  constructor(APN: APNStatic);
51
- createAPN(phone: string, country?: CountryISO): APNType;
55
+ createAPN(phone: string, country?: CountryISO): ParsedPhoneNumber;
52
56
  getJSON(phone: string, country?: CountryISO): PhoneNumberJSON;
53
57
  checkPossibility(input: unknown): PhoneNumberPossibility;
54
58
  validate(input: unknown, { required, requiredMessage, invalidMessage, tooLongMessage, tooShortMessage, }?: PhoneValidationRules): string | undefined;
55
59
  deletePrefix(phone: string, country: CountryISO): string;
56
60
  getInfo(phone: string): PhoneNumberInfo;
57
61
  format(input: unknown, { fallback, format, country: countryOption, }?: PhoneFormatOptions): string;
58
- formatNationalNumber(apn: APNType, country: CountryISO): string | undefined;
62
+ formatNationalNumber(country: CountryISO, internationalNumber?: string): string | undefined;
59
63
  private customFormat;
60
64
  }
61
65
  declare function usePhoneService(): PhoneService;