Skip to content

Instantly share code, notes, and snippets.

@MrAntix
Created March 14, 2016 16:51
Show Gist options
  • Save MrAntix/9d58bcc5db787f50abf9 to your computer and use it in GitHub Desktop.
Save MrAntix/9d58bcc5db787f50abf9 to your computer and use it in GitHub Desktop.
Dynamic Filter
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