expressions-js
Advanced tools
Comparing version 0.1.3 to 0.1.4
{ | ||
"name": "expressions-js", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Converts simplified JavaScript expressions into executable functions for JavaScript frameworks.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -19,3 +19,3 @@ # Expressions.js | ||
``` | ||
```js | ||
var user = { name: 'Jacob' }; | ||
@@ -33,3 +33,3 @@ var product = { name: 'Toothbrush' }; | ||
``` | ||
```js | ||
var setName = expressions.parseSetter('name'); | ||
@@ -46,3 +46,3 @@ | ||
``` | ||
```js | ||
expressions.parse('user.name'); | ||
@@ -84,3 +84,3 @@ | ||
to `globals`. For example: | ||
``` | ||
```js | ||
var expr = expressions.parse('_.map(obj, mapper)', { "_": _ }); | ||
@@ -99,3 +99,3 @@ ``` | ||
globals to provide functionality to all expressions. Example: | ||
``` | ||
```js | ||
expressions.globals._ = require('underscore'); | ||
@@ -114,3 +114,3 @@ ``` | ||
``` | ||
```js | ||
// Using formatters | ||
@@ -144,3 +144,3 @@ var formatters = { | ||
``` | ||
```js | ||
var formatters = { | ||
@@ -157,3 +157,3 @@ lower: function(value) { | ||
``` | ||
```js | ||
formatters.filter = function(value, filterFunc) { | ||
@@ -170,3 +170,3 @@ return Array.isArray(value) ? value.filter(filterFunc) : value; | ||
``` | ||
```js | ||
formatters.isoDate = function(value, isSetter) { | ||
@@ -187,3 +187,3 @@ if (isSetter) { | ||
to pass the value of the setter in. Here is an example: | ||
``` | ||
```js | ||
var context = { | ||
@@ -210,7 +210,7 @@ add: function(a, b) { | ||
``` | ||
```js | ||
expressions.parse('name'); | ||
``` | ||
creates | ||
``` | ||
```js | ||
function() { | ||
@@ -220,7 +220,7 @@ return this.name; | ||
``` | ||
``` | ||
```js | ||
expressions.parseSetter('name'); | ||
``` | ||
creates | ||
``` | ||
```js | ||
function(_value_) { | ||
@@ -248,7 +248,7 @@ this.name = _value_; | ||
``` | ||
```js | ||
expressions.parse('user.name'); | ||
``` | ||
creates | ||
``` | ||
```js | ||
function() { | ||
@@ -259,7 +259,7 @@ var _ref1; | ||
``` | ||
``` | ||
```js | ||
expressions.parseSetter('user.name'); | ||
``` | ||
creates | ||
``` | ||
```js | ||
function(value) { | ||
@@ -279,3 +279,3 @@ var _ref1; | ||
``` | ||
```js | ||
var globals = { moment: require('moment') }; | ||
@@ -285,3 +285,3 @@ expression.parse('moment(friend.birthday).format()', globals); | ||
creates | ||
``` | ||
```js | ||
function(_globals_) { | ||
@@ -296,7 +296,7 @@ var _ref1, _ref2; | ||
``` | ||
```js | ||
expression.parse('group.members | filter(isAdmin)'); | ||
``` | ||
creates | ||
``` | ||
```js | ||
function(_formatters_) { | ||
@@ -303,0 +303,0 @@ var _ref1; |
@@ -11,4 +11,4 @@ var slice = Array.prototype.slice; | ||
exports.parse = function(expr, globals, formatters, extraArgs) { | ||
if (!Array.isArray(extraArgs)) extraArgs = []; | ||
exports.parse = function(expr, globals, formatters) { | ||
var extraArgs = slice.call(arguments, 3); | ||
var cacheKey = expr + '|' + extraArgs.join(','); | ||
@@ -41,6 +41,4 @@ // Returns the cached function for this expression if it exists. | ||
exports.parseSetter = function(expr, globals, formatters, extraArgs) { | ||
if (!Array.isArray(extraArgs)) extraArgs = []; | ||
var extraArgs = slice.call(arguments, 3); | ||
// Add _value_ as the first extra argument | ||
extraArgs.unshift(valueProperty); | ||
if (expr.charAt(0) === '!') { | ||
@@ -53,3 +51,4 @@ // Allow '!prop' to become 'prop = !value' | ||
return exports.parse(expr, globals, formatters, extraArgs); | ||
// Add _value_ as the first extra argument | ||
return exports.parse.apply(exports, [expr, globals, formatters, valueProperty].concat(extraArgs)); | ||
}; | ||
@@ -56,0 +55,0 @@ |
@@ -211,2 +211,15 @@ var expect = require('chai').expect; | ||
}); | ||
it('should suppor extra arguments', function() { | ||
var callFooBar = expressions.parse('foo(bar, bash)', null, null, 'bar', 'bash'); | ||
var passedArg1, passedArg2, bar = {}, bash = {}; | ||
callFooBar.call({ | ||
foo: function(arg1, arg2) { | ||
passedArg1 = arg1; | ||
passedArg2 = arg2; | ||
} | ||
}, bar, bash); | ||
expect(passedArg1).to.equal(bar); | ||
expect(passedArg2).to.equal(bash); | ||
}); | ||
}); | ||
@@ -213,0 +226,0 @@ |
37316
650