Skip to content

Commit dad4560

Browse files
committed
Updated README and added interface for function parameters
1 parent 5f78f38 commit dad4560

File tree

3 files changed

+152
-15
lines changed

3 files changed

+152
-15
lines changed

README.md

+90-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,90 @@
1-
# deno-password-checker
1+
# Password Checker
2+
3+
A Deno module to test if a password/string matches the preset criteria.
4+
5+
## Installation
6+
7+
`import { checkPassword } from "https://deno.land/x/password-checker/mods.ts";`
8+
9+
## Parameters
10+
11+
### Mandatory Parameters:
12+
13+
- password: string
14+
15+
### Optional Parameters:
16+
17+
- minLen: number - To check the password against a minimum length. Defaults to 0
18+
which disables the check.
19+
- maxLen: number - To check the password against a maximum length. Defaults to 0
20+
which disables the check.
21+
- containsNum: boolean - To check if the password contains any numbers. Defaults
22+
to true and enables the check.
23+
- containsSpecialChar: boolean - To check if the password contains any special
24+
characters. Defaults to true and enables the check.
25+
- containsAlphabet: boolean - To check if the password contains any alphabets.
26+
Defaults to true and enables the check.
27+
28+
## Usage
29+
30+
```typescript
31+
import { checkPassword } from "https://deno.land/x/password-checker/mods.ts";
32+
33+
const passwordString: string = "randomPassword123!.";
34+
35+
let isPasswordValid: boolean;
36+
37+
// Default case which checks if password is alphanumeric and contains special characters
38+
isPasswordValid = checkPassword({ password: passWordString });
39+
40+
// To set minimum length of password
41+
isPasswordValid = checkPassword({ password: passwordString, minLen: 5 });
42+
43+
// To set maximum length of password
44+
isPasswordValid = checkPassword({
45+
password: passwordString,
46+
minLen: 5,
47+
maxLen: 12,
48+
});
49+
50+
// To disable number check on password
51+
isPasswordValid = checkPassword({
52+
password: passwordString,
53+
containsNum: false,
54+
});
55+
56+
// To disable alphanumeric check on password
57+
isPasswordValid = checkPassword({
58+
password: passwordString,
59+
containsNum: false,
60+
containsAlphabet: false,
61+
});
62+
63+
// To disable special characters check on password
64+
isPasswordValid = checkPassword({
65+
password: passwordString,
66+
containsSpecialChar: false,
67+
});
68+
```
69+
70+
## License
71+
72+
This package is published under the Unlicense. For more information, see the
73+
accompanying LICENSE file.
74+
75+
<br>
76+
77+
---
78+
79+
<br>
80+
81+
### PS:
82+
83+
If you find this package useful, please consider giving a
84+
[star](https://github.com/arghyadeep-k/deno-password-checker) to this project on
85+
Github.
86+
87+
And, if you are willing to [buy me a coffee](https://ko-fi.com/arghyadeep), that
88+
would be awesome. :)
89+
90+
<a href='https://ko-fi.com/arghyadeep' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi1.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

mod.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
export function checkPassword(
2-
password: string,
1+
interface params {
2+
password: string;
3+
minLen?: number;
4+
maxLen?: number;
5+
containsNum?: boolean;
6+
containsSpecialChar?: boolean;
7+
containsAlphabet?: boolean;
8+
}
9+
export function checkPassword({
10+
password,
311
minLen = 0,
412
maxLen = 0,
5-
containsNum = false,
6-
containsSpecialChar = false,
7-
containsAlphabets = false,
8-
): boolean {
13+
containsNum = true,
14+
containsSpecialChar = true,
15+
containsAlphabet = true,
16+
}: params): boolean {
917
if (minLen != 0) {
1018
if (password.length < minLen) {
1119
return false;
@@ -26,7 +34,7 @@ export function checkPassword(
2634
return false;
2735
}
2836
}
29-
if (containsAlphabets) {
37+
if (containsAlphabet) {
3038
if (password.search(/[A-Za-z]/) == -1) {
3139
return false;
3240
}

mod_test.ts

+47-7
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,69 @@ import { assertEquals } from "./test_deps.ts";
22
import { checkPassword } from "./mod.ts";
33

44
Deno.test("Test with defaults", () => {
5-
assertEquals(checkPassword("random"), true);
5+
assertEquals(checkPassword({ password: "random123." }), true);
66
});
77

88
Deno.test("Test for minimum length", () => {
9-
assertEquals(checkPassword("random123", 10), false);
9+
assertEquals(checkPassword({ password: "random123", minLen: 10 }), false);
1010
});
1111

1212
Deno.test("Test for maximum length", () => {
13-
assertEquals(checkPassword("random123", 2, 5), false);
13+
assertEquals(
14+
checkPassword({ password: "random123", minLen: 2, maxLen: 5 }),
15+
false,
16+
);
1417
});
1518

1619
Deno.test("Test for numbers", () => {
17-
assertEquals(checkPassword("random", 2, 10, true), false);
20+
assertEquals(
21+
checkPassword({
22+
password: "random",
23+
minLen: 2,
24+
maxLen: 10,
25+
containsNum: true,
26+
}),
27+
false,
28+
);
1829
});
1930

2031
Deno.test("Test for special characters", () => {
21-
assertEquals(checkPassword("random123", 2, 10, true, true), false);
32+
assertEquals(
33+
checkPassword({
34+
password: "random123",
35+
minLen: 2,
36+
maxLen: 10,
37+
containsNum: true,
38+
containsSpecialChar: true,
39+
}),
40+
false,
41+
);
2242
});
2343

2444
Deno.test("Test for alphabets", () => {
25-
assertEquals(checkPassword("123456.!", 2, 10, true, true, true), false);
45+
assertEquals(
46+
checkPassword({
47+
password: "123456.!",
48+
minLen: 2,
49+
maxLen: 10,
50+
containsNum: true,
51+
containsSpecialChar: true,
52+
containsAlphabet: true,
53+
}),
54+
false,
55+
);
2656
});
2757

2858
Deno.test("Test for all options", () => {
29-
assertEquals(checkPassword("abc123456.!", 2, 12, true, true, true), true);
59+
assertEquals(
60+
checkPassword({
61+
password: "abc123456.!",
62+
minLen: 2,
63+
maxLen: 12,
64+
containsNum: true,
65+
containsSpecialChar: true,
66+
containsAlphabet: true,
67+
}),
68+
true,
69+
);
3070
});

0 commit comments

Comments
 (0)