Created
March 14, 2016 16:51
-
-
Save MrAntix/9d58bcc5db787f50abf9 to your computer and use it in GitHub Desktop.
Dynamic Filter
This file contains hidden or 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
var properties = { Two:true, Three:1 }; | |
var filters = ['One===true', 'Two===true', 'Three>6', 'Three<6 || Two===true']; | |
var fnString = ''; | |
for(var name in properties) { | |
fnString +='var '+name+'='+properties[name]+';\n'; | |
} | |
console.log(fnString); | |
for(var i = 0; i < filters.length; i++) { | |
var filter = filters[i]; | |
var fnStringFilter = fnString+'try{ return '+filter+'; } catch(e){ return false; }'; | |
console.log(fnStringFilter); | |
var fn = new Function(fnStringFilter); | |
console.log(fn()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment