Search in sources :

Example 26 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class MaxFunction method computeForJsSrc.

@Override
public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg0 = args.get(0);
    JsExpr arg1 = args.get(1);
    return new JsExpr("Math.max(" + arg0.getText() + ", " + arg1.getText() + ")", Integer.MAX_VALUE);
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr)

Example 27 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class RandomIntFunction method computeForJsSrc.

@Override
public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg = args.get(0);
    JsExpr random = new JsExpr("Math.random()", Integer.MAX_VALUE);
    JsExpr randomTimesArg = SoyJsPluginUtils.genJsExprUsingSoySyntax(Operator.TIMES, Lists.newArrayList(random, arg));
    return new JsExpr("Math.floor(" + randomTimesArg.getText() + ")", Integer.MAX_VALUE);
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr)

Example 28 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class RoundFunction method computeForJsSrc.

@Override
public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr value = args.get(0);
    JsExpr numDigitsAfterPt = (args.size() == 2) ? args.get(1) : null;
    int numDigitsAfterPtAsInt = convertNumDigits(numDigitsAfterPt);
    if (numDigitsAfterPtAsInt == 0) {
        // Case 1: round() has only one argument or the second argument is 0.
        return new JsExpr("Math.round(" + value.getText() + ")", Integer.MAX_VALUE);
    } else if ((numDigitsAfterPtAsInt >= 0 && numDigitsAfterPtAsInt <= 12) || numDigitsAfterPtAsInt == Integer.MIN_VALUE) {
        String shiftExprText;
        if (numDigitsAfterPtAsInt >= 0 && numDigitsAfterPtAsInt <= 12) {
            shiftExprText = "1" + "000000000000".substring(0, numDigitsAfterPtAsInt);
        } else {
            shiftExprText = "Math.pow(10, " + numDigitsAfterPt.getText() + ")";
        }
        JsExpr shift = new JsExpr(shiftExprText, Integer.MAX_VALUE);
        JsExpr valueTimesShift = SoyJsPluginUtils.genJsExprUsingSoySyntax(Operator.TIMES, Lists.newArrayList(value, shift));
        return new JsExpr("Math.round(" + valueTimesShift.getText() + ") / " + shift.getText(), Operator.DIVIDE_BY.getPrecedence());
    } else if (numDigitsAfterPtAsInt < 0 && numDigitsAfterPtAsInt >= -12) {
        String shiftExprText = "1" + "000000000000".substring(0, -numDigitsAfterPtAsInt);
        JsExpr shift = new JsExpr(shiftExprText, Integer.MAX_VALUE);
        JsExpr valueDivideByShift = SoyJsPluginUtils.genJsExprUsingSoySyntax(Operator.DIVIDE_BY, Lists.newArrayList(value, shift));
        return new JsExpr("Math.round(" + valueDivideByShift.getText() + ") * " + shift.getText(), Operator.TIMES.getPrecedence());
    } else {
        throw new IllegalArgumentException("Second argument to round() function is " + numDigitsAfterPtAsInt + ", which is too large in magnitude.");
    }
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr)

Example 29 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class StrSubFunction method computeForJsSrc.

@Override
public JsExpr computeForJsSrc(List<JsExpr> args) {
    // Coerce SanitizedContent args to strings.
    String arg0 = JsExprUtils.toString(args.get(0)).getText();
    JsExpr arg1 = args.get(1);
    JsExpr arg2 = args.size() == 3 ? args.get(2) : null;
    return new JsExpr("(" + arg0 + ").substring(" + arg1.getText() + (arg2 != null ? "," + arg2.getText() : "") + ")", Integer.MAX_VALUE);
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr)

Example 30 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class IsNullFunction method computeForJsSrc.

@Override
public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg = args.get(0);
    JsExpr nullJsExpr = new JsExpr("null", Integer.MAX_VALUE);
    return SoyJsPluginUtils.genJsExprUsingSoySyntax(Operator.EQUAL, ImmutableList.of(arg, nullJsExpr));
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr)

Aggregations

JsExpr (com.google.template.soy.jssrc.restricted.JsExpr)64 Test (org.junit.Test)42 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)3 ImmutableList (com.google.common.collect.ImmutableList)2 RequiresCollector (com.google.template.soy.jssrc.dsl.CodeChunk.RequiresCollector)2 SoyLibraryAssistedJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 SanitizedContent (com.google.template.soy.data.SanitizedContent)1 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)1 SoyValue (com.google.template.soy.data.SoyValue)1 UnsafeSanitizedContentOrdainer (com.google.template.soy.data.UnsafeSanitizedContentOrdainer)1 SoyString (com.google.template.soy.data.restricted.SoyString)1 StringData (com.google.template.soy.data.restricted.StringData)1 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)1 StringNode (com.google.template.soy.exprtree.StringNode)1 WithValue (com.google.template.soy.jssrc.dsl.CodeChunk.WithValue)1 SoyJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective)1