@routr/processor 2.33.0 → 2.34.0

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.
@@ -9,16 +9,21 @@ export interface FormattedNumber {
9
9
  /**
10
10
  * Writes a phone number the way a Trunk's carrier asks for it.
11
11
  *
12
- * E164 +442079460018
12
+ * E164 +442079460018, with no prefix
13
13
  * E164_WITHOUT_PLUS 442079460018
14
14
  * NATIONAL 02079460018: the digits of the national format, trunk
15
15
  * prefix included where the country uses one, for a
16
- * number of the reference country. A number of another
17
- * calling code, or any number when the reference country
18
- * is empty or unknown, is written +E.164 with no prefix
16
+ * number of the reference country; a number of another
17
+ * calling code in international form
18
+ * INTERNATIONAL 00442079460018: the international prefix of the
19
+ * reference country, the country code and the national
20
+ * number
19
21
  *
20
- * The prefix goes in front of the formatted number. A user that is not a
21
- * phone number is returned as it is, with no prefix.
22
+ * NATIONAL and INTERNATIONAL write +E.164 with no prefix when the reference
23
+ * country is empty, unknown or has no single international prefix. The
24
+ * prefix goes in front of the formatted number, with # escaped as %23 (RFC
25
+ * 3261 section 25.1). A user that is not a phone number is returned as it is,
26
+ * with no prefix.
22
27
  *
23
28
  * @param {string} user - the user part of a URI, as routed
24
29
  * @param {FormatNumberOptions} options - the format, the prefix and the
@@ -33,19 +33,40 @@ const read = (user, country) => [
33
33
  (0, libphonenumber_js_1.parsePhoneNumberFromString)(user, country),
34
34
  (0, libphonenumber_js_1.parsePhoneNumberFromString)(`+${user}`)
35
35
  ].find((number) => number === null || number === void 0 ? void 0 : number.isPossible());
36
+ // The international prefix of a country, digits only, as libphonenumber-js
37
+ // writes it from there toward a number of another country code: 00 from GB,
38
+ // 011 from the US, 0011 from AU. format("IDD") writes a number of the same
39
+ // country in national form, so the probe is always of another country, and
40
+ // its own country code and national number are dropped from the end. Empty
41
+ // when the country has no single international prefix (BR, KR...).
42
+ const internationalPrefix = (country) => {
43
+ var _a;
44
+ const probe = (0, libphonenumber_js_1.parsePhoneNumberFromString)((0, libphonenumber_js_1.getCountryCallingCode)(country) === "44" ? "+12015550123" : "+442079460000");
45
+ const written = probe.format("IDD", { fromCountry: country });
46
+ const tail = probe.countryCallingCode + probe.nationalNumber;
47
+ const digits = (_a = written === null || written === void 0 ? void 0 : written.replace(/\D/g, "")) !== null && _a !== void 0 ? _a : "";
48
+ if (!written || written.startsWith("+") || !digits.endsWith(tail))
49
+ return "";
50
+ return digits.slice(0, -tail.length);
51
+ };
36
52
  /**
37
53
  * Writes a phone number the way a Trunk's carrier asks for it.
38
54
  *
39
- * E164 +442079460018
55
+ * E164 +442079460018, with no prefix
40
56
  * E164_WITHOUT_PLUS 442079460018
41
57
  * NATIONAL 02079460018: the digits of the national format, trunk
42
58
  * prefix included where the country uses one, for a
43
- * number of the reference country. A number of another
44
- * calling code, or any number when the reference country
45
- * is empty or unknown, is written +E.164 with no prefix
59
+ * number of the reference country; a number of another
60
+ * calling code in international form
61
+ * INTERNATIONAL 00442079460018: the international prefix of the
62
+ * reference country, the country code and the national
63
+ * number
46
64
  *
47
- * The prefix goes in front of the formatted number. A user that is not a
48
- * phone number is returned as it is, with no prefix.
65
+ * NATIONAL and INTERNATIONAL write +E.164 with no prefix when the reference
66
+ * country is empty, unknown or has no single international prefix. The
67
+ * prefix goes in front of the formatted number, with # escaped as %23 (RFC
68
+ * 3261 section 25.1). A user that is not a phone number is returned as it is,
69
+ * with no prefix.
49
70
  *
50
71
  * @param {string} user - the user part of a URI, as routed
51
72
  * @param {FormatNumberOptions} options - the format, the prefix and the
@@ -54,7 +75,9 @@ const read = (user, country) => [
54
75
  * describes it
55
76
  */
56
77
  const formatNumber = (user, options) => {
57
- const { format, prefix = "" } = options;
78
+ var _a;
79
+ const { format } = options;
80
+ const prefix = ((_a = options.prefix) !== null && _a !== void 0 ? _a : "").replace(/#/g, "%23");
58
81
  const country = countryOf(options.countryIsoCode);
59
82
  const untouched = { user, keepsUserPhone: true };
60
83
  if (!user || !format || format === common_1.CommonTypes.NumberFormat.UNSPECIFIED) {
@@ -67,16 +90,28 @@ const formatNumber = (user, options) => {
67
90
  user,
68
91
  keepsUserPhone: user.startsWith("+")
69
92
  });
70
- if (format === common_1.CommonTypes.NumberFormat.E164)
71
- return written(prefix + number.number);
72
- if (format === common_1.CommonTypes.NumberFormat.E164_WITHOUT_PLUS) {
73
- return written(prefix + number.number.slice(1));
74
- }
75
- // NATIONAL means something only inside the reference country's own plan
76
- if (!country ||
77
- (0, libphonenumber_js_1.getCountryCallingCode)(country) !== number.countryCallingCode) {
78
- return written(number.number);
93
+ const international = () => {
94
+ const idd = country ? internationalPrefix(country) : "";
95
+ if (!idd)
96
+ return written(number.number);
97
+ return written(prefix + idd + number.countryCallingCode + number.nationalNumber);
98
+ };
99
+ switch (format) {
100
+ case common_1.CommonTypes.NumberFormat.E164:
101
+ return written(number.number);
102
+ case common_1.CommonTypes.NumberFormat.E164_WITHOUT_PLUS:
103
+ return written(prefix + number.number.slice(1));
104
+ case common_1.CommonTypes.NumberFormat.NATIONAL:
105
+ // NATIONAL means something only inside the reference country's own plan
106
+ if (!country ||
107
+ (0, libphonenumber_js_1.getCountryCallingCode)(country) !== number.countryCallingCode) {
108
+ return international();
109
+ }
110
+ return written(prefix + number.formatNational().replace(/\D/g, ""));
111
+ case common_1.CommonTypes.NumberFormat.INTERNATIONAL:
112
+ return international();
113
+ default:
114
+ return untouched;
79
115
  }
80
- return written(prefix + number.formatNational().replace(/\D/g, ""));
81
116
  };
82
117
  exports.formatNumber = formatNumber;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/processor",
3
- "version": "2.33.0",
3
+ "version": "2.34.0",
4
4
  "description": "Processor server and API",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -15,7 +15,7 @@
15
15
  "@fonoster/logger": "0.6.0",
16
16
  "@grpc/grpc-js": "~1.14.6",
17
17
  "@grpc/proto-loader": "~0.7.10",
18
- "@routr/common": "^2.33.0",
18
+ "@routr/common": "^2.34.0",
19
19
  "fp-ts": "^2.11.8",
20
20
  "libphonenumber-js": "1.11.20",
21
21
  "phone": "^3.1.68"
@@ -37,5 +37,5 @@
37
37
  "bugs": {
38
38
  "url": "https://github.com/fonoster/routr/issues"
39
39
  },
40
- "gitHead": "ae7d4c7b239c7ba0c9283c010cb16f388980fa5d"
40
+ "gitHead": "7d2faf42bc43b7f3f1ac7d77097ffbca1b7cf4d2"
41
41
  }