From b1e71429f0833932594b93e5be0b18271037473d Mon Sep 17 00:00:00 2001 From: Matheus Ferreira Date: Wed, 9 Feb 2022 20:20:50 -0300 Subject: [PATCH] =?UTF-8?q?fun=C3=A7=C3=A3o=20refatorada?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Settings/Settings.js | 28 ++++------------------------ src/helpers/functions.js | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 src/helpers/functions.js diff --git a/src/components/Settings/Settings.js b/src/components/Settings/Settings.js index d9bfb34..ea7c3eb 100644 --- a/src/components/Settings/Settings.js +++ b/src/components/Settings/Settings.js @@ -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() { @@ -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(); diff --git a/src/helpers/functions.js b/src/helpers/functions.js new file mode 100644 index 0000000..a48e0f1 --- /dev/null +++ b/src/helpers/functions.js @@ -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;