use-password-policy 1.0.3 → 1.0.5

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.
package/README.md CHANGED
@@ -12,7 +12,7 @@ Features:
12
12
  Installation:
13
13
 
14
14
  ```
15
- npm install your-package-name
15
+ npm install use-password-policy
16
16
  ```
17
17
 
18
18
  Usage:
package/Type.ts CHANGED
@@ -8,3 +8,7 @@ export interface CustomPolicy {
8
8
  export interface StateObjects {
9
9
  [key: string]: boolean;
10
10
  }
11
+
12
+ export interface DefaultConfig {
13
+ [key: string]: boolean | RegExp | string | number;
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-password-policy",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "use-password-policy.ts",
6
6
  "scripts": {
@@ -1,6 +1,5 @@
1
1
  import { useState, useEffect, useMemo } from "react";
2
- import { CustomPolicy, StateObjects } from "./Type";
3
-
2
+ import { CustomPolicy, StateObjects, DefaultConfig } from "./Type";
4
3
 
5
4
  export const usePasswordPolicy = ({
6
5
  password,
@@ -13,15 +12,15 @@ export const usePasswordPolicy = ({
13
12
  customPolicies?: CustomPolicy[];
14
13
  useDefaultConfig?: boolean;
15
14
  }) => {
16
- const defaultConfig = {
15
+ const defaultConfig: DefaultConfig = {
17
16
  caseCheck: true,
18
17
  lengthCheck: true,
19
18
  digitCheck: true,
20
19
  specialCharCheck: true,
21
- minLength: 8, // Adjust minimum length as needed
22
- uppercaseCharRegex: /[A-Z]/, // Allow customization of regex patterns
20
+ minLength: 8,
21
+ uppercaseCharRegex: /[A-Z]/,
23
22
  digitRegex: /\d/,
24
- specialCharRegex: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/, // More inclusive special characters
23
+ specialCharRegex: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/,
25
24
  };
26
25
 
27
26
  const merged = useMemo(() => mergedConfig(defaultConfig, config), []);
@@ -36,12 +35,22 @@ export const usePasswordPolicy = ({
36
35
 
37
36
  const [policy, setPolicy] = useState<StateObjects>(() => {
38
37
  if (useDefaultConfig) {
39
- return {
38
+ const defaultStates: StateObjects = {
40
39
  caseCheck: false,
41
40
  lengthCheck: false,
42
41
  digitCheck: false,
43
42
  specialCharCheck: false,
44
- // Add properties for custom policies from mergedConfig
43
+ };
44
+ const safeConfig: StateObjects = config || {};
45
+
46
+ Object.entries(defaultStates).map(([key]) => {
47
+ if (safeConfig.hasOwnProperty(key) && safeConfig[key] === false) {
48
+ delete defaultStates[key];
49
+ delete defaultConfig[key];
50
+ }
51
+ });
52
+ return {
53
+ ...defaultStates,
45
54
  ...customPolicies.reduce(
46
55
  (acc, policy: { name: string }) => ({
47
56
  ...acc,
@@ -64,16 +73,24 @@ export const usePasswordPolicy = ({
64
73
 
65
74
  const evaluateChecks = () => {
66
75
  const newPolicy: StateObjects = { ...policy };
76
+ const policyKeys = Object.keys(policy);
77
+
67
78
  if (useDefaultConfig) {
68
- newPolicy.caseCheck =
69
- merged.caseCheck && RegExp(merged.uppercaseCharRegex).test(password);
70
- newPolicy.lengthCheck =
71
- merged.lengthCheck && password.length >= merged.minLength;
72
- newPolicy.digitCheck =
73
- merged.digitCheck && RegExp(merged.digitRegex).test(password);
74
- newPolicy.specialCharCheck =
75
- merged.specialCharCheck &&
76
- RegExp(merged.specialCharRegex).test(password);
79
+ policyKeys.forEach((key) => {
80
+ if (merged.hasOwnProperty(key) && policy.hasOwnProperty(key)) {
81
+ const checkFunction = {
82
+ caseCheck: () => RegExp(merged.uppercaseCharRegex).test(password),
83
+ lengthCheck: () => password.length >= merged.minLength,
84
+ digitCheck: () => RegExp(merged.digitRegex).test(password),
85
+ specialCharCheck: () =>
86
+ RegExp(merged.specialCharRegex).test(password),
87
+ }[key];
88
+
89
+ if (checkFunction) {
90
+ newPolicy[key] = merged[key] && checkFunction();
91
+ }
92
+ }
93
+ });
77
94
  }
78
95
 
79
96
  customPolicies.forEach((customPolicy: CustomPolicy) => {