Skip to content

Commit 4e79241

Browse files
committed
Merged if conditions to reduce cognitive complexity
1 parent 7a66942 commit 4e79241

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

mod.ts

+33-42
Original file line numberDiff line numberDiff line change
@@ -30,54 +30,45 @@ export function checkPasswordWithResult({
3030
containsAlphabet = true,
3131
checkWithCommonPasswords = false,
3232
}: IParams): VerificationResult {
33-
if (minLen != 0) {
34-
if (password.length < minLen) {
35-
return {
36-
isValid: false,
37-
reason: `The password should contain at least ${minLen} characters`,
38-
};
39-
}
33+
if (minLen != 0 && password.length < minLen) {
34+
return {
35+
isValid: false,
36+
reason: `The password should contain at least ${minLen} characters`,
37+
};
4038
}
41-
if (maxLen != 0) {
42-
if (password.length > maxLen) {
43-
return {
44-
isValid: false,
45-
reason: `The password should contain at most ${maxLen} characters`,
46-
};
47-
}
39+
if (maxLen != 0 && password.length > maxLen) {
40+
return {
41+
isValid: false,
42+
reason: `The password should contain at most ${maxLen} characters`,
43+
};
4844
}
49-
if (containsNum) {
50-
if (password.search(/\d/) == -1) {
51-
return {
52-
isValid: false,
53-
reason: "The password should contain at least one digit",
54-
};
55-
}
45+
if (containsNum && password.search(/\d/) == -1) {
46+
return {
47+
isValid: false,
48+
reason: "The password should contain at least one digit",
49+
};
5650
}
57-
if (containsSpecialChar) {
58-
if (password.search(/[^\w\s]/) == -1) {
59-
return {
60-
isValid: false,
61-
reason: "The password should contain at least one special character",
62-
};
63-
}
51+
if (containsSpecialChar && password.search(/[^\w\s]/) == -1) {
52+
return {
53+
isValid: false,
54+
reason: "The password should contain at least one special character",
55+
};
6456
}
65-
if (containsAlphabet) {
66-
if (password.search(/[A-Za-z]/) == -1) {
67-
return {
68-
isValid: false,
69-
reason: "The password should contain at least one letter",
70-
};
71-
}
57+
if (containsAlphabet && password.search(/[A-Za-z]/) == -1) {
58+
return {
59+
isValid: false,
60+
reason: "The password should contain at least one letter",
61+
};
7262
}
7363

74-
if (checkWithCommonPasswords) {
75-
if (passwordList != undefined && passwordList.includes(password)) {
76-
return {
77-
isValid: false,
78-
reason: "The password should not be too common",
79-
};
80-
}
64+
if (
65+
checkWithCommonPasswords && passwordList != undefined &&
66+
passwordList.includes(password)
67+
) {
68+
return {
69+
isValid: false,
70+
reason: "The password should not be too common",
71+
};
8172
}
8273

8374
return { isValid: true };

0 commit comments

Comments
 (0)