@sprucelabs/schema 32.2.1 → 32.2.3

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.
@@ -63,7 +63,8 @@ export default function formatPhoneNumber(value, shouldFailSilently = true) {
63
63
  const parsed = parseInput(value);
64
64
  const countryFormat = detectCountryFormat(parsed);
65
65
  const localDigits = stripCountryCodeFromDigits(parsed.digits, countryFormat.code);
66
- if (!localDigits.length) {
66
+ const isCodeOnly = parsed.digits.length > 0 && parsed.digits === countryFormat.code;
67
+ if (!localDigits.length && !isCodeOnly) {
67
68
  if (!shouldFailSilently) {
68
69
  throw new Error('INVALID_PHONE_NUMBER');
69
70
  }
@@ -71,10 +72,14 @@ export default function formatPhoneNumber(value, shouldFailSilently = true) {
71
72
  }
72
73
  const formattedLocal = formatLocalDigits(localDigits, countryFormat);
73
74
  const codeSeparator = (_a = countryFormat.codeSeparator) !== null && _a !== void 0 ? _a : DEFAULT_CODE_SEPARATOR;
74
- const formatted = formattedLocal
75
- ? `+${countryFormat.code}${codeSeparator}${formattedLocal}`
76
- : `+${countryFormat.code}`;
77
- return formatted.trim();
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;
78
83
  }
79
84
  export function isDummyNumber(phone) {
80
85
  const cleanedValue = phone.replace(/\D/g, '');
@@ -83,10 +88,12 @@ export function isDummyNumber(phone) {
83
88
  function parseInput(original) {
84
89
  const digits = original.replace(/\D/g, '');
85
90
  const hasExplicitPlus = original.trim().startsWith('+');
91
+ const hasTrailingSpace = /\s$/.test(original);
86
92
  return {
87
93
  original,
88
94
  digits,
89
95
  hasExplicitPlus,
96
+ hasTrailingSpace,
90
97
  };
91
98
  }
92
99
  function detectCountryFormat(input) {
@@ -100,6 +107,15 @@ function detectCountryFormat(input) {
100
107
  }
101
108
  return createAdHocFormat(input.digits);
102
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);
118
+ }
103
119
  const detected = COUNTRY_FORMATS.find((format) => format.detect ? format.detect(input) : false);
104
120
  return detected !== null && detected !== void 0 ? detected : DEFAULT_COUNTRY;
105
121
  }
@@ -65,7 +65,8 @@ function formatPhoneNumber(value, shouldFailSilently = true) {
65
65
  const parsed = parseInput(value);
66
66
  const countryFormat = detectCountryFormat(parsed);
67
67
  const localDigits = stripCountryCodeFromDigits(parsed.digits, countryFormat.code);
68
- if (!localDigits.length) {
68
+ const isCodeOnly = parsed.digits.length > 0 && parsed.digits === countryFormat.code;
69
+ if (!localDigits.length && !isCodeOnly) {
69
70
  if (!shouldFailSilently) {
70
71
  throw new Error('INVALID_PHONE_NUMBER');
71
72
  }
@@ -73,10 +74,14 @@ function formatPhoneNumber(value, shouldFailSilently = true) {
73
74
  }
74
75
  const formattedLocal = formatLocalDigits(localDigits, countryFormat);
75
76
  const codeSeparator = countryFormat.codeSeparator ?? DEFAULT_CODE_SEPARATOR;
76
- const formatted = formattedLocal
77
- ? `+${countryFormat.code}${codeSeparator}${formattedLocal}`
78
- : `+${countryFormat.code}`;
79
- return formatted.trim();
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;
80
85
  }
81
86
  function isDummyNumber(phone) {
82
87
  const cleanedValue = phone.replace(/\D/g, '');
@@ -85,10 +90,12 @@ function isDummyNumber(phone) {
85
90
  function parseInput(original) {
86
91
  const digits = original.replace(/\D/g, '');
87
92
  const hasExplicitPlus = original.trim().startsWith('+');
93
+ const hasTrailingSpace = /\s$/.test(original);
88
94
  return {
89
95
  original,
90
96
  digits,
91
97
  hasExplicitPlus,
98
+ hasTrailingSpace,
92
99
  };
93
100
  }
94
101
  function detectCountryFormat(input) {
@@ -102,6 +109,15 @@ function detectCountryFormat(input) {
102
109
  }
103
110
  return createAdHocFormat(input.digits);
104
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);
120
+ }
105
121
  const detected = COUNTRY_FORMATS.find((format) => format.detect ? format.detect(input) : false);
106
122
  return detected ?? DEFAULT_COUNTRY;
107
123
  }
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "!build/__tests__",
9
9
  "esm"
10
10
  ],
11
- "version": "32.2.1",
11
+ "version": "32.2.3",
12
12
  "main": "./build/index.js",
13
13
  "types": "./build/index.d.ts",
14
14
  "module": "./build/esm/index.js",
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "@sprucelabs/error": "^7.1.31",
62
- "@sprucelabs/test-utils": "^6.0.100",
62
+ "@sprucelabs/test-utils": "^6.0.101",
63
63
  "email-validator": "^2.0.4",
64
64
  "just-safe-get": "^4.2.0",
65
65
  "just-safe-set": "^4.2.1"