Skip to content

Commit 063f437

Browse files
Merge pull request #2 from abdussalam-alali/main
Turn Code into NPM Package
2 parents 97774ac + b85aff2 commit 063f437

File tree

10 files changed

+142
-191
lines changed

10 files changed

+142
-191
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
.vscode/
3+
lib/
4+
node_modules/

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
.idea/
3+
.vscode/

README.md

Lines changed: 40 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,52 @@
1-
# JS-Password-Generator :lock_with_ink_pen:
1+
# JS-Password-Generator
2+
### Install:
3+
`npm i sos-password-generator`
24

3-
![image](assets/screenshot.jpg)
45

5-
It enables you to generate your own passwords with different degrees of power and security and adjust them to the length, characters, etc. You can include it in your website very easily, where no outside libraries have been used. Only Java Script.
6+
### Usage
7+
1. Require the package:
68

7-
This writing method is not the only one and you can generate the same results in different ways in the same language.
8-
I hope it's useful to you in your work, and I'm going to constantly improve its coordination and add new features to it.
9-
10-
- Explain Code </br>
11-
Here we use some function to generate a strings from Uppercase and lowercase letters, numbers and symbols
12-
```java-script
13-
function getRandomLower() {
14-
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
15-
}
16-
17-
function getRandomUpper() {
18-
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
19-
}
20-
21-
function getRandomNumber() {
22-
return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
23-
}
9+
```javascript
10+
const PasswordGenerator = require('sos-password-generator')
11+
```
2412

25-
function getRandomSymbol() {
26-
const symbols = "!@#$%^&*(){}[]=/<>,."
27-
return symbols[Math.floor(Math.random() * symbols.length)];
28-
}
13+
2. Create an instance of PowerGenerator Class
2914

30-
const randomFunc = {
31-
lower: getRandomLower,
32-
upper: getRandomUpper,
33-
number: getRandomNumber,
34-
symbol: getRandomSymbol,
35-
};
15+
```javascript
16+
const generator = new PasswordGenerator();
3617
```
37-
This is the code that executes the command when the button is pressed in order to collect and generate those values
38-
```java-script
39-
const generate = document.getElementById("generateBtn");
40-
generate.addEventListener("click", () => {
41-
const length = document.getElementById("PasswordLength").value;
42-
const hasUpper = document.getElementById("uppercase").checked;
43-
const hasLower = document.getElementById("lowercase").checked;
44-
const hasNumber = document.getElementById("numbers").checked;
45-
const hasSymbol = document.getElementById("symbols").checked;
46-
const result = document.getElementById("PasswordResult");
47-
result.innerText = generatePassword(
48-
hasLower,
49-
hasUpper,
50-
hasNumber,
51-
hasSymbol,
52-
length
53-
);
54-
});
55-
56-
function generatePassword(lower, upper, number, symbol, length){
57-
let generatedPassword = "";
58-
const typesCount = lower + upper + number + symbol;
59-
const typesArr = [{lower}, {upper}, {number}, {symbol}].filter(
60-
(item) => Object.values(item)[0]
61-
);
62-
for (let i = 0; i < length; i += typesCount){
63-
typesArr.forEach((type) => {
64-
const funcName = Object.keys(type)[0];
65-
generatedPassword += randomFunc[funcName]();
66-
});
67-
}
68-
const finalPassword = generatedPassword.slice(0, length);
69-
return finalPassword;
70-
}
18+
3. Generate your password:
19+
```javascript
20+
console.log(generator.generatePassword()); //Result => generates random 8-character-length password
21+
// contains numbers,lower case ,upper case, and symbols.
22+
```
23+
24+
25+
### Custom Options
26+
Before generating password,You can pass options object to the method init
27+
28+
```javascript
29+
generator.init({
30+
hasUpper:true,
31+
hasLower:true,
32+
hasSymbol:true,
33+
hasNumber:false,
34+
passwordLength:15,
35+
allowedSymbols: '!@#$',
36+
})
7137
```
72-
And the last ting is the code for the "copy to clipboard" button in order to copy the generated password.
7338

74-
```java-script
75-
let button = document.getElementById("clipboardBtn");
76-
button.addEventListener("click", (e) => {
77-
e.preventDefault();
78-
document.execCommand(
79-
"copy",
80-
false,
81-
document.getElementById("PasswordResult").select()
82-
);
83-
});
39+
### Default options
40+
41+
```javascript
42+
hasUpper:true
43+
hasLower:true
44+
hasSymbol:true
45+
hasNumber:true
46+
passwordLength:8
47+
allowedSymbols: '!@#$%^&*(){}[]=/<>,.'
8448
```
85-
Note: This is optional, you can not add it, it's up to how useful this code can be in your projects.
49+
8650

8751
Credits
8852
-------

assets/js.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

index.html

Lines changed: 0 additions & 49 deletions
This file was deleted.

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "sos-password-generator",
3+
"version": "1.0.0",
4+
"description": "Secure passwords generator",
5+
"main": "src/password-generator.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/abdussalam-alali/js-password-generator.git"
12+
},
13+
"author": "Syrian Open Source",
14+
"license": "ISC",
15+
"bugs": {
16+
"url": "https://github.com/abdussalam-alali/js-password-generator/issues"
17+
},
18+
"homepage": "https://github.com/abdussalam-alali/js-password-generator#readme"
19+
}

src/password-generator.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
class PasswordGenerator{
3+
constructor() {
4+
this.settings = {
5+
hasUpper:true,
6+
hasLower:true,
7+
hasSymbol:true,
8+
hasNumber:true,
9+
passwordLength:8,
10+
allowedSymbols: '!@#$%^&*(){}[]=/<>,.',
11+
};
12+
13+
this.password = "";
14+
}
15+
init(settings){
16+
this.settings = Object.assign({},this.settings,settings);
17+
}
18+
#getRandomCharacter(start,offset) {
19+
return String.fromCharCode(Math.floor(Math.random() * offset) + start);
20+
}
21+
22+
#randomFunc = {
23+
hasLower: ()=> { return this.#getRandomCharacter(97,26)},
24+
hasUpper: ()=> { return this.#getRandomCharacter(97,26)},
25+
hasNumber: ()=>{ return this.#getRandomCharacter(48,10) },
26+
hasSymbol: ()=> { return this.settings.allowedSymbols[Math.floor(Math.random() * this.settings.allowedSymbols.length)];
27+
},
28+
};
29+
generatePassword(){
30+
let password = "";
31+
32+
const typesCount = Object.values(this.settings).filter(item=>{if(item===true) return item}).length;
33+
const typesArr = Object.keys(this.settings)
34+
.filter((item) =>
35+
{ if(this.settings[item]===true)
36+
return item
37+
});
38+
39+
for (let i = 0; i < this.settings.passwordLength; i += typesCount){
40+
typesArr.forEach((type) => {
41+
password += this.#randomFunc[type]();
42+
});
43+
44+
}
45+
password = password.slice(0,this.settings.passwordLength);
46+
for(let i =0; i<16; i++)
47+
{
48+
password = this.#shuffle(password);
49+
}
50+
return password;
51+
}
52+
53+
54+
#shuffle(s) {
55+
var arr = s.split('');
56+
var n = arr.length;
57+
58+
for(var i=0 ; i<n-1 ; ++i) {
59+
var j = Math.floor(Math.random() * n);
60+
61+
var temp = arr[i];
62+
arr[i] = arr[j];
63+
arr[j] = temp;
64+
}
65+
66+
s = arr.join('');
67+
return s;
68+
}
69+
}
70+
71+
module.exports = PasswordGenerator;
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)