Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Created March 3, 2012 19:51
Show Gist options
  • Save unscriptable/1967876 to your computer and use it in GitHub Desktop.
Save unscriptable/1967876 to your computer and use it in GitHub Desktop.
wire and/or cola function to allow an arbitrary expression to be evaluated
/** MIT License (c) copyright B Cavalier & J Hann */
(function (define, globalEval) {
define(function () {
"use strict";
/**
* Creates a function that can be used to evaluate arbitrary expressions
* on a data item.
* @param options {Object}
* @returns {Function} function (value, item) {}
* @description
*/
return function createExpressionTransform (options) {
return function expressionTransform (value, item) {
try {
return globalEval.call(options.expression, value, item);
}
catch (ex) {
return ex.message;
}
}
};
});
}(
typeof define == 'function'
? define
: function (factory) { module.exports = factory(); },
function (value, item) {
// the only variables in scope are value, item, and any globals.
// we have to cast to string because of "oddities" between
// `eval` and `this`.
return eval('' + this);
}
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment