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);
}
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);
}
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.");
}
}
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);
}
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));
}
Aggregations