Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions documentation/reference/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ function does not remove duplicate records or protobufs. Only primitive types
(null, undefined, bool, int, float, number, string) are successfully
deduplicated.

<span id="list-string|float|int|gbigint_join"></span>
<span id="list-string|number|gbigint_join"></span>

### `list.join(separator)` {#join}

Joins a list of strings or numbers with a string separator.

Also callable as deprecated global function: `join(list, separator)`

<span id="list-float|int_sort"></span>
<span id="list-number_sort"></span>
<span id="list-gbigint_sort"></span>

### `list<any>.toSorted(comparator)` {#list-any_toSorted}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,8 @@ public static boolean veHasSameId(SoyVisualElement ve1, SoyVisualElement ve2) {
* Returns the smallest (closest to negative infinity) integer value that is greater than or equal
* to the argument.
*/
public static long ceil(SoyValue arg) {
if (arg instanceof IntegerData) {
return arg.longValue();
} else {
return (long) Math.ceil(arg.floatValue());
}
public static double ceil(SoyValue arg) {
return Math.ceil(arg.numberValue());
}

/** Concatenates its arguments. */
Expand Down Expand Up @@ -132,7 +128,7 @@ public static boolean listContains(List<? extends SoyValueProvider> list, SoyVal
}

/** Checks if list contains a value. */
public static int listIndexOf(
public static double listIndexOf(
List<? extends SoyValueProvider> list, SoyValue value, NumberData startIndex) {
int clampedStartIndex = clampListIndex(list, startIndex);
if (clampedStartIndex >= list.size()) {
Expand Down Expand Up @@ -220,8 +216,8 @@ public static ImmutableList<? extends SoyValueProvider> listFlat(

@Nonnull
public static ImmutableList<? extends SoyValueProvider> listFlat(
List<? extends SoyValueProvider> list, IntegerData data) {
return listFlatImpl(list, (int) data.getValue());
List<? extends SoyValueProvider> list, NumberData data) {
return listFlatImpl(list, data.coerceToInt());
}

@Nonnull
Expand Down Expand Up @@ -285,12 +281,8 @@ public static ImmutableList<SoyValueProvider> stringListSort(
* Returns the largest (closest to positive infinity) integer value that is less than or equal to
* the argument.
*/
public static long floor(SoyValue arg) {
if (arg instanceof IntegerData) {
return arg.longValue();
} else {
return (long) Math.floor(arg.floatValue());
}
public static double floor(SoyValue arg) {
return Math.floor(arg.numberValue());
}

/**
Expand Down Expand Up @@ -364,48 +356,43 @@ public static FloatData parseFloat(String str) {
}

@Nullable
public static IntegerData parseInt(String str, SoyValue radixVal) {
public static NumberData parseInt(String str, SoyValue radixVal) {
int radix = SoyValue.isNullish(radixVal) ? 10 : (int) radixVal.numberValue();
if (radix < 2 || radix > 36) {
return null;
}
Long l = Longs.tryParse(str, radix);
return (l == null) ? null : IntegerData.forValue(l);
return (l == null) ? null : FloatData.forValue(l);
}

/** Returns a random integer between {@code 0} and the provided argument. */
public static long randomInt(double number) {
return (long) Math.floor(Math.random() * number);
public static double randomInt(double number) {
return Math.floor(Math.random() * number);
}

/**
* Rounds the given value to the closest decimal point left (negative numbers) or right (positive
* numbers) of the decimal point
*/
@Nonnull
public static NumberData round(SoyValue value, int numDigitsAfterPoint) {
public static double round(SoyValue value, double numDigitsAfterPoint) {
// NOTE: for more accurate rounding, this should really be using BigDecimal which can do correct
// decimal arithmetic. However, for compatibility with js, that probably isn't an option.
if (numDigitsAfterPoint == 0) {
return IntegerData.forValue(round(value));
return Math.round(value.numberValue());
} else if (numDigitsAfterPoint > 0) {
double valueDouble = value.numberValue();
double shift = Math.pow(10, numDigitsAfterPoint);
return FloatData.forValue(Math.round(valueDouble * shift) / shift);
return Math.round(valueDouble * shift) / shift;
} else {
double valueDouble = value.numberValue();
double shift = Math.pow(10, -numDigitsAfterPoint);
return IntegerData.forValue((int) (Math.round(valueDouble / shift) * shift));
return Math.round(valueDouble / shift) * shift;
}
}

/** Rounds the given value to the closest integer. */
public static long round(SoyValue value) {
if (value instanceof IntegerData) {
return value.longValue();
} else {
return Math.round(value.numberValue());
}
public static double round(SoyValue value) {
return Math.round(value.numberValue());
}

@Nonnull
Expand Down Expand Up @@ -444,7 +431,7 @@ public static boolean strContains(SoyValue left, String right) {
return left.stringValue().contains(right);
}

public static int strIndexOf(SoyValue str, SoyValue searchStr, NumberData start) {
public static double strIndexOf(SoyValue str, SoyValue searchStr, NumberData start) {
// TODO(b/74259210) -- Change the params to String & avoid using stringValue().
// Add clamping behavior for start index to match js implementation
String strValue = str.stringValue();
Expand Down Expand Up @@ -522,7 +509,7 @@ public static String strTrim(String str) {
return str.trim();
}

public static int length(List<?> list) {
public static double length(List<?> list) {
return list.size();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
name = "ceiling",
value =
@Signature(
parameterTypes = {"float|int"},
returnType = "int"))
parameterTypes = {"number"},
returnType = "number"))
final class CeilingFunction
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
name = "floor",
value =
@Signature(
parameterTypes = {"float|int"},
returnType = "int"))
parameterTypes = {"number"},
returnType = "number"))
final class FloorFunction
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
name = "isFinite",
value =
@Signature(
parameterTypes = {"float|int"},
parameterTypes = {"number"},
returnType = "bool"))
final class IsFiniteFunction
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
name = "join",
value = {
@Signature(
parameterTypes = {"list<string|float|int|gbigint>", "string"},
parameterTypes = {"list<string|number|gbigint>", "string"},
returnType = "string"),
})
@SoyMethodSignature(
name = "join",
baseType = "list<string|float|int|gbigint>",
baseType = "list<string|number|gbigint>",
value = @Signature(parameterTypes = "string", returnType = "string"))
@SoyPureFunction
public final class JoinFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
value =
@Signature(
parameterTypes = {"list<any>"},
returnType = "int"))
@SoyFieldSignature(name = "length", baseType = "list<any>", returnType = "int")
returnType = "number"))
@SoyFieldSignature(name = "length", baseType = "list<any>", returnType = "number")
public final class LengthFunction extends TypedSoyFunction
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.template.soy.basicfunctions;

import com.google.template.soy.data.restricted.IntegerData;
import com.google.template.soy.data.restricted.NumberData;
import com.google.template.soy.plugin.java.restricted.JavaPluginContext;
import com.google.template.soy.plugin.java.restricted.JavaValue;
import com.google.template.soy.plugin.java.restricted.JavaValueFactory;
Expand Down Expand Up @@ -46,7 +46,7 @@
value = {
// returnType is made intelligent in ResolveExpressionTypesPass
@Signature(returnType = "list<?>"),
@Signature(parameterTypes = "int", returnType = "list<?>")
@Signature(parameterTypes = "number", returnType = "list<?>")
})
@SoyPureFunction
public class ListFlatMethod
Expand Down Expand Up @@ -78,7 +78,7 @@ private static final class Methods {
JavaValueFactory.createMethod(BasicFunctionsRuntime.class, "listFlat", List.class);
static final Method LIST_FLAT_ARG1_FN =
JavaValueFactory.createMethod(
BasicFunctionsRuntime.class, "listFlat", List.class, IntegerData.class);
BasicFunctionsRuntime.class, "listFlat", List.class, NumberData.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
name = "indexOf",
baseType = "list<any>",
value = {
@Signature(parameterTypes = "any", returnType = "int"),
@Signature(parameterTypes = "any", returnType = "number"),
@Signature(
parameterTypes = {"any", "float|int"},
returnType = "int")
parameterTypes = {"any", "number"},
returnType = "number")
})
@SoyPureFunction
public class ListIndexOfFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
baseType = "list<any>",
value = {
@Signature(returnType = "list<any>"),
@Signature(parameterTypes = "float|int", returnType = "list<any>"),
@Signature(parameterTypes = "number", returnType = "list<any>"),
@Signature(
parameterTypes = {"float|int", "float|int"},
parameterTypes = {"number", "number"},
returnType = "list<any>")
})
@SoyPureFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.List;

/** Soy function that gets the number of keys in a map. */
@SoyFieldSignature(name = "size", baseType = "map<any, any>", returnType = "int")
@SoyFieldSignature(name = "size", baseType = "map<any, any>", returnType = "number")
@SoyPureFunction
public final class MapLengthMethod
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@
/** Soy method for sorting a list of numbers in numerical order. */
@SoyMethodSignature(
name = "sort",
baseType = "list<float|int>",
baseType = "list<number>",
value = {
@Signature(
parameterTypes = {},
// The generic type may be overwritten to a narrower int or float type by
// ResolveExpressionTypesPass.
returnType = "list<float|int>"),
returnType = "list<number>"),
})
@SoyPureFunction
public final class NumberListSortMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
value =
@Signature(
parameterTypes = {"string"},
// TODO(b/70946095): should be nullable
returnType = "float"))
returnType = "number"))
final class ParseFloatFunction
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@
@Signature(
parameterTypes = {"string"},
// TODO(b/70946095): should be nullable
returnType = "int"),
returnType = "number"),
@Signature(
parameterTypes = {"string", "int|float|undefined"},
returnType = "int|null")
parameterTypes = {"string", "number|undefined"},
returnType = "number|null")
})
final class ParseIntFunction
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
name = "pow",
value =
@Signature(
returnType = "float|int",
parameterTypes = {"float|int", "float|int"}))
returnType = "number",
parameterTypes = {"number", "number"}))
@SoyPureFunction
class PowFunction
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
/** Soy function that generates a random integer in the range [0, n-1]. */
@SoyFunctionSignature(
name = "randomInt",
// TODO(b/70946095): param should be an 'int', not a 'number'
value = @Signature(returnType = "int", parameterTypes = "float|int"))
value = @Signature(returnType = "number", parameterTypes = "number"))
public final class RandomIntFunction
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {

Expand All @@ -64,6 +63,6 @@ private static final class Methods {
@Override
public JavaValue applyForJavaSource(
JavaValueFactory factory, List<JavaValue> args, JavaPluginContext context) {
return factory.callStaticMethod(Methods.RANDOM_INT_FN, args.get(0).asSoyInt());
return factory.callStaticMethod(Methods.RANDOM_INT_FN, args.get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,16 @@
*/
@SoyFunctionSignature(
name = "range",
// TODO(b/70946095): params should be an 'int', not a 'number'
value = {
@Signature(
parameterTypes = {"float|int"},
returnType = "list<int>"),
parameterTypes = {"number"},
returnType = "list<number>"),
@Signature(
parameterTypes = {"float|int", "float|int"},
returnType = "list<int>"),
parameterTypes = {"number", "number"},
returnType = "list<number>"),
@Signature(
parameterTypes = {"float|int", "float|int", "float|int"},
returnType = "list<int>")
parameterTypes = {"number", "number", "number"},
returnType = "list<number>")
})
@SoyPureFunction
public final class RangeFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
name = "round",
value = {
// TODO(b/70946095): the parameters should be number values
@Signature(returnType = "int", parameterTypes = "?"),
@Signature(returnType = "number", parameterTypes = "?"),
@Signature(
returnType = "float|int",
parameterTypes = {"?", "?"}),
returnType = "number",
parameterTypes = {"?", "number"}),
})
@SoyPureFunction
final class RoundFunction
Expand Down Expand Up @@ -79,7 +79,7 @@ private static final class Methods {

static final Method BOXED_ROUND_WITH_NUM_DIGITS_AFTER_POINT_FN =
JavaValueFactory.createMethod(
BasicFunctionsRuntime.class, "round", SoyValue.class, int.class);
BasicFunctionsRuntime.class, "round", SoyValue.class, double.class);
}

@Override
Expand All @@ -89,7 +89,7 @@ public JavaValue applyForJavaSource(
return factory.callStaticMethod(Methods.BOXED_ROUND_FN, args.get(0));
} else {
return factory.callStaticMethod(
Methods.BOXED_ROUND_WITH_NUM_DIGITS_AFTER_POINT_FN, args.get(0), args.get(1).asSoyInt());
Methods.BOXED_ROUND_WITH_NUM_DIGITS_AFTER_POINT_FN, args.get(0), args.get(1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.List;

/** Soy field that gets the size of a set. */
@SoyFieldSignature(name = "size", baseType = "set<any>", returnType = "int")
@SoyFieldSignature(name = "size", baseType = "set<any>", returnType = "number")
@SoyPureFunction
public final class SetSizeField
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {
Expand Down
Loading