Skip to content

Instantly share code, notes, and snippets.

@leo-bianchi
Last active December 7, 2019 22:24
Show Gist options
  • Save leo-bianchi/7a50630bb334ed3ac6605b7c60c6e690 to your computer and use it in GitHub Desktop.
Save leo-bianchi/7a50630bb334ed3ac6605b7c60c6e690 to your computer and use it in GitHub Desktop.
Refatorando condições
console.time('test');
const saldosQtd = [
{
qtd: 1
},
{
qtd: 10
}
];
function verify(saldo, number) {
if (number < -60) {
return saldo.qtd + 1;
}
if (number >= -60 && number < -40) {
return saldo.qtd + 1;
}
}
const array = saldosQtd.map(saldo => {
return verify(saldo, -70);
});
console.timeEnd('test');
console.time('my test');
const saldosQtd = [
{
qtd: 1
},
{
qtd: 10
}
];
function getResult(type, arr, number) {
const conditions = {
less60() {
return number < -60 ? arr.qtd + 1 : false;
},
between60and40() {
return number >= -60 && number < -40 ? arr.qtd + 1 : false;
}
};
return conditions[type]();
}
// return getResult(number, arr);
// eslint-disable-next-line dot-notation
// const fn = conditions[type] ? conditions[type] : conditions['default'];
// return fn();
// }
const types = ['less60', 'between60and40'];
const array = saldosQtd.map(saldo => {
let res = false;
types.map(type => {
if (res) return;
res = getResult(type, saldo, -70);
});
return res;
});
console.timeEnd('my test');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment