Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Created March 3, 2012 19:51

Revisions

  1. John Hann revised this gist Mar 4, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion examples.js
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,10 @@ console.log(expression({ expression: "document" })());
    }
    }

    //The downside? It allows stuff like this:
    // 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
  2. John Hann revised this gist Mar 3, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions cola.transform.expression.js
    Original 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.
    // we have to cast to string because of "oddities" between
    // `eval` and `this`.
    // 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);
    }
    ));
  3. John Hann revised this gist Mar 3, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions cola.transform.expression.js
    Original 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`.
  4. John Hann revised this gist Mar 3, 2012. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions examples.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,17 @@
    curl(['cola/transform/expression'], function (expression) {

    console.log(expression(0.5, {}, { expression: "value * 100 + '%'" }));
    console.log(expression({ expression: "value * 100 + '%'" })(0.5, {}));

    console.log(expression(1330804406919, {}, { expression: "new Date(value)" }));
    console.log(expression({ expression: "new Date()" })(1330804406919, {}));

    var item = { firstName: 'Ralph', lastName: 'Emerson' };
    console.log(expression('whatevs', item, { expression: "item.firstName + (item.middleName ? ' ' + item.middleName : '') + ' ' + item.lastName" }));
    console.log(expression({ expression: "item.firstName + (item.middleName ? ' ' + item.middleName : '') + ' ' + item.lastName" })('whatevs', item));

    console.log(expression({ expression: "document" })());

    });

    // hypothetical usage:
    // hypothetical wirey usage:
    {
    bindings: {
    dob: { expression: "new Date(value)" },
  5. John Hann revised this gist Mar 3, 2012. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions examples.js
    Original 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
  6. John Hann created this gist Mar 3, 2012.
    36 changes: 36 additions & 0 deletions cola.transform.expression.js
    Original 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);
    }
    ));