Last active
February 16, 2018 16:40
-
-
Save viniceosm/4f03af77732003282df3bb6574fd996d to your computer and use it in GitHub Desktop.
Montar as possibilidades através de if/else
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
class PossibilidadesIf { | |
constructor() { | |
this.arrSe = []; | |
this.syntaxesComParametro = ['se', 'senao', 'log', 'getMensagem']; | |
this.syntaxesSemParametro = ['fimse']; | |
} | |
se(str) { | |
this.arrSe.push(str); | |
} | |
senao(str) { | |
this.arrSe.pop(); | |
this.arrSe.push(str); | |
} | |
fimse() { | |
this.arrSe.pop(); | |
} | |
log(str) { | |
console.log(this.getMensagem(str)); | |
} | |
getMensagem(str) { | |
return (this.arrSe.join(' && ') + (str != undefined ? '\n\t\t' + str : '')); | |
} | |
evaluate(fn) { | |
let str = adicionaThisNasFuncoes(fn.toString(), this); | |
eval('let fn = ' + str + '; fn()'); | |
function adicionaThisNasFuncoes(str, o) { | |
for (let palavraSubstituir of o.syntaxesComParametro) { | |
str = str.replace(new RegExp(palavraSubstituir + "\\(\\'", "g"), 'this.' + palavraSubstituir + '(\''); | |
}; | |
for (let palavraSubstituir of o.syntaxesSemParametro) { | |
str = str.replace(new RegExp(palavraSubstituir + "\\(\\)", "g"), 'this.' + palavraSubstituir + '()'); | |
} | |
return str; | |
} | |
} | |
} | |
var possibilidadesIf = new PossibilidadesIf(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exemplo 1
Exemplo 2
output