From 0e6d52306b5d469bafef659705372fd0ad4c0407 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 11 Nov 2014 17:18:09 -0800 Subject: [PATCH] feat(change_detector): add support for ternary --- modules/change_detection/src/watch_group.js | 11 ++++++++++- modules/change_detection/test/change_detector_spec.js | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/modules/change_detection/src/watch_group.js b/modules/change_detection/src/watch_group.js index 3563852cd7..fd25d4f67f 100644 --- a/modules/change_detection/src/watch_group.js +++ b/modules/change_detection/src/watch_group.js @@ -3,7 +3,7 @@ import {ProtoRecord, Record, PROTO_RECORD_CONST, PROTO_RECORD_PURE_FUNCTION, import {FIELD, IMPLEMENTS, isBlank, isPresent, int, toBool, autoConvertAdd, BaseException} from 'facade/lang'; import {ListWrapper} from 'facade/collection'; import {AST, AccessMember, ImplicitReceiver, AstVisitor, LiteralPrimitive, - Binary, Formatter, MethodCall, FunctionCall, PrefixNot} from './parser/ast'; + Binary, Formatter, MethodCall, FunctionCall, PrefixNot, Conditional} from './parser/ast'; export class ProtoWatchGroup { @FIELD('headRecord:ProtoRecord') @@ -214,6 +214,14 @@ class ProtoRecordCreator { this.add(record); } + visitConditional(ast:Conditional, dest) { + var record = this.construct(PROTO_RECORD_PURE_FUNCTION, _cond, 3, dest); + ast.condition.visit(this, new Destination(record, 0)); + ast.trueExp.visit(this, new Destination(record, 1)); + ast.falseExp.visit(this, new Destination(record, 2)); + this.add(record); + } + createRecordsFromAST(ast:AST, memento){ ast.visit(this, memento); } @@ -267,4 +275,5 @@ function _operation_less_or_equals_then(left, right) {return left <= right;} function _operation_greater_or_equals_then(left, right) {return left >= right;} function _operation_logical_and(left, right) {return left && right;} function _operation_logical_or(left, right) {return left || right;} +function _cond(cond, trueVal, falseVal) {return cond ? trueVal : falseVal;} diff --git a/modules/change_detection/test/change_detector_spec.js b/modules/change_detection/test/change_detector_spec.js index b85507fc27..abe1ca2d84 100644 --- a/modules/change_detection/test/change_detector_spec.js +++ b/modules/change_detection/test/change_detector_spec.js @@ -126,6 +126,11 @@ export function main() { expect(executeWatch('exp', '!!true')).toEqual(['exp=true']); }); + it("should support conditionals", () => { + expect(executeWatch('m', '1 < 2 ? 1 : 2')).toEqual(['m=1']); + expect(executeWatch('m', '1 > 2 ? 1 : 2')).toEqual(['m=2']); + }); + it("should support formatters", () => { var formatters = { "uppercase" : (v) => v.toUpperCase(),