Skip to content

Instantly share code, notes, and snippets.

@nbenns
Last active January 12, 2016 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbenns/5c0fa7df0e8db12c165f to your computer and use it in GitHub Desktop.
Save nbenns/5c0fa7df0e8db12c165f to your computer and use it in GitHub Desktop.
'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