Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions src/components/Settings/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Checkbox from '../Checkbox/Checkbox';
import BtnGenerate from '../BtnGenerate/BtnGenerate';
import './Settings.css';
import { savePassword, copyPassword } from '../../redux/actions';
import caracters from '../../data/caracters';
import createPassword from '../../helpers/functions';

class Settings extends Component {
constructor() {
Expand All @@ -20,31 +20,11 @@ class Settings extends Component {
}
}

createPassword = () => {
const { lengthPassword } = this.props;
const options = Object.entries(this.state).filter((entrie) => entrie[1]);
let password = '';
let temporaryOptions = [...options];

while (password.length < lengthPassword) {
if (temporaryOptions.length === 0) temporaryOptions = [...options];

const indexOptions = Math.ceil(Math.random() * temporaryOptions.length - 1);
const typeCaracter = temporaryOptions[indexOptions][0];

const indexCaracter = Math.ceil(Math.random() * caracters[typeCaracter].length) - 1;
password += caracters[typeCaracter][indexCaracter];

temporaryOptions.splice(indexOptions, 1);
};

return password;
}

handleClick = (event) => {
event.preventDefault();
const { savePassword, copyPassword } = this.props;
const password = this.createPassword();
const { savePassword, copyPassword, lengthPassword } = this.props;
const options = Object.entries(this.state).filter((entrie) => entrie[1]);
const password = createPassword(lengthPassword, options);

savePassword(password);
copyPassword();
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import caracters from '../data/caracters';

const createPassword = (lengthPassword, options) => {
let password = '';
let temporaryOptions = [...options];

while (password.length < lengthPassword) {
if (temporaryOptions.length === 0) temporaryOptions = [...options];

const indexOptions = Math.ceil(Math.random() * temporaryOptions.length - 1);
const typeCaracter = temporaryOptions[indexOptions][0];

const indexCaracter = Math.ceil(Math.random() * caracters[typeCaracter].length) - 1;
password += caracters[typeCaracter][indexCaracter];

temporaryOptions.splice(indexOptions, 1);
}

return password;
};

export default createPassword;