| 12345678910111213141516171819202122232425262728293031 |
- var CondExpression = module.exports = (function(){
- // CONSTRUCTOR
- /* $cond expression; @see evaluate */
- var klass = module.exports = function CondExpression(){
- if(arguments.length !== 0) throw new Error("zero args expected");
- base.call(this);
- }, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
- // DEPENDENCIES
- var Value = require("../Value");
- // PROTOTYPE MEMBERS
- proto.getOpName = function getOpName(){
- return "$cond";
- };
- proto.addOperand = function addOperand(expr) {
- this.checkArgLimit(3);
- base.addOperand(expr);
- };
- /** Use the $cond operator with the following syntax: { $cond: [ <boolean-expression>, <true-case>, <false-case> ] } **/
- proto.evaluate = function evaluate(doc){
- this.checkArgCount(3);
- var pCond = this.operands[0].evaluate(doc),
- idx = Value.coerceToBool(pCond) ? 1 : 2;
- return this.operands[idx].evaluate(doc);
- };
- return klass;
- })();
|