Last active
January 12, 2016 18:02
-
-
Save nbenns/5c0fa7df0e8db12c165f to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const R = require('ramda'); | |
const rules = {"&": [ | |
{">": [ "!products.blah", 2 ]}, | |
{">": [ "!products.meow", 1 ]} | |
]}; | |
const basket = { products: { meow: 2, blah: 3 } }; | |
const Functions = { | |
"&": (o1, o2) => o1 && o2, | |
">": (o1, o2) => o1 > o2 | |
}; | |
let runDSL = (Func, Obj) => { | |
const operation = Object.keys(Func)[0]; | |
const operands = Func[operation]; | |
const args = operands.map((o) => { | |
const t = typeof o; | |
if (t == 'object') { | |
return runDSL(o, Obj); | |
} | |
else if (/^!/.exec(o)) { | |
const objLens = R.lensPath(o.slice(1).split('.')); | |
return R.view(objLens, Obj); | |
} | |
else return o; | |
}); | |
console.log(operation, args); | |
return Functions[operation].apply(null, args); | |
}; | |
const output = runDSL(rules, basket); | |
console.log(output); | |
/* Outputs: | |
* > [ 3, 2 ] | |
* > [ 2, 1 ] | |
* & [ true, true ] | |
* true | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment