@sprucelabs/schema 29.3.10 → 29.4.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.
@@ -1,12 +1,23 @@
1
- function formatUsNumber(phoneNumberString) {
1
+ function formatNumberWithCode(phoneNumberString, code = '1') {
2
2
  if (phoneNumberString.match(/[a-zA-Z]/g)) {
3
3
  return null;
4
4
  }
5
- let cleaned = ('' + phoneNumberString).replace(/\D/g, '');
6
- let match = cleaned.match(/^(1|)?(\d{3})(\d{0,3})(\d{0,4})$/);
5
+ const cleaned = ('' + phoneNumberString).replace(/\D/g, '');
6
+ // Dynamically insert the country code into the regex
7
+ let regexPattern = new RegExp(`^(${code})?(\\d{3})(\\d{0,3})(\\d{0,4})$`);
8
+ let match = cleaned.match(regexPattern);
7
9
  if (match) {
8
- let intlCode = match[1] ? '+1 ' : '+1 ';
9
- const number = [intlCode, '', match[2], '-', match[3], '-', match[4]]
10
+ let intlCode = match[1] ? `+${match[1]} ` : `+${code} `; // Use the matched code or the provided code
11
+ const divider = code === '1' ? '-' : ' ';
12
+ const number = [
13
+ intlCode,
14
+ '',
15
+ match[2],
16
+ divider,
17
+ match[3],
18
+ divider,
19
+ match[4],
20
+ ]
10
21
  .join('')
11
22
  .replace(/-+$/, '');
12
23
  return number;
@@ -15,11 +26,31 @@ function formatUsNumber(phoneNumberString) {
15
26
  }
16
27
  export function isValidNumber(number) {
17
28
  var _a;
18
- const formatted = (_a = formatUsNumber(number)) === null || _a === void 0 ? void 0 : _a.replace(/[^0-9]/g, '');
29
+ const code = getCode(number);
30
+ const formatted = (_a = formatNumberWithCode(number, code)) === null || _a === void 0 ? void 0 : _a.replace(/[^0-9]/g, '');
19
31
  return (formatted === null || formatted === void 0 ? void 0 : formatted.length) === 11 || (formatted === null || formatted === void 0 ? void 0 : formatted.length) === 12;
20
32
  }
33
+ function getCode(number) {
34
+ let code = `1`; // Default to North American country code
35
+ const cleaned = number.replace(/\D/g, '');
36
+ // Explicitly check for '+' sign to distinguish international codes
37
+ if (cleaned.startsWith('+90') ||
38
+ (cleaned.startsWith('90') && cleaned.length > 10)) {
39
+ code = `90`;
40
+ }
41
+ else if (cleaned.startsWith('+49') ||
42
+ (cleaned.startsWith('49') && cleaned.length > 10)) {
43
+ code = `49`;
44
+ }
45
+ // Adjust the condition to ensure that '905...' numbers without a '+' are treated as North American
46
+ else if (cleaned.startsWith('905') && cleaned.length === 10) {
47
+ code = `1`;
48
+ }
49
+ return code;
50
+ }
21
51
  export default function formatPhoneNumber(val, shouldFailSilently = true) {
22
- const formatted = formatUsNumber(val);
52
+ const code = getCode(val);
53
+ const formatted = formatNumberWithCode(val, code);
23
54
  if (!formatted) {
24
55
  if (!shouldFailSilently) {
25
56
  throw new Error('INVALID_PHONE_NUMBER');
@@ -29,46 +60,6 @@ export default function formatPhoneNumber(val, shouldFailSilently = true) {
29
60
  }
30
61
  }
31
62
  return formatted;
32
- // const PNF = libPhoneNumber.PhoneNumberFormat
33
- // const phoneUtil = libPhoneNumber.PhoneNumberUtil.getInstance()
34
- // let num = val
35
- // try {
36
- // // First try parsing it as an international number
37
- // // @ts-ignore: Upgrade to google-libphonenumber@^3
38
- // num = phoneUtil.parse(val)
39
- // } catch (e) {
40
- // // Try parsing it as a US number
41
- // // log.debug('Unable to parse number as international number')
42
- // try {
43
- // // @ts-ignore: Upgrade to google-libphonenumber@^3
44
- // num = phoneUtil.parse(val, 'US')
45
- // } catch (err) {
46
- // // Log.debug('Unable to parse number as international or US number')
47
- // }
48
- // }
49
- // let formattedNumber = num
50
- // try {
51
- // // @ts-ignore: Upgrade to google-libphonenumber@^3
52
- // formattedNumber = phoneUtil.format(num, PNF.INTERNATIONAL)
53
- // } catch (e) {
54
- // // Log.debug('Unable to format phone number')
55
- // if (!failSilently) {
56
- // throw new Error('INVALID_PHONE_NUMBER')
57
- // }
58
- // }
59
- // if (!failSilently) {
60
- // let isValid
61
- // const cleanedValue = val.replace(/\D/g, '')
62
- // if (isDummyNumber(val)) {
63
- // isValid = cleanedValue.length === 11 || cleanedValue.length === 10
64
- // } else {
65
- // isValid = isValidNumber(formattedNumber)
66
- // }
67
- // if (!isValid) {
68
- // throw new Error('INVALID_PHONE_NUMBER')
69
- // }
70
- // }
71
- // return formattedNumber
72
63
  }
73
64
  export function isDummyNumber(phone) {
74
65
  const cleanedValue = phone.replace(/\D/g, '');
@@ -1,15 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isDummyNumber = exports.isValidNumber = void 0;
4
- function formatUsNumber(phoneNumberString) {
4
+ function formatNumberWithCode(phoneNumberString, code = '1') {
5
5
  if (phoneNumberString.match(/[a-zA-Z]/g)) {
6
6
  return null;
7
7
  }
8
- let cleaned = ('' + phoneNumberString).replace(/\D/g, '');
9
- let match = cleaned.match(/^(1|)?(\d{3})(\d{0,3})(\d{0,4})$/);
8
+ const cleaned = ('' + phoneNumberString).replace(/\D/g, '');
9
+ // Dynamically insert the country code into the regex
10
+ let regexPattern = new RegExp(`^(${code})?(\\d{3})(\\d{0,3})(\\d{0,4})$`);
11
+ let match = cleaned.match(regexPattern);
10
12
  if (match) {
11
- let intlCode = match[1] ? '+1 ' : '+1 ';
12
- const number = [intlCode, '', match[2], '-', match[3], '-', match[4]]
13
+ let intlCode = match[1] ? `+${match[1]} ` : `+${code} `; // Use the matched code or the provided code
14
+ const divider = code === '1' ? '-' : ' ';
15
+ const number = [
16
+ intlCode,
17
+ '',
18
+ match[2],
19
+ divider,
20
+ match[3],
21
+ divider,
22
+ match[4],
23
+ ]
13
24
  .join('')
14
25
  .replace(/-+$/, '');
15
26
  return number;
@@ -18,12 +29,32 @@ function formatUsNumber(phoneNumberString) {
18
29
  }
19
30
  function isValidNumber(number) {
20
31
  var _a;
21
- const formatted = (_a = formatUsNumber(number)) === null || _a === void 0 ? void 0 : _a.replace(/[^0-9]/g, '');
32
+ const code = getCode(number);
33
+ const formatted = (_a = formatNumberWithCode(number, code)) === null || _a === void 0 ? void 0 : _a.replace(/[^0-9]/g, '');
22
34
  return (formatted === null || formatted === void 0 ? void 0 : formatted.length) === 11 || (formatted === null || formatted === void 0 ? void 0 : formatted.length) === 12;
23
35
  }
24
36
  exports.isValidNumber = isValidNumber;
37
+ function getCode(number) {
38
+ let code = `1`; // Default to North American country code
39
+ const cleaned = number.replace(/\D/g, '');
40
+ // Explicitly check for '+' sign to distinguish international codes
41
+ if (cleaned.startsWith('+90') ||
42
+ (cleaned.startsWith('90') && cleaned.length > 10)) {
43
+ code = `90`;
44
+ }
45
+ else if (cleaned.startsWith('+49') ||
46
+ (cleaned.startsWith('49') && cleaned.length > 10)) {
47
+ code = `49`;
48
+ }
49
+ // Adjust the condition to ensure that '905...' numbers without a '+' are treated as North American
50
+ else if (cleaned.startsWith('905') && cleaned.length === 10) {
51
+ code = `1`;
52
+ }
53
+ return code;
54
+ }
25
55
  function formatPhoneNumber(val, shouldFailSilently = true) {
26
- const formatted = formatUsNumber(val);
56
+ const code = getCode(val);
57
+ const formatted = formatNumberWithCode(val, code);
27
58
  if (!formatted) {
28
59
  if (!shouldFailSilently) {
29
60
  throw new Error('INVALID_PHONE_NUMBER');
@@ -33,46 +64,6 @@ function formatPhoneNumber(val, shouldFailSilently = true) {
33
64
  }
34
65
  }
35
66
  return formatted;
36
- // const PNF = libPhoneNumber.PhoneNumberFormat
37
- // const phoneUtil = libPhoneNumber.PhoneNumberUtil.getInstance()
38
- // let num = val
39
- // try {
40
- // // First try parsing it as an international number
41
- // // @ts-ignore: Upgrade to google-libphonenumber@^3
42
- // num = phoneUtil.parse(val)
43
- // } catch (e) {
44
- // // Try parsing it as a US number
45
- // // log.debug('Unable to parse number as international number')
46
- // try {
47
- // // @ts-ignore: Upgrade to google-libphonenumber@^3
48
- // num = phoneUtil.parse(val, 'US')
49
- // } catch (err) {
50
- // // Log.debug('Unable to parse number as international or US number')
51
- // }
52
- // }
53
- // let formattedNumber = num
54
- // try {
55
- // // @ts-ignore: Upgrade to google-libphonenumber@^3
56
- // formattedNumber = phoneUtil.format(num, PNF.INTERNATIONAL)
57
- // } catch (e) {
58
- // // Log.debug('Unable to format phone number')
59
- // if (!failSilently) {
60
- // throw new Error('INVALID_PHONE_NUMBER')
61
- // }
62
- // }
63
- // if (!failSilently) {
64
- // let isValid
65
- // const cleanedValue = val.replace(/\D/g, '')
66
- // if (isDummyNumber(val)) {
67
- // isValid = cleanedValue.length === 11 || cleanedValue.length === 10
68
- // } else {
69
- // isValid = isValidNumber(formattedNumber)
70
- // }
71
- // if (!isValid) {
72
- // throw new Error('INVALID_PHONE_NUMBER')
73
- // }
74
- // }
75
- // return formattedNumber
76
67
  }
77
68
  exports.default = formatPhoneNumber;
78
69
  function isDummyNumber(phone) {
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "!build/__tests__",
9
9
  "esm"
10
10
  ],
11
- "version": "29.3.10",
11
+ "version": "29.4.0",
12
12
  "main": "./build/index.js",
13
13
  "types": "./build/index.d.ts",
14
14
  "module": "./build/esm/index.js",
@@ -66,8 +66,8 @@
66
66
  "build.copy-files": "mkdir -p build && rsync -avzq --exclude='*.ts' ./src/ ./build/"
67
67
  },
68
68
  "dependencies": {
69
- "@sprucelabs/error": "^5.1.57",
70
- "@sprucelabs/test-utils": "^4.0.96",
69
+ "@sprucelabs/error": "^5.1.63",
70
+ "@sprucelabs/test-utils": "^4.0.97",
71
71
  "email-validator": "^2.0.4",
72
72
  "just-safe-get": "^4.2.0",
73
73
  "just-safe-set": "^4.2.1"
@@ -85,7 +85,7 @@
85
85
  "eslint-config-spruce": "^10.13.6",
86
86
  "jest": "^29.7.0",
87
87
  "jest-circus": "^29.7.0",
88
- "prettier": "^3.1.1",
88
+ "prettier": "^3.2.5",
89
89
  "ts-node": "^10.9.2",
90
90
  "tsc-watch": "^6.0.4",
91
91
  "tsconfig-paths": "^4.2.0",