Last active
December 7, 2019 22:24
-
-
Save leo-bianchi/7a50630bb334ed3ac6605b7c60c6e690 to your computer and use it in GitHub Desktop.
Refatorando condições
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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