@sprucelabs/schema 32.2.0 → 32.2.2
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.
|
@@ -31,6 +31,11 @@ const COUNTRY_FORMATS = [
|
|
|
31
31
|
},
|
|
32
32
|
];
|
|
33
33
|
const DEFAULT_COUNTRY = (_a = COUNTRY_FORMATS.find((format) => format.code === '1')) !== null && _a !== void 0 ? _a : COUNTRY_FORMATS[0];
|
|
34
|
+
const createAdHocFormat = (code) => ({
|
|
35
|
+
code,
|
|
36
|
+
groupSizes: [],
|
|
37
|
+
groupSeparator: DEFAULT_CODE_SEPARATOR,
|
|
38
|
+
});
|
|
34
39
|
export function isValidNumber(number) {
|
|
35
40
|
var _a;
|
|
36
41
|
if (LETTER_PATTERN.test(number)) {
|
|
@@ -58,7 +63,8 @@ export default function formatPhoneNumber(value, shouldFailSilently = true) {
|
|
|
58
63
|
const parsed = parseInput(value);
|
|
59
64
|
const countryFormat = detectCountryFormat(parsed);
|
|
60
65
|
const localDigits = stripCountryCodeFromDigits(parsed.digits, countryFormat.code);
|
|
61
|
-
|
|
66
|
+
const isCodeOnly = parsed.digits.length > 0 && parsed.digits === countryFormat.code;
|
|
67
|
+
if (!localDigits.length && !isCodeOnly) {
|
|
62
68
|
if (!shouldFailSilently) {
|
|
63
69
|
throw new Error('INVALID_PHONE_NUMBER');
|
|
64
70
|
}
|
|
@@ -66,10 +72,14 @@ export default function formatPhoneNumber(value, shouldFailSilently = true) {
|
|
|
66
72
|
}
|
|
67
73
|
const formattedLocal = formatLocalDigits(localDigits, countryFormat);
|
|
68
74
|
const codeSeparator = (_a = countryFormat.codeSeparator) !== null && _a !== void 0 ? _a : DEFAULT_CODE_SEPARATOR;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
75
|
+
let formatted = `+${countryFormat.code}`;
|
|
76
|
+
if (formattedLocal) {
|
|
77
|
+
formatted += `${codeSeparator}${formattedLocal}`;
|
|
78
|
+
}
|
|
79
|
+
else if (parsed.hasTrailingSpace && isCodeOnly) {
|
|
80
|
+
formatted += codeSeparator;
|
|
81
|
+
}
|
|
82
|
+
return formatted;
|
|
73
83
|
}
|
|
74
84
|
export function isDummyNumber(phone) {
|
|
75
85
|
const cleanedValue = phone.replace(/\D/g, '');
|
|
@@ -78,10 +88,12 @@ export function isDummyNumber(phone) {
|
|
|
78
88
|
function parseInput(original) {
|
|
79
89
|
const digits = original.replace(/\D/g, '');
|
|
80
90
|
const hasExplicitPlus = original.trim().startsWith('+');
|
|
91
|
+
const hasTrailingSpace = /\s$/.test(original);
|
|
81
92
|
return {
|
|
82
93
|
original,
|
|
83
94
|
digits,
|
|
84
95
|
hasExplicitPlus,
|
|
96
|
+
hasTrailingSpace,
|
|
85
97
|
};
|
|
86
98
|
}
|
|
87
99
|
function detectCountryFormat(input) {
|
|
@@ -93,6 +105,16 @@ function detectCountryFormat(input) {
|
|
|
93
105
|
if (explicitMatch) {
|
|
94
106
|
return explicitMatch;
|
|
95
107
|
}
|
|
108
|
+
return createAdHocFormat(input.digits);
|
|
109
|
+
}
|
|
110
|
+
if (input.hasTrailingSpace &&
|
|
111
|
+
input.digits.length > 0 &&
|
|
112
|
+
input.digits.length <= 2) {
|
|
113
|
+
const exactMatch = COUNTRY_FORMATS.find((format) => format.code === input.digits);
|
|
114
|
+
if (exactMatch) {
|
|
115
|
+
return exactMatch;
|
|
116
|
+
}
|
|
117
|
+
return createAdHocFormat(input.digits);
|
|
96
118
|
}
|
|
97
119
|
const detected = COUNTRY_FORMATS.find((format) => format.detect ? format.detect(input) : false);
|
|
98
120
|
return detected !== null && detected !== void 0 ? detected : DEFAULT_COUNTRY;
|
|
@@ -35,6 +35,11 @@ const COUNTRY_FORMATS = [
|
|
|
35
35
|
},
|
|
36
36
|
];
|
|
37
37
|
const DEFAULT_COUNTRY = COUNTRY_FORMATS.find((format) => format.code === '1') ?? COUNTRY_FORMATS[0];
|
|
38
|
+
const createAdHocFormat = (code) => ({
|
|
39
|
+
code,
|
|
40
|
+
groupSizes: [],
|
|
41
|
+
groupSeparator: DEFAULT_CODE_SEPARATOR,
|
|
42
|
+
});
|
|
38
43
|
function isValidNumber(number) {
|
|
39
44
|
if (LETTER_PATTERN.test(number)) {
|
|
40
45
|
return false;
|
|
@@ -60,7 +65,8 @@ function formatPhoneNumber(value, shouldFailSilently = true) {
|
|
|
60
65
|
const parsed = parseInput(value);
|
|
61
66
|
const countryFormat = detectCountryFormat(parsed);
|
|
62
67
|
const localDigits = stripCountryCodeFromDigits(parsed.digits, countryFormat.code);
|
|
63
|
-
|
|
68
|
+
const isCodeOnly = parsed.digits.length > 0 && parsed.digits === countryFormat.code;
|
|
69
|
+
if (!localDigits.length && !isCodeOnly) {
|
|
64
70
|
if (!shouldFailSilently) {
|
|
65
71
|
throw new Error('INVALID_PHONE_NUMBER');
|
|
66
72
|
}
|
|
@@ -68,10 +74,14 @@ function formatPhoneNumber(value, shouldFailSilently = true) {
|
|
|
68
74
|
}
|
|
69
75
|
const formattedLocal = formatLocalDigits(localDigits, countryFormat);
|
|
70
76
|
const codeSeparator = countryFormat.codeSeparator ?? DEFAULT_CODE_SEPARATOR;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
77
|
+
let formatted = `+${countryFormat.code}`;
|
|
78
|
+
if (formattedLocal) {
|
|
79
|
+
formatted += `${codeSeparator}${formattedLocal}`;
|
|
80
|
+
}
|
|
81
|
+
else if (parsed.hasTrailingSpace && isCodeOnly) {
|
|
82
|
+
formatted += codeSeparator;
|
|
83
|
+
}
|
|
84
|
+
return formatted;
|
|
75
85
|
}
|
|
76
86
|
function isDummyNumber(phone) {
|
|
77
87
|
const cleanedValue = phone.replace(/\D/g, '');
|
|
@@ -80,10 +90,12 @@ function isDummyNumber(phone) {
|
|
|
80
90
|
function parseInput(original) {
|
|
81
91
|
const digits = original.replace(/\D/g, '');
|
|
82
92
|
const hasExplicitPlus = original.trim().startsWith('+');
|
|
93
|
+
const hasTrailingSpace = /\s$/.test(original);
|
|
83
94
|
return {
|
|
84
95
|
original,
|
|
85
96
|
digits,
|
|
86
97
|
hasExplicitPlus,
|
|
98
|
+
hasTrailingSpace,
|
|
87
99
|
};
|
|
88
100
|
}
|
|
89
101
|
function detectCountryFormat(input) {
|
|
@@ -95,6 +107,16 @@ function detectCountryFormat(input) {
|
|
|
95
107
|
if (explicitMatch) {
|
|
96
108
|
return explicitMatch;
|
|
97
109
|
}
|
|
110
|
+
return createAdHocFormat(input.digits);
|
|
111
|
+
}
|
|
112
|
+
if (input.hasTrailingSpace &&
|
|
113
|
+
input.digits.length > 0 &&
|
|
114
|
+
input.digits.length <= 2) {
|
|
115
|
+
const exactMatch = COUNTRY_FORMATS.find((format) => format.code === input.digits);
|
|
116
|
+
if (exactMatch) {
|
|
117
|
+
return exactMatch;
|
|
118
|
+
}
|
|
119
|
+
return createAdHocFormat(input.digits);
|
|
98
120
|
}
|
|
99
121
|
const detected = COUNTRY_FORMATS.find((format) => format.detect ? format.detect(input) : false);
|
|
100
122
|
return detected ?? DEFAULT_COUNTRY;
|