@vtex/faststore-plugin-buyer-portal 2.0.41 → 2.0.43

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,76 +1,16 @@
1
- import mask from "./mask";
1
+ import {
2
+ applyZipInput,
3
+ getCountryRule,
4
+ } from "../../addresses/utils/countryRules";
2
5
 
3
6
  export function maskPostalCode(postalCode: string, country?: string) {
4
- if (postalCode) {
5
- switch (country) {
6
- case "USA":
7
- return mask(postalCode, "99999").substring(0, 5);
8
- case "MEX":
9
- return mask(postalCode, "99999").substring(0, 5);
10
- case "BRA":
11
- return mask(postalCode, "99999-999").substring(0, 9);
12
- case "CAN":
13
- return formatCanadaPostalCode(postalCode);
14
- default:
15
- return postalCode;
16
- }
17
- } else {
18
- return postalCode;
19
- }
7
+ if (!postalCode) return postalCode;
8
+ return applyZipInput(country, postalCode);
20
9
  }
21
10
 
22
11
  export function validatePostalCode(postalCode: string, country?: string) {
23
- if (postalCode) {
24
- switch (country) {
25
- case "USA":
26
- return /^[0-9]{5}$/.test(postalCode.replace("-", ""));
27
- case "MEX":
28
- return /^[0-9]{5}$/.test(postalCode.replace("-", ""));
29
- case "BRA":
30
- return /^[0-9]{5}-?[0-9]{3}$/.test(postalCode.replace("-", ""));
31
- case "CAN":
32
- return validateCanadianPostalCode(postalCode);
33
- default:
34
- return true;
35
- }
36
- } else {
37
- return true;
38
- }
12
+ if (!postalCode) return true;
13
+ const rule = getCountryRule(country);
14
+ if (!rule.zip.pattern) return true;
15
+ return rule.zip.pattern.test(postalCode.trim());
39
16
  }
40
-
41
- const formatCanadaPostalCode = (postalCode: string) => {
42
- postalCode = postalCode.replace(/[^\w]+/g, "").toUpperCase();
43
-
44
- if (postalCode.length === 6) {
45
- return `${postalCode.substring(0, 3)} ${postalCode.substring(
46
- 3
47
- )}`.toUpperCase();
48
- }
49
-
50
- return postalCode;
51
- };
52
-
53
- const unformatPostalCode = (postalCode: string) => {
54
- if (!postalCode || typeof postalCode !== "string") return "";
55
-
56
- postalCode = postalCode.replace(/[^\w]+/g, "").toUpperCase();
57
-
58
- return postalCode;
59
- };
60
-
61
- const validateCanadianPostalCode = (postalCode: string) => {
62
- if (!postalCode) return false;
63
-
64
- postalCode = unformatPostalCode(postalCode);
65
- const postalCodePattern = /^[A-z][0-9][A-z][0-9][A-z][0-9]$/;
66
-
67
- if (
68
- postalCode.length > 5 &&
69
- postalCode.length < 8 &&
70
- postalCodePattern.test(postalCode)
71
- ) {
72
- return true;
73
- }
74
-
75
- return false;
76
- };