@swan-io/shared-business 18.1.5 → 18.2.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.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swan-io/shared-business",
|
|
3
|
-
"version": "18.
|
|
3
|
+
"version": "18.2.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">22.12.0"
|
|
6
6
|
},
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"react": ">=19.0.0",
|
|
29
29
|
"react-dom": ">=19.0.0",
|
|
30
30
|
"react-native-web": ">=0.21.2",
|
|
31
|
-
"@swan-io/lake": "18.
|
|
31
|
+
"@swan-io/lake": "18.2.0"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@formatjs/intl": "^4.1.16",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"@swan-io/use-form": "^4.0.0",
|
|
39
39
|
"dayjs": "^1.11.21",
|
|
40
40
|
"iban": "^0.0.14",
|
|
41
|
+
"libphonenumber-js": "^1.13.15",
|
|
41
42
|
"react-atomic-state": "^2.1.0",
|
|
42
43
|
"react-dropzone": "^17.0.0",
|
|
43
44
|
"rifm": "^0.12.1",
|
|
@@ -53,6 +54,6 @@
|
|
|
53
54
|
"react-dom": "^19.2.7",
|
|
54
55
|
"react-native-web": "^0.21.2",
|
|
55
56
|
"type-fest": "^5.8.0",
|
|
56
|
-
"@swan-io/lake": "18.
|
|
57
|
+
"@swan-io/lake": "18.2.0"
|
|
57
58
|
}
|
|
58
59
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { france, getCountryByCCA2, getCountryByCCA3 } from "../../constants/countries";
|
|
3
|
+
import { getMostLikelyUserCountry } from "../localization";
|
|
4
|
+
import { maskPhoneNumber, parsePhoneNumber, prefixPhoneNumber } from "../phone";
|
|
5
|
+
const gambia = getCountryByCCA2("GM");
|
|
6
|
+
const jamaica = getCountryByCCA2("JM");
|
|
7
|
+
const curacao = getCountryByCCA2("CW");
|
|
8
|
+
const bonaire = getCountryByCCA2("BQ");
|
|
9
|
+
const vatican = getCountryByCCA2("VA");
|
|
10
|
+
describe("parsePhoneNumber splits an international number into its country and national number", () => {
|
|
11
|
+
it("parses a French mobile number", () => {
|
|
12
|
+
expect(parsePhoneNumber("+33612345678")).toEqual({
|
|
13
|
+
country: france,
|
|
14
|
+
nationalNumber: "612345678",
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
it("parses a 9-digit Gambian number", () => {
|
|
18
|
+
expect(parsePhoneNumber("+220873012345")).toEqual({
|
|
19
|
+
country: gambia,
|
|
20
|
+
nationalNumber: "873012345",
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
it("parses a number from a country sharing the +1 calling code", () => {
|
|
24
|
+
expect(parsePhoneNumber("+18762101234")).toEqual({
|
|
25
|
+
country: jamaica,
|
|
26
|
+
nationalNumber: "8762101234",
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
it("resolves a country listed under several dialling prefixes to the one the country picker offers", () => {
|
|
30
|
+
expect(parsePhoneNumber("+18292345678")).toEqual({
|
|
31
|
+
country: getCountryByCCA3("DOM"),
|
|
32
|
+
nationalNumber: "8292345678",
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
it("parses a number from a country whose dialling prefix is longer than its calling code", () => {
|
|
36
|
+
expect(parsePhoneNumber("+5993181234")).toEqual({
|
|
37
|
+
country: bonaire,
|
|
38
|
+
nationalNumber: "3181234",
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
it("parses a number from a country that dials through another country's code", () => {
|
|
42
|
+
expect(parsePhoneNumber("+390669812345")).toEqual({
|
|
43
|
+
country: vatican,
|
|
44
|
+
nationalNumber: "0669812345",
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
it("parses a number written with separators", () => {
|
|
48
|
+
expect(parsePhoneNumber("+33 6 12 34 56 78")).toEqual({
|
|
49
|
+
country: france,
|
|
50
|
+
nationalNumber: "612345678",
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
describe("parsePhoneNumber falls back to the most likely user country and an empty national number", () => {
|
|
55
|
+
const fallback = { country: getMostLikelyUserCountry(), nationalNumber: "" };
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
vi.unstubAllGlobals();
|
|
58
|
+
});
|
|
59
|
+
it("falls back for an undefined value", () => {
|
|
60
|
+
expect(parsePhoneNumber(undefined)).toEqual(fallback);
|
|
61
|
+
});
|
|
62
|
+
it("falls back for an empty string", () => {
|
|
63
|
+
expect(parsePhoneNumber("")).toEqual(fallback);
|
|
64
|
+
});
|
|
65
|
+
it("falls back for a blank string", () => {
|
|
66
|
+
expect(parsePhoneNumber(" ")).toEqual(fallback);
|
|
67
|
+
});
|
|
68
|
+
it("falls back for a value that is not a phone number", () => {
|
|
69
|
+
expect(parsePhoneNumber("not a phone")).toEqual(fallback);
|
|
70
|
+
});
|
|
71
|
+
it("falls back for a national number without a country code", () => {
|
|
72
|
+
expect(parsePhoneNumber("0612345678")).toEqual(fallback);
|
|
73
|
+
});
|
|
74
|
+
it("falls back for a number that is too short to be valid", () => {
|
|
75
|
+
expect(parsePhoneNumber("+3300")).toEqual(fallback);
|
|
76
|
+
});
|
|
77
|
+
it("falls back for a number of valid length that is not a valid number", () => {
|
|
78
|
+
expect(parsePhoneNumber("+33012345678")).toEqual(fallback);
|
|
79
|
+
});
|
|
80
|
+
it("falls back for a valid number from a country that is not listed", () => {
|
|
81
|
+
expect(parsePhoneNumber("+17215205678")).toEqual(fallback);
|
|
82
|
+
});
|
|
83
|
+
it("follows the browser language for the fallback country", () => {
|
|
84
|
+
vi.stubGlobal("navigator", { languages: ["de-DE"] });
|
|
85
|
+
expect(parsePhoneNumber(undefined)).toEqual({
|
|
86
|
+
country: getCountryByCCA2("DE"),
|
|
87
|
+
nationalNumber: "",
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
describe("prefixPhoneNumber turns a national number into an E.164 number", () => {
|
|
92
|
+
it("prefixes a French national number", () => {
|
|
93
|
+
expect(prefixPhoneNumber(france, "612345678")).toEqual({
|
|
94
|
+
valid: true,
|
|
95
|
+
e164: "+33612345678",
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
it("accepts a French number with its trunk prefix", () => {
|
|
99
|
+
expect(prefixPhoneNumber(france, "0612345678")).toEqual({
|
|
100
|
+
valid: true,
|
|
101
|
+
e164: "+33612345678",
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
it("ignores spaces and dashes", () => {
|
|
105
|
+
expect(prefixPhoneNumber(france, "6 12-34-56-78")).toEqual({
|
|
106
|
+
valid: true,
|
|
107
|
+
e164: "+33612345678",
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
it("ignores characters that are not part of a phone number", () => {
|
|
111
|
+
expect(prefixPhoneNumber(france, "06_12_34_56_78")).toEqual({
|
|
112
|
+
valid: true,
|
|
113
|
+
e164: "+33612345678",
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
it("accepts a 9-digit Gambian number", () => {
|
|
117
|
+
expect(prefixPhoneNumber(gambia, "873012345")).toEqual({
|
|
118
|
+
valid: true,
|
|
119
|
+
e164: "+220873012345",
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
it("accepts an international number whatever the selected country", () => {
|
|
123
|
+
expect(prefixPhoneNumber(france, "+4915112345678")).toEqual({
|
|
124
|
+
valid: true,
|
|
125
|
+
e164: "+4915112345678",
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
it("prefixes a number from a country sharing the +1 calling code", () => {
|
|
129
|
+
expect(prefixPhoneNumber(jamaica, "8762101234")).toEqual({
|
|
130
|
+
valid: true,
|
|
131
|
+
e164: "+18762101234",
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
it("prefixes a number from a country whose dialling prefix is longer than its calling code", () => {
|
|
135
|
+
expect(prefixPhoneNumber(curacao, "95181234")).toEqual({
|
|
136
|
+
valid: true,
|
|
137
|
+
e164: "+59995181234",
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
it("prefixes a number from a country that dials through another country's code", () => {
|
|
141
|
+
expect(prefixPhoneNumber(vatican, "0669812345")).toEqual({
|
|
142
|
+
valid: true,
|
|
143
|
+
e164: "+390669812345",
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
it("accepts the digits typed after a dialling prefix that already holds the area code", () => {
|
|
147
|
+
expect(prefixPhoneNumber(jamaica, "2101234")).toEqual({
|
|
148
|
+
valid: true,
|
|
149
|
+
e164: "+18762101234",
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
it("accepts the digits typed after a dialling prefix longer than the calling code", () => {
|
|
153
|
+
expect(prefixPhoneNumber(curacao, "5181234")).toEqual({
|
|
154
|
+
valid: true,
|
|
155
|
+
e164: "+59995181234",
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
it("accepts a six-digit value typed after a dialling prefix", () => {
|
|
159
|
+
expect(prefixPhoneNumber(bonaire, "171234")).toEqual({
|
|
160
|
+
valid: true,
|
|
161
|
+
e164: "+5997171234",
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
it("ignores characters that are not part of a phone number in the digits typed after a dialling prefix", () => {
|
|
165
|
+
expect(prefixPhoneNumber(jamaica, "210_1234")).toEqual({
|
|
166
|
+
valid: true,
|
|
167
|
+
e164: "+18762101234",
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
it("rejects digits typed after a dialling prefix that do not make a valid number", () => {
|
|
171
|
+
expect(prefixPhoneNumber(jamaica, "123")).toEqual({ valid: false });
|
|
172
|
+
});
|
|
173
|
+
it("reads a value as typed after the dialling prefix when its other reading is a Canadian number", () => {
|
|
174
|
+
expect(prefixPhoneNumber(jamaica, "3101234")).toEqual({
|
|
175
|
+
valid: true,
|
|
176
|
+
e164: "+18763101234",
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
it("reads a value as typed after the dialling prefix when its other reading is a Bonaire number", () => {
|
|
180
|
+
expect(prefixPhoneNumber(curacao, "7201234")).toEqual({
|
|
181
|
+
valid: true,
|
|
182
|
+
e164: "+59997201234",
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
it("keeps a full national number unchanged when both readings belong to the selected country", () => {
|
|
186
|
+
expect(prefixPhoneNumber(curacao, "6001234")).toEqual({
|
|
187
|
+
valid: true,
|
|
188
|
+
e164: "+5996001234",
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
it("accepts a full national number from another area code of the selected country", () => {
|
|
192
|
+
expect(prefixPhoneNumber(getCountryByCCA3("DOM"), "8292345678")).toEqual({
|
|
193
|
+
valid: true,
|
|
194
|
+
e164: "+18292345678",
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
it("accepts a number from another country sharing the selected calling code", () => {
|
|
198
|
+
expect(prefixPhoneNumber(getCountryByCCA2("US"), "8762101234")).toEqual({
|
|
199
|
+
valid: true,
|
|
200
|
+
e164: "+18762101234",
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
it("accepts a full national number of another country when the dialling prefix holds an area code", () => {
|
|
204
|
+
expect(prefixPhoneNumber(bonaire, "95181234")).toEqual({
|
|
205
|
+
valid: true,
|
|
206
|
+
e164: "+59995181234",
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
it("accepts an international number when the dialling prefix holds an area code", () => {
|
|
210
|
+
expect(prefixPhoneNumber(jamaica, "+33612345678")).toEqual({
|
|
211
|
+
valid: true,
|
|
212
|
+
e164: "+33612345678",
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
it("prefixes a number from a territory that borrows another country's numbering plan", () => {
|
|
216
|
+
expect(prefixPhoneNumber(getCountryByCCA2("BV"), "40612345")).toEqual({
|
|
217
|
+
valid: true,
|
|
218
|
+
e164: "+4740612345",
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
it("rejects a number of valid length that is not a valid number", () => {
|
|
222
|
+
expect(prefixPhoneNumber(france, "0012345678")).toEqual({ valid: false });
|
|
223
|
+
});
|
|
224
|
+
it("rejects a number that is too long", () => {
|
|
225
|
+
expect(prefixPhoneNumber(france, "999999999999999")).toEqual({ valid: false });
|
|
226
|
+
});
|
|
227
|
+
it("rejects a number that is too short", () => {
|
|
228
|
+
expect(prefixPhoneNumber(france, "0")).toEqual({ valid: false });
|
|
229
|
+
});
|
|
230
|
+
it("rejects an empty national number", () => {
|
|
231
|
+
expect(prefixPhoneNumber(france, "")).toEqual({ valid: false });
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
describe("maskPhoneNumber hides the middle digits of a phone number", () => {
|
|
235
|
+
it("masks a 10-digit number", () => {
|
|
236
|
+
expect(maskPhoneNumber("0612345678")).toBe("061****678");
|
|
237
|
+
});
|
|
238
|
+
it("masks a 9-digit number", () => {
|
|
239
|
+
expect(maskPhoneNumber("612345678")).toBe("612***678");
|
|
240
|
+
});
|
|
241
|
+
it("grows the asterisk run with the number length", () => {
|
|
242
|
+
expect(maskPhoneNumber("33612345678")).toBe("336*****678");
|
|
243
|
+
});
|
|
244
|
+
it("keeps a non-digit prefix", () => {
|
|
245
|
+
expect(maskPhoneNumber("+33612345678")).toBe("+336*****678");
|
|
246
|
+
});
|
|
247
|
+
it("masks the shortest run it recognises", () => {
|
|
248
|
+
expect(maskPhoneNumber("1234567")).toBe("123*567");
|
|
249
|
+
});
|
|
250
|
+
it("leaves a run of fewer than 7 digits unchanged", () => {
|
|
251
|
+
expect(maskPhoneNumber("123456")).toBe("123456");
|
|
252
|
+
});
|
|
253
|
+
it("leaves a number written with separators unmasked", () => {
|
|
254
|
+
expect(maskPhoneNumber("+33 612 345 678")).toBe("+33 612 345 678");
|
|
255
|
+
});
|
|
256
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { E164Number } from "libphonenumber-js";
|
|
2
|
+
import { Country } from "../constants/countries";
|
|
3
|
+
export declare const parsePhoneNumber: (value?: string) => {
|
|
4
|
+
country: Country;
|
|
5
|
+
nationalNumber: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const prefixPhoneNumber: (country: Country, nationalNumber: string) => {
|
|
8
|
+
valid: true;
|
|
9
|
+
e164: E164Number;
|
|
10
|
+
} | {
|
|
11
|
+
valid: false;
|
|
12
|
+
};
|
|
13
|
+
export declare const maskPhoneNumber: (value: string) => string;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Lazy, Result } from "@bloodyowl/boxed";
|
|
2
|
+
import { isNullish } from "@swan-io/lake/src/utils/nullish";
|
|
3
|
+
import { getCountryCallingCode, isSupportedCountry, parsePhoneNumberWithError, } from "libphonenumber-js";
|
|
4
|
+
import { getCountryByCCA2, isCountryCCA2 } from "../constants/countries";
|
|
5
|
+
import { getMostLikelyUserCountry } from "./localization";
|
|
6
|
+
export const parsePhoneNumber = (value) => {
|
|
7
|
+
const fallback = Lazy(() => ({ country: getMostLikelyUserCountry(), nationalNumber: "" }));
|
|
8
|
+
if (isNullish(value) || value.trim() === "") {
|
|
9
|
+
return fallback.get();
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
const phoneNumber = parsePhoneNumberWithError(value);
|
|
13
|
+
const { country, nationalNumber } = phoneNumber;
|
|
14
|
+
if (!phoneNumber.isValid() || !isCountryCCA2(country)) {
|
|
15
|
+
return fallback.get();
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
country: getCountryByCCA2(country),
|
|
19
|
+
nationalNumber: String(nationalNumber).replace(/[^0-9]/g, ""),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return fallback.get();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export const prefixPhoneNumber = (country, nationalNumber) => {
|
|
27
|
+
const sanitized = nationalNumber.replace(/[^+0-9]/g, "");
|
|
28
|
+
const callingCode = isSupportedCountry(country.cca2)
|
|
29
|
+
? getCountryCallingCode(country.cca2)
|
|
30
|
+
: country.idd;
|
|
31
|
+
const areaCode = country.idd.startsWith(callingCode) ? country.idd.slice(callingCode.length) : "";
|
|
32
|
+
const parse = (value) => Result.fromExecution(() => parsePhoneNumberWithError(value, { defaultCallingCode: callingCode }))
|
|
33
|
+
.toOption()
|
|
34
|
+
.filter(phoneNumber => phoneNumber.isValid());
|
|
35
|
+
const national = parse(sanitized);
|
|
36
|
+
const local = parse(`${areaCode}${sanitized}`);
|
|
37
|
+
return national
|
|
38
|
+
.filter(phoneNumber => phoneNumber.country === country.cca2)
|
|
39
|
+
.orElse(local)
|
|
40
|
+
.orElse(national)
|
|
41
|
+
.match({
|
|
42
|
+
Some: ({ number }) => ({ valid: true, e164: number }),
|
|
43
|
+
None: () => ({ valid: false }),
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
export const maskPhoneNumber = (value) => value.replace(/(\d{3})(\d+)(\d{3})/, (_, $1, $2, $3) => `${$1}${"*".repeat($2.length)}${$3}`);
|