@open-tender/utils 0.5.59 → 0.5.61

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.
@@ -35,3 +35,4 @@ export * from './useSendResetForm';
35
35
  export * from './useSigninForm';
36
36
  export * from './useSignUpGuestForm';
37
37
  export * from './useSignUpForm';
38
+ export * from './usePasswordValidation';
@@ -38,3 +38,4 @@ tslib_1.__exportStar(require("./useSendResetForm"), exports);
38
38
  tslib_1.__exportStar(require("./useSigninForm"), exports);
39
39
  tslib_1.__exportStar(require("./useSignUpGuestForm"), exports);
40
40
  tslib_1.__exportStar(require("./useSignUpForm"), exports);
41
+ tslib_1.__exportStar(require("./usePasswordValidation"), exports);
@@ -1,4 +1,4 @@
1
- import { CartItem, CartLevels } from '@open-tender/types';
1
+ import { CartItem, CartItemGroups, CartLevels } from '@open-tender/types';
2
2
  export declare const useBuilder: (orderItem: CartItem, orderTimeoutCallback?: () => void) => {
3
3
  item: CartItem;
4
4
  increment: () => void;
@@ -11,4 +11,5 @@ export declare const useBuilder: (orderItem: CartItem, orderTimeoutCallback?: ()
11
11
  incrementOption: (levels: CartLevels) => CartItem;
12
12
  decrementOption: (levels: CartLevels) => CartItem;
13
13
  setOptionQuantity: (levels: CartLevels, quantity: number) => CartItem;
14
+ setGroups: (groups: CartItemGroups) => void;
14
15
  };
@@ -134,6 +134,10 @@ const useBuilder = (orderItem, orderTimeoutCallback) => {
134
134
  setItem(pricedItem);
135
135
  return pricedItem;
136
136
  };
137
+ const setGroups = (groups) => {
138
+ const pricedItem = (0, utils_1.calcPrices)(Object.assign(Object.assign({}, item), { groups }));
139
+ setItem(pricedItem);
140
+ };
137
141
  return {
138
142
  item,
139
143
  increment,
@@ -145,7 +149,8 @@ const useBuilder = (orderItem, orderTimeoutCallback) => {
145
149
  toggleOption,
146
150
  incrementOption,
147
151
  decrementOption,
148
- setOptionQuantity
152
+ setOptionQuantity,
153
+ setGroups
149
154
  };
150
155
  };
151
156
  exports.useBuilder = useBuilder;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Custom hook for password field validation and state management
3
+ * Follows SRP by encapsulating all password-related state and logic
4
+ */
5
+ export declare const usePasswordValidation: () => {
6
+ handlePasswordChange: () => void;
7
+ isPasswordValid: (password: string | undefined) => boolean;
8
+ shouldShowRequirements: (password: string | undefined, hidePassword?: boolean) => boolean;
9
+ getPasswordErrors: (password: string | undefined, errors: Record<string, string>, fieldName?: string) => Record<string, string>;
10
+ };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePasswordValidation = void 0;
4
+ const react_1 = require("react");
5
+ const passwordValidation_1 = require("../utils/passwordValidation");
6
+ /**
7
+ * Custom hook for password field validation and state management
8
+ * Follows SRP by encapsulating all password-related state and logic
9
+ */
10
+ const usePasswordValidation = () => {
11
+ const [passwordTouched, setPasswordTouched] = (0, react_1.useState)(false);
12
+ const handlePasswordChange = () => {
13
+ if (!passwordTouched) {
14
+ setPasswordTouched(true);
15
+ }
16
+ };
17
+ const isPasswordValid = (password) => {
18
+ if (!password)
19
+ return false;
20
+ const validation = (0, passwordValidation_1.validatePassword)(password);
21
+ return validation.isValid;
22
+ };
23
+ const shouldShowRequirements = (password, hidePassword = false) => {
24
+ return !hidePassword && !!password;
25
+ };
26
+ const getPasswordErrors = (password, errors, fieldName = 'password') => {
27
+ const passwordErrors = {};
28
+ if (errors[fieldName]) {
29
+ // If password is valid according to frontend but still has error, it's from backend
30
+ const isValidFrontend = password && (0, passwordValidation_1.validatePassword)(password).isValid;
31
+ if (isValidFrontend || !password) {
32
+ passwordErrors[fieldName] = errors[fieldName];
33
+ }
34
+ }
35
+ return passwordErrors;
36
+ };
37
+ return {
38
+ handlePasswordChange,
39
+ isPasswordValid,
40
+ shouldShowRequirements,
41
+ getPasswordErrors
42
+ };
43
+ };
44
+ exports.usePasswordValidation = usePasswordValidation;
@@ -75,6 +75,7 @@ export declare const useProfileForm: (profile: Customer | null, loading: Request
75
75
  style?: import("react").CSSProperties | undefined;
76
76
  type: string;
77
77
  value?: string | undefined;
78
+ onBlur?: ((evt: any) => void) | undefined;
78
79
  })[];
79
80
  handleChange: (name: string, value: string | number | boolean) => void;
80
81
  handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
@@ -18,3 +18,4 @@ export * from './version';
18
18
  export * from './theme';
19
19
  export * from './favorites';
20
20
  export * from './pizzaBuilder';
21
+ export * from './passwordValidation';
@@ -22,3 +22,4 @@ tslib_1.__exportStar(require("./version"), exports);
22
22
  tslib_1.__exportStar(require("./theme"), exports);
23
23
  tslib_1.__exportStar(require("./favorites"), exports);
24
24
  tslib_1.__exportStar(require("./pizzaBuilder"), exports);
25
+ tslib_1.__exportStar(require("./passwordValidation"), exports);
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Password validation utility matching backend password policy
3
+ *
4
+ * Requirements:
5
+ * - Minimum 10 characters
6
+ * - At least one uppercase letter
7
+ * - At least one lowercase letter
8
+ * - At least one number
9
+ * - At least one special character from allowed set: !@#$%^&_-+=<>
10
+ *
11
+ * Backend regex: ^[A-Za-z0-9\!\@\#\$\%\^\&\_\-\+\=\<\>]*$
12
+ */
13
+ export interface PasswordValidationResult {
14
+ isValid: boolean;
15
+ errors: string[];
16
+ }
17
+ export interface PasswordRequirement {
18
+ met: boolean;
19
+ message: string;
20
+ }
21
+ export declare const validatePassword: (password: string) => PasswordValidationResult;
22
+ export declare const getPasswordRequirements: (password: string) => PasswordRequirement[];
23
+ export declare const getPasswordErrorMessage: (password: string) => string;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ /**
3
+ * Password validation utility matching backend password policy
4
+ *
5
+ * Requirements:
6
+ * - Minimum 10 characters
7
+ * - At least one uppercase letter
8
+ * - At least one lowercase letter
9
+ * - At least one number
10
+ * - At least one special character from allowed set: !@#$%^&_-+=<>
11
+ *
12
+ * Backend regex: ^[A-Za-z0-9\!\@\#\$\%\^\&\_\-\+\=\<\>]*$
13
+ */
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getPasswordErrorMessage = exports.getPasswordRequirements = exports.validatePassword = void 0;
16
+ // Constants
17
+ const MIN_LENGTH = 10;
18
+ const ALLOWED_SPECIAL_CHARS = '!@#$%^&_-+=<>';
19
+ // Regex patterns - DRY principle
20
+ const REGEX = {
21
+ ALLOWED_CHARS: /^[A-Za-z0-9!@#$%^&_\-+=<>]*$/,
22
+ UPPERCASE: /[A-Z]/,
23
+ LOWERCASE: /[a-z]/,
24
+ NUMBER: /[0-9]/,
25
+ SPECIAL_CHAR: /[!@#$%^&_\-+=<>]/,
26
+ };
27
+ // Error messages - DRY principle
28
+ const ERROR_MESSAGES = {
29
+ MIN_LENGTH: `Password must be at least ${MIN_LENGTH} characters long`,
30
+ INVALID_CHARS: `Password contains invalid characters. Only letters, numbers, and (${ALLOWED_SPECIAL_CHARS}) are allowed`,
31
+ UPPERCASE: 'Password must include at least one uppercase letter',
32
+ LOWERCASE: 'Password must include at least one lowercase letter',
33
+ NUMBER: 'Password must include at least one number',
34
+ SPECIAL_CHAR: `Password must include at least one special character (${ALLOWED_SPECIAL_CHARS})`,
35
+ };
36
+ // SRP: Individual validation functions
37
+ const hasMinLength = (password) => password.length >= MIN_LENGTH;
38
+ const hasOnlyAllowedChars = (password) => REGEX.ALLOWED_CHARS.test(password);
39
+ const hasUppercase = (password) => REGEX.UPPERCASE.test(password);
40
+ const hasLowercase = (password) => REGEX.LOWERCASE.test(password);
41
+ const hasNumber = (password) => REGEX.NUMBER.test(password);
42
+ const hasSpecialChar = (password) => REGEX.SPECIAL_CHAR.test(password);
43
+ const validatePassword = (password) => {
44
+ const errors = [];
45
+ if (!password || !hasMinLength(password)) {
46
+ errors.push(ERROR_MESSAGES.MIN_LENGTH);
47
+ }
48
+ if (!hasOnlyAllowedChars(password)) {
49
+ errors.push(ERROR_MESSAGES.INVALID_CHARS);
50
+ }
51
+ if (!hasUppercase(password)) {
52
+ errors.push(ERROR_MESSAGES.UPPERCASE);
53
+ }
54
+ if (!hasLowercase(password)) {
55
+ errors.push(ERROR_MESSAGES.LOWERCASE);
56
+ }
57
+ if (!hasNumber(password)) {
58
+ errors.push(ERROR_MESSAGES.NUMBER);
59
+ }
60
+ if (!hasSpecialChar(password)) {
61
+ errors.push(ERROR_MESSAGES.SPECIAL_CHAR);
62
+ }
63
+ return {
64
+ isValid: errors.length === 0,
65
+ errors,
66
+ };
67
+ };
68
+ exports.validatePassword = validatePassword;
69
+ const getPasswordRequirements = (password) => {
70
+ const requirements = [
71
+ {
72
+ met: hasMinLength(password),
73
+ message: `At least ${MIN_LENGTH} characters`,
74
+ },
75
+ {
76
+ met: hasUppercase(password),
77
+ message: 'One uppercase letter',
78
+ },
79
+ {
80
+ met: hasLowercase(password),
81
+ message: 'One lowercase letter',
82
+ },
83
+ {
84
+ met: hasNumber(password),
85
+ message: 'One number',
86
+ },
87
+ {
88
+ met: hasOnlyAllowedChars(password) && hasSpecialChar(password),
89
+ message: `One special character from: ${ALLOWED_SPECIAL_CHARS}`,
90
+ },
91
+ ];
92
+ return requirements;
93
+ };
94
+ exports.getPasswordRequirements = getPasswordRequirements;
95
+ const getPasswordErrorMessage = (password) => {
96
+ const validation = (0, exports.validatePassword)(password);
97
+ if (validation.isValid)
98
+ return '';
99
+ // Return all errors as a single message
100
+ return validation.errors.join('. ');
101
+ };
102
+ exports.getPasswordErrorMessage = getPasswordErrorMessage;
@@ -35,3 +35,4 @@ export * from './useSendResetForm';
35
35
  export * from './useSigninForm';
36
36
  export * from './useSignUpGuestForm';
37
37
  export * from './useSignUpForm';
38
+ export * from './usePasswordValidation';
@@ -35,3 +35,4 @@ export * from './useSendResetForm';
35
35
  export * from './useSigninForm';
36
36
  export * from './useSignUpGuestForm';
37
37
  export * from './useSignUpForm';
38
+ export * from './usePasswordValidation';
@@ -1,4 +1,4 @@
1
- import { CartItem, CartLevels } from '@open-tender/types';
1
+ import { CartItem, CartItemGroups, CartLevels } from '@open-tender/types';
2
2
  export declare const useBuilder: (orderItem: CartItem, orderTimeoutCallback?: () => void) => {
3
3
  item: CartItem;
4
4
  increment: () => void;
@@ -11,4 +11,5 @@ export declare const useBuilder: (orderItem: CartItem, orderTimeoutCallback?: ()
11
11
  incrementOption: (levels: CartLevels) => CartItem;
12
12
  decrementOption: (levels: CartLevels) => CartItem;
13
13
  setOptionQuantity: (levels: CartLevels, quantity: number) => CartItem;
14
+ setGroups: (groups: CartItemGroups) => void;
14
15
  };
@@ -131,6 +131,10 @@ export const useBuilder = (orderItem, orderTimeoutCallback) => {
131
131
  setItem(pricedItem);
132
132
  return pricedItem;
133
133
  };
134
+ const setGroups = (groups) => {
135
+ const pricedItem = calcPrices(Object.assign(Object.assign({}, item), { groups }));
136
+ setItem(pricedItem);
137
+ };
134
138
  return {
135
139
  item,
136
140
  increment,
@@ -142,6 +146,7 @@ export const useBuilder = (orderItem, orderTimeoutCallback) => {
142
146
  toggleOption,
143
147
  incrementOption,
144
148
  decrementOption,
145
- setOptionQuantity
149
+ setOptionQuantity,
150
+ setGroups
146
151
  };
147
152
  };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Custom hook for password field validation and state management
3
+ * Follows SRP by encapsulating all password-related state and logic
4
+ */
5
+ export declare const usePasswordValidation: () => {
6
+ handlePasswordChange: () => void;
7
+ isPasswordValid: (password: string | undefined) => boolean;
8
+ shouldShowRequirements: (password: string | undefined, hidePassword?: boolean) => boolean;
9
+ getPasswordErrors: (password: string | undefined, errors: Record<string, string>, fieldName?: string) => Record<string, string>;
10
+ };
@@ -0,0 +1,40 @@
1
+ import { useState } from 'react';
2
+ import { validatePassword } from '../utils/passwordValidation';
3
+ /**
4
+ * Custom hook for password field validation and state management
5
+ * Follows SRP by encapsulating all password-related state and logic
6
+ */
7
+ export const usePasswordValidation = () => {
8
+ const [passwordTouched, setPasswordTouched] = useState(false);
9
+ const handlePasswordChange = () => {
10
+ if (!passwordTouched) {
11
+ setPasswordTouched(true);
12
+ }
13
+ };
14
+ const isPasswordValid = (password) => {
15
+ if (!password)
16
+ return false;
17
+ const validation = validatePassword(password);
18
+ return validation.isValid;
19
+ };
20
+ const shouldShowRequirements = (password, hidePassword = false) => {
21
+ return !hidePassword && !!password;
22
+ };
23
+ const getPasswordErrors = (password, errors, fieldName = 'password') => {
24
+ const passwordErrors = {};
25
+ if (errors[fieldName]) {
26
+ // If password is valid according to frontend but still has error, it's from backend
27
+ const isValidFrontend = password && validatePassword(password).isValid;
28
+ if (isValidFrontend || !password) {
29
+ passwordErrors[fieldName] = errors[fieldName];
30
+ }
31
+ }
32
+ return passwordErrors;
33
+ };
34
+ return {
35
+ handlePasswordChange,
36
+ isPasswordValid,
37
+ shouldShowRequirements,
38
+ getPasswordErrors
39
+ };
40
+ };
@@ -75,6 +75,7 @@ export declare const useProfileForm: (profile: Customer | null, loading: Request
75
75
  style?: import("react").CSSProperties | undefined;
76
76
  type: string;
77
77
  value?: string | undefined;
78
+ onBlur?: ((evt: any) => void) | undefined;
78
79
  })[];
79
80
  handleChange: (name: string, value: string | number | boolean) => void;
80
81
  handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
@@ -18,3 +18,4 @@ export * from './version';
18
18
  export * from './theme';
19
19
  export * from './favorites';
20
20
  export * from './pizzaBuilder';
21
+ export * from './passwordValidation';
@@ -19,3 +19,4 @@ export * from './version';
19
19
  export * from './theme';
20
20
  export * from './favorites';
21
21
  export * from './pizzaBuilder';
22
+ export * from './passwordValidation';
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Password validation utility matching backend password policy
3
+ *
4
+ * Requirements:
5
+ * - Minimum 10 characters
6
+ * - At least one uppercase letter
7
+ * - At least one lowercase letter
8
+ * - At least one number
9
+ * - At least one special character from allowed set: !@#$%^&_-+=<>
10
+ *
11
+ * Backend regex: ^[A-Za-z0-9\!\@\#\$\%\^\&\_\-\+\=\<\>]*$
12
+ */
13
+ export interface PasswordValidationResult {
14
+ isValid: boolean;
15
+ errors: string[];
16
+ }
17
+ export interface PasswordRequirement {
18
+ met: boolean;
19
+ message: string;
20
+ }
21
+ export declare const validatePassword: (password: string) => PasswordValidationResult;
22
+ export declare const getPasswordRequirements: (password: string) => PasswordRequirement[];
23
+ export declare const getPasswordErrorMessage: (password: string) => string;
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Password validation utility matching backend password policy
3
+ *
4
+ * Requirements:
5
+ * - Minimum 10 characters
6
+ * - At least one uppercase letter
7
+ * - At least one lowercase letter
8
+ * - At least one number
9
+ * - At least one special character from allowed set: !@#$%^&_-+=<>
10
+ *
11
+ * Backend regex: ^[A-Za-z0-9\!\@\#\$\%\^\&\_\-\+\=\<\>]*$
12
+ */
13
+ // Constants
14
+ const MIN_LENGTH = 10;
15
+ const ALLOWED_SPECIAL_CHARS = '!@#$%^&_-+=<>';
16
+ // Regex patterns - DRY principle
17
+ const REGEX = {
18
+ ALLOWED_CHARS: /^[A-Za-z0-9!@#$%^&_\-+=<>]*$/,
19
+ UPPERCASE: /[A-Z]/,
20
+ LOWERCASE: /[a-z]/,
21
+ NUMBER: /[0-9]/,
22
+ SPECIAL_CHAR: /[!@#$%^&_\-+=<>]/,
23
+ };
24
+ // Error messages - DRY principle
25
+ const ERROR_MESSAGES = {
26
+ MIN_LENGTH: `Password must be at least ${MIN_LENGTH} characters long`,
27
+ INVALID_CHARS: `Password contains invalid characters. Only letters, numbers, and (${ALLOWED_SPECIAL_CHARS}) are allowed`,
28
+ UPPERCASE: 'Password must include at least one uppercase letter',
29
+ LOWERCASE: 'Password must include at least one lowercase letter',
30
+ NUMBER: 'Password must include at least one number',
31
+ SPECIAL_CHAR: `Password must include at least one special character (${ALLOWED_SPECIAL_CHARS})`,
32
+ };
33
+ // SRP: Individual validation functions
34
+ const hasMinLength = (password) => password.length >= MIN_LENGTH;
35
+ const hasOnlyAllowedChars = (password) => REGEX.ALLOWED_CHARS.test(password);
36
+ const hasUppercase = (password) => REGEX.UPPERCASE.test(password);
37
+ const hasLowercase = (password) => REGEX.LOWERCASE.test(password);
38
+ const hasNumber = (password) => REGEX.NUMBER.test(password);
39
+ const hasSpecialChar = (password) => REGEX.SPECIAL_CHAR.test(password);
40
+ export const validatePassword = (password) => {
41
+ const errors = [];
42
+ if (!password || !hasMinLength(password)) {
43
+ errors.push(ERROR_MESSAGES.MIN_LENGTH);
44
+ }
45
+ if (!hasOnlyAllowedChars(password)) {
46
+ errors.push(ERROR_MESSAGES.INVALID_CHARS);
47
+ }
48
+ if (!hasUppercase(password)) {
49
+ errors.push(ERROR_MESSAGES.UPPERCASE);
50
+ }
51
+ if (!hasLowercase(password)) {
52
+ errors.push(ERROR_MESSAGES.LOWERCASE);
53
+ }
54
+ if (!hasNumber(password)) {
55
+ errors.push(ERROR_MESSAGES.NUMBER);
56
+ }
57
+ if (!hasSpecialChar(password)) {
58
+ errors.push(ERROR_MESSAGES.SPECIAL_CHAR);
59
+ }
60
+ return {
61
+ isValid: errors.length === 0,
62
+ errors,
63
+ };
64
+ };
65
+ export const getPasswordRequirements = (password) => {
66
+ const requirements = [
67
+ {
68
+ met: hasMinLength(password),
69
+ message: `At least ${MIN_LENGTH} characters`,
70
+ },
71
+ {
72
+ met: hasUppercase(password),
73
+ message: 'One uppercase letter',
74
+ },
75
+ {
76
+ met: hasLowercase(password),
77
+ message: 'One lowercase letter',
78
+ },
79
+ {
80
+ met: hasNumber(password),
81
+ message: 'One number',
82
+ },
83
+ {
84
+ met: hasOnlyAllowedChars(password) && hasSpecialChar(password),
85
+ message: `One special character from: ${ALLOWED_SPECIAL_CHARS}`,
86
+ },
87
+ ];
88
+ return requirements;
89
+ };
90
+ export const getPasswordErrorMessage = (password) => {
91
+ const validation = validatePassword(password);
92
+ if (validation.isValid)
93
+ return '';
94
+ // Return all errors as a single message
95
+ return validation.errors.join('. ');
96
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.59",
3
+ "version": "0.5.61",
4
4
  "description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -57,7 +57,7 @@
57
57
  "typescript": "^4.8.2"
58
58
  },
59
59
  "peerDependencies": {
60
- "@open-tender/types": "^0.4.172",
60
+ "@open-tender/types": "^0.4.174",
61
61
  "@reduxjs/toolkit": "^1.8.5",
62
62
  "react": "^18.2.0"
63
63
  },