|
|
@@ -8,44 +8,44 @@
|
|
|
* @constructor
|
|
|
**/
|
|
|
var CondExpression = module.exports = function CondExpression(vars) {
|
|
|
- this.nargs = 3;
|
|
|
- this.pCond = this.evaluateInternal(vars);
|
|
|
- this.idx = this.pCond.coerceToBool() ? 1 : 2;
|
|
|
-
|
|
|
- if (arguments.length !== 3) throw new Error("three args expected");
|
|
|
- base.call(this);
|
|
|
-}, klass = CondExpression,
|
|
|
- base = require("./NaryExpression"),
|
|
|
- proto = klass.prototype = Object.create(base.prototype, {
|
|
|
- constructor: {
|
|
|
- value: klass
|
|
|
- }
|
|
|
- });
|
|
|
+ if (arguments.length !== 0) throw new Error("zero args expected");
|
|
|
+ base.call(this);
|
|
|
+ }, klass = CondExpression,
|
|
|
+ base = require("./FixedArityExpression")(klass, 3),
|
|
|
+ proto = klass.prototype = Object.create(base.prototype, {
|
|
|
+ constructor: {
|
|
|
+ value: klass
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
// DEPENDENCIES
|
|
|
var Value = require("../Value"),
|
|
|
Expression = require("./Expression");
|
|
|
|
|
|
// PROTOTYPE MEMBERS
|
|
|
+klass.opName = "$cond";
|
|
|
proto.getOpName = function getOpName() {
|
|
|
- return "$cond";
|
|
|
+ return klass.opName;
|
|
|
};
|
|
|
|
|
|
klass.parse = function parse(expr, vps) {
|
|
|
- this.checkArgLimit(3);
|
|
|
+ // There may only be one argument - an array of 3 items, or a hash containing 3 keys.
|
|
|
+ //this.checkArgLimit(3);
|
|
|
|
|
|
// if not an object, return;
|
|
|
if (typeof(expr) !== Object)
|
|
|
return Expression.parse(expr, vps);
|
|
|
|
|
|
- // verify
|
|
|
- if (Expression.parseOperand(expr) !== "$cond")
|
|
|
- throw new Error("Invalid expression");
|
|
|
+ if(!(klass.opName in expr)) {
|
|
|
+ throw new Error("Invalid expression. Expected to see '"+klass.opName+"'");
|
|
|
+ }
|
|
|
|
|
|
var ret = new CondExpression();
|
|
|
|
|
|
- var ex = Expression.parseObject(expr);
|
|
|
+ // If this is an Object and not an array, verify all the bits are specified.
|
|
|
+ // If this is an Object that is an array, verify there are three bits.
|
|
|
var args = Expression.parseOperand(expr, vps);
|
|
|
+
|
|
|
if (args[0] !== "if")
|
|
|
throw new Error("Missing 'if' parameter to $cond");
|
|
|
if (args[1] !== "then")
|
|
|
@@ -58,7 +58,10 @@ klass.parse = function parse(expr, vps) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Use the $cond operator with the following syntax: { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
|
|
|
+ * Use the $cond operator with the following syntax:
|
|
|
+ * { $cond: { if: <boolean-expression>, then: <true-case>, else: <false-case-> } }
|
|
|
+ * -or-
|
|
|
+ * { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
|
|
|
* @method evaluate
|
|
|
**/
|
|
|
proto.evaluateInternal = function evaluateInternal(vars) {
|
|
|
@@ -75,4 +78,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
|
|
|
};
|
|
|
|
|
|
/** Register Expression */
|
|
|
-Expression.registerExpression("$cond", klass.parse);
|
|
|
+Expression.registerExpression(klass.opName, klass.parse);
|