@cimplify/sdk 0.16.3 → 0.17.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.
@@ -0,0 +1,170 @@
1
+ 'use strict';
2
+
3
+ var libphonenumberJs = require('libphonenumber-js');
4
+
5
+ // src/utils/phone.ts
6
+ var ISO3_TO_COUNTRY_CODE = {
7
+ AGO: "AO",
8
+ ARE: "AE",
9
+ AUS: "AU",
10
+ BDI: "BI",
11
+ BRA: "BR",
12
+ BWA: "BW",
13
+ CAN: "CA",
14
+ CHE: "CH",
15
+ CHN: "CN",
16
+ COD: "CD",
17
+ EGY: "EG",
18
+ ETH: "ET",
19
+ GHA: "GH",
20
+ GIN: "GN",
21
+ GMB: "GM",
22
+ GBR: "GB",
23
+ HKG: "HK",
24
+ IDN: "ID",
25
+ IND: "IN",
26
+ ISR: "IL",
27
+ JPN: "JP",
28
+ KEN: "KE",
29
+ KOR: "KR",
30
+ LBR: "LR",
31
+ MUS: "MU",
32
+ MEX: "MX",
33
+ MOZ: "MZ",
34
+ MWI: "MW",
35
+ MYS: "MY",
36
+ NAM: "NA",
37
+ NGA: "NG",
38
+ PHL: "PH",
39
+ RWA: "RW",
40
+ SAU: "SA",
41
+ SGP: "SG",
42
+ SLE: "SL",
43
+ THA: "TH",
44
+ TUR: "TR",
45
+ TWN: "TW",
46
+ TZA: "TZ",
47
+ UGA: "UG",
48
+ USA: "US",
49
+ VNM: "VN",
50
+ ZAF: "ZA",
51
+ ZMB: "ZM"
52
+ };
53
+ var CURRENCY_TO_COUNTRY_CODE = {
54
+ AED: "AE",
55
+ AOA: "AO",
56
+ AUD: "AU",
57
+ BIF: "BI",
58
+ BWP: "BW",
59
+ BRL: "BR",
60
+ CAD: "CA",
61
+ CDF: "CD",
62
+ CHF: "CH",
63
+ CNY: "CN",
64
+ EGP: "EG",
65
+ ETB: "ET",
66
+ GHS: "GH",
67
+ GMD: "GM",
68
+ GNF: "GN",
69
+ GBP: "GB",
70
+ HKD: "HK",
71
+ IDR: "ID",
72
+ ILS: "IL",
73
+ INR: "IN",
74
+ JPY: "JP",
75
+ KES: "KE",
76
+ KRW: "KR",
77
+ LRD: "LR",
78
+ MUR: "MU",
79
+ MWK: "MW",
80
+ MXN: "MX",
81
+ MYR: "MY",
82
+ MZN: "MZ",
83
+ NAD: "NA",
84
+ NGN: "NG",
85
+ PHP: "PH",
86
+ RWF: "RW",
87
+ SAR: "SA",
88
+ SGD: "SG",
89
+ SLL: "SL",
90
+ THB: "TH",
91
+ TRY: "TR",
92
+ TWD: "TW",
93
+ TZS: "TZ",
94
+ UGX: "UG",
95
+ USD: "US",
96
+ VND: "VN",
97
+ ZAR: "ZA",
98
+ ZMW: "ZM"
99
+ };
100
+ function normalizeCountryCode(value) {
101
+ const normalized = value?.trim().toUpperCase();
102
+ if (!normalized) {
103
+ return void 0;
104
+ }
105
+ if (normalized.length === 2) {
106
+ return normalized;
107
+ }
108
+ return ISO3_TO_COUNTRY_CODE[normalized];
109
+ }
110
+ function parsePhoneCandidate(value, country) {
111
+ try {
112
+ return country ? libphonenumberJs.parsePhoneNumberFromString(value, country) : libphonenumberJs.parsePhoneNumberFromString(value);
113
+ } catch {
114
+ return void 0;
115
+ }
116
+ }
117
+ function resolvePhoneCountryCode(options = {}) {
118
+ const explicitCountry = normalizeCountryCode(options.country);
119
+ if (explicitCountry) {
120
+ return explicitCountry;
121
+ }
122
+ const currency = options.currency?.trim().toUpperCase();
123
+ if (!currency) {
124
+ return void 0;
125
+ }
126
+ return CURRENCY_TO_COUNTRY_CODE[currency];
127
+ }
128
+ function normalizePhoneToE164(raw, options = {}) {
129
+ const trimmed = raw.trim();
130
+ if (!trimmed) {
131
+ return null;
132
+ }
133
+ const country = resolvePhoneCountryCode(options);
134
+ const digits = trimmed.replace(/\D/g, "");
135
+ const internationalDigits = digits.startsWith("00") ? digits.slice(2) : digits;
136
+ const attempts = [];
137
+ const seen = /* @__PURE__ */ new Set();
138
+ function addAttempt(value, nextCountry) {
139
+ const key = `${nextCountry || ""}:${value}`;
140
+ if (seen.has(key)) {
141
+ return;
142
+ }
143
+ seen.add(key);
144
+ attempts.push({ value, country: nextCountry });
145
+ }
146
+ if (country) {
147
+ addAttempt(trimmed, country);
148
+ }
149
+ addAttempt(trimmed);
150
+ if (country && digits) {
151
+ addAttempt(digits, country);
152
+ }
153
+ if (internationalDigits) {
154
+ addAttempt(`+${internationalDigits}`);
155
+ }
156
+ for (const attempt of attempts) {
157
+ const parsed = parsePhoneCandidate(attempt.value, attempt.country);
158
+ if (parsed?.isValid()) {
159
+ return parsed.format("E.164");
160
+ }
161
+ }
162
+ return null;
163
+ }
164
+ function isValidPhoneForContext(raw, options = {}) {
165
+ return normalizePhoneToE164(raw, options) !== null;
166
+ }
167
+
168
+ exports.isValidPhoneForContext = isValidPhoneForContext;
169
+ exports.normalizePhoneToE164 = normalizePhoneToE164;
170
+ exports.resolvePhoneCountryCode = resolvePhoneCountryCode;
@@ -0,0 +1,166 @@
1
+ import { parsePhoneNumberFromString } from 'libphonenumber-js';
2
+
3
+ // src/utils/phone.ts
4
+ var ISO3_TO_COUNTRY_CODE = {
5
+ AGO: "AO",
6
+ ARE: "AE",
7
+ AUS: "AU",
8
+ BDI: "BI",
9
+ BRA: "BR",
10
+ BWA: "BW",
11
+ CAN: "CA",
12
+ CHE: "CH",
13
+ CHN: "CN",
14
+ COD: "CD",
15
+ EGY: "EG",
16
+ ETH: "ET",
17
+ GHA: "GH",
18
+ GIN: "GN",
19
+ GMB: "GM",
20
+ GBR: "GB",
21
+ HKG: "HK",
22
+ IDN: "ID",
23
+ IND: "IN",
24
+ ISR: "IL",
25
+ JPN: "JP",
26
+ KEN: "KE",
27
+ KOR: "KR",
28
+ LBR: "LR",
29
+ MUS: "MU",
30
+ MEX: "MX",
31
+ MOZ: "MZ",
32
+ MWI: "MW",
33
+ MYS: "MY",
34
+ NAM: "NA",
35
+ NGA: "NG",
36
+ PHL: "PH",
37
+ RWA: "RW",
38
+ SAU: "SA",
39
+ SGP: "SG",
40
+ SLE: "SL",
41
+ THA: "TH",
42
+ TUR: "TR",
43
+ TWN: "TW",
44
+ TZA: "TZ",
45
+ UGA: "UG",
46
+ USA: "US",
47
+ VNM: "VN",
48
+ ZAF: "ZA",
49
+ ZMB: "ZM"
50
+ };
51
+ var CURRENCY_TO_COUNTRY_CODE = {
52
+ AED: "AE",
53
+ AOA: "AO",
54
+ AUD: "AU",
55
+ BIF: "BI",
56
+ BWP: "BW",
57
+ BRL: "BR",
58
+ CAD: "CA",
59
+ CDF: "CD",
60
+ CHF: "CH",
61
+ CNY: "CN",
62
+ EGP: "EG",
63
+ ETB: "ET",
64
+ GHS: "GH",
65
+ GMD: "GM",
66
+ GNF: "GN",
67
+ GBP: "GB",
68
+ HKD: "HK",
69
+ IDR: "ID",
70
+ ILS: "IL",
71
+ INR: "IN",
72
+ JPY: "JP",
73
+ KES: "KE",
74
+ KRW: "KR",
75
+ LRD: "LR",
76
+ MUR: "MU",
77
+ MWK: "MW",
78
+ MXN: "MX",
79
+ MYR: "MY",
80
+ MZN: "MZ",
81
+ NAD: "NA",
82
+ NGN: "NG",
83
+ PHP: "PH",
84
+ RWF: "RW",
85
+ SAR: "SA",
86
+ SGD: "SG",
87
+ SLL: "SL",
88
+ THB: "TH",
89
+ TRY: "TR",
90
+ TWD: "TW",
91
+ TZS: "TZ",
92
+ UGX: "UG",
93
+ USD: "US",
94
+ VND: "VN",
95
+ ZAR: "ZA",
96
+ ZMW: "ZM"
97
+ };
98
+ function normalizeCountryCode(value) {
99
+ const normalized = value?.trim().toUpperCase();
100
+ if (!normalized) {
101
+ return void 0;
102
+ }
103
+ if (normalized.length === 2) {
104
+ return normalized;
105
+ }
106
+ return ISO3_TO_COUNTRY_CODE[normalized];
107
+ }
108
+ function parsePhoneCandidate(value, country) {
109
+ try {
110
+ return country ? parsePhoneNumberFromString(value, country) : parsePhoneNumberFromString(value);
111
+ } catch {
112
+ return void 0;
113
+ }
114
+ }
115
+ function resolvePhoneCountryCode(options = {}) {
116
+ const explicitCountry = normalizeCountryCode(options.country);
117
+ if (explicitCountry) {
118
+ return explicitCountry;
119
+ }
120
+ const currency = options.currency?.trim().toUpperCase();
121
+ if (!currency) {
122
+ return void 0;
123
+ }
124
+ return CURRENCY_TO_COUNTRY_CODE[currency];
125
+ }
126
+ function normalizePhoneToE164(raw, options = {}) {
127
+ const trimmed = raw.trim();
128
+ if (!trimmed) {
129
+ return null;
130
+ }
131
+ const country = resolvePhoneCountryCode(options);
132
+ const digits = trimmed.replace(/\D/g, "");
133
+ const internationalDigits = digits.startsWith("00") ? digits.slice(2) : digits;
134
+ const attempts = [];
135
+ const seen = /* @__PURE__ */ new Set();
136
+ function addAttempt(value, nextCountry) {
137
+ const key = `${nextCountry || ""}:${value}`;
138
+ if (seen.has(key)) {
139
+ return;
140
+ }
141
+ seen.add(key);
142
+ attempts.push({ value, country: nextCountry });
143
+ }
144
+ if (country) {
145
+ addAttempt(trimmed, country);
146
+ }
147
+ addAttempt(trimmed);
148
+ if (country && digits) {
149
+ addAttempt(digits, country);
150
+ }
151
+ if (internationalDigits) {
152
+ addAttempt(`+${internationalDigits}`);
153
+ }
154
+ for (const attempt of attempts) {
155
+ const parsed = parsePhoneCandidate(attempt.value, attempt.country);
156
+ if (parsed?.isValid()) {
157
+ return parsed.format("E.164");
158
+ }
159
+ }
160
+ return null;
161
+ }
162
+ function isValidPhoneForContext(raw, options = {}) {
163
+ return normalizePhoneToE164(raw, options) !== null;
164
+ }
165
+
166
+ export { isValidPhoneForContext, normalizePhoneToE164, resolvePhoneCountryCode };