Permalink
Browse files

added a function implementation

  • Loading branch information...
1 parent 51bf713 commit be7d58f04184bd98bbcaa6a8c2918f7aef1bd50a @chrisdew committed Nov 28, 2012
Showing with 32 additions and 15 deletions.
  1. +24 −15 lib/table.js
  2. +8 −0 test/table-test.js
View
@@ -105,24 +105,33 @@ function createFn(expr) {
}
}
- // by here we assume that it's a 2-arg function
assert(expr.fn);
- assert(expr.args.length === 2);
- var fn0 = createFn(expr.args[0]);
- var fn1 = createFn(expr.args[1]);
- if (expr.fn === 'equal') {
- return function(r) {
- return fn0(r) === fn1(r);
+ if (expr.args.length === 0) {
+ // 0-arity functions
+ } else if (expr.args.length === 1) {
+ var fn0 = createFn(expr.args[0]);
+ if (expr.fn === 'equalsOne') {
+ return function(r) {
+ return fn0(r) === 1;
+ }
}
- }
- if (expr.fn === 'and') {
- return function(r) {
- return fn0(r) && fn1(r);
+ } else if (expr.args.length === 2) {
+ var fn0 = createFn(expr.args[0]);
+ var fn1 = createFn(expr.args[1]);
+ if (expr.fn === 'equal') {
+ return function(r) {
+ return fn0(r) === fn1(r);
+ }
}
- }
- if (expr.fn === 'gt') {
- return function(r) {
- return fn0(r) > fn1(r);
+ if (expr.fn === 'and') {
+ return function(r) {
+ return fn0(r) && fn1(r);
+ }
+ }
+ if (expr.fn === 'gt') {
+ return function(r) {
+ return fn0(r) > fn1(r);
+ }
}
}
throw "cannot handle " + JSON.stringify(expr);
View
@@ -76,5 +76,13 @@ describe('Table', function() {
console.log(JSON.stringify(bar));
});
+ it('should update a using a named function in the where clause', function(done) {
+ bar.once('delta', function(delta) {
+ assert.deepEqual({"op":"update","table":"bar","pk":"1","set":[{"field":"foo","expr":"hello2"}]}, delta);
+ done();
+ });
+ bar.update({"table":"bar","set":[{"field":"foo","expr":"hello2"}],"where":{"expr":{"fn":"equalsOne","args":[{field:"id"}]}}});
+ });
+
});

0 comments on commit be7d58f

Please sign in to comment.