Created
March 3, 2012 19:51
Revisions
-
John Hann revised this gist
Mar 4, 2012 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -19,8 +19,10 @@ console.log(expression({ expression: "document" })()); } } // If we wanted to, we could allow access to the global scope and allow stuff like this, but I'd rather not: "$('.allMyNodes')"; // access to other dom nodes "dojo.date.locale.format(value)"; // access to globals // I don't think we can stop devs from doing this: "item.otherProp = value * 100; value;"; // side-effects, non-deterministic behavior -
John Hann revised this gist
Mar 3, 2012 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -29,9 +29,9 @@ define(function () { : function (factory) { module.exports = factory(); }, function (value, item) { var window, document; // the only variables in scope are value, item, and any globals // not listed in the var statement above. we have to cast to string // because of "oddities" between `eval` and `this`. return eval('' + this); } )); -
John Hann revised this gist
Mar 3, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,6 +28,7 @@ define(function () { ? define : function (factory) { module.exports = factory(); }, function (value, item) { var window, document; // the only variables in scope are value, item, and any globals. // we have to cast to string because of "oddities" between // `eval` and `this`. -
John Hann revised this gist
Mar 3, 2012 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,17 @@ curl(['cola/transform/expression'], function (expression) { console.log(expression({ expression: "value * 100 + '%'" })(0.5, {})); console.log(expression({ expression: "new Date()" })(1330804406919, {})); var item = { firstName: 'Ralph', lastName: 'Emerson' }; console.log(expression({ expression: "item.firstName + (item.middleName ? ' ' + item.middleName : '') + ' ' + item.lastName" })('whatevs', item)); console.log(expression({ expression: "document" })()); }); // hypothetical wirey usage: { bindings: { dob: { expression: "new Date(value)" }, -
John Hann revised this gist
Mar 3, 2012 . 1 changed file with 24 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ curl(['cola/transform/expression'], function (expression) { console.log(expression(0.5, {}, { expression: "value * 100 + '%'" })); console.log(expression(1330804406919, {}, { expression: "new Date(value)" })); var item = { firstName: 'Ralph', lastName: 'Emerson' }; console.log(expression('whatevs', item, { expression: "item.firstName + (item.middleName ? ' ' + item.middleName : '') + ' ' + item.lastName" })); }); // hypothetical usage: { bindings: { dob: { expression: "new Date(value)" }, displayName: { expression: "item.firstName + (item.middleName ? ' ' + item.middleName : '') + ' ' + item.lastName" } } } //The downside? It allows stuff like this: "$('.allMyNodes')"; // access to other dom nodes "dojo.date.locale.format(value)"; // access to globals "item.otherProp = value * 100; value;"; // side-effects, non-deterministic behavior -
John Hann created this gist
Mar 3, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ /** 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); } ));