TomFrost / Jexl

Javascript Expression Language: Powerful context-based expression parser and evaluator
MIT License
573 stars 94 forks source link

Map collect #6

Closed will-weiss closed 9 years ago

will-weiss commented 9 years ago

Got one more for ya!

This PR adds native map/collect to Jexl. The <| and |> characters indicate a subexpression to be applied over the values of the incoming subject. Within the context of that subexpression # and code>@</code refer to the key and value of the element, respectively.

For example,

jexl.eval("foo <| @bar + # + 5 |>", {foo: [{bar: 5}, {bar: 10}, {bar: 15}]}).then(function(res) {
    console.log(res); // Output: [10, 16, 22]
});

Also cleaned up some earlier code and added some tests.