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
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static ImmutableList<SoySourceFunction> functions() {
new ListFlatMethod(),
new ListIncludesFunction(),
new ListIndexOfFunction(),
new ListLastIndexOfFunction(),
new ListReverseMethod(),
new ListSliceMethod(),
new ListUniqMethod(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ public static int listIndexOf(
return indexInSubList == -1 ? -1 : indexInSubList + clampedStartIndex;
}

/** Returns the last index of value in list or -1. */
public static int listLastIndexOf(
List<? extends SoyValueProvider> list, SoyValue value, NumberData startIndex) {
int clampedStartIndex = clampListIndex(list, startIndex);
if (clampedStartIndex == list.size()) {
clampedStartIndex = list.size() - 1;
}
for (int i = clampedStartIndex; i >= 0; i--) {
if (list.get(i).resolve().equals(value)) {
return i;
}
}
return -1;
}

/** Joins the list elements by a separator. */
@Nonnull
public static String join(List<? extends SoyValueProvider> list, String separator) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2026 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.template.soy.basicfunctions;

import com.google.template.soy.data.SoyValue;
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;
import com.google.template.soy.plugin.java.restricted.SoyJavaSourceFunction;
import com.google.template.soy.plugin.javascript.restricted.JavaScriptPluginContext;
import com.google.template.soy.plugin.javascript.restricted.JavaScriptValue;
import com.google.template.soy.plugin.javascript.restricted.JavaScriptValueFactory;
import com.google.template.soy.plugin.javascript.restricted.SoyJavaScriptSourceFunction;
import com.google.template.soy.plugin.python.restricted.PythonPluginContext;
import com.google.template.soy.plugin.python.restricted.PythonValue;
import com.google.template.soy.plugin.python.restricted.PythonValueFactory;
import com.google.template.soy.plugin.python.restricted.SoyPythonSourceFunction;
import com.google.template.soy.shared.restricted.Signature;
import com.google.template.soy.shared.restricted.SoyMethodSignature;
import com.google.template.soy.shared.restricted.SoyPureFunction;
import java.lang.reflect.Method;
import java.util.List;

/**
* Soy function for checking the last index an item is contained in a list.
*
* <p>Usage: {@code list.lastIndexOf(item)}
*
* <ul>
* <li>list: The list in which to look for the item.
* <li>item: The item to search for in the list.
* </ul>
*/
@SoyMethodSignature(
name = "lastIndexOf",
baseType = "list<any>",
value = {
@Signature(parameterTypes = "any", returnType = "int"),
@Signature(
parameterTypes = {"any", "float|int"},
returnType = "int")
})
@SoyPureFunction
public class ListLastIndexOfFunction
implements SoyJavaSourceFunction, SoyJavaScriptSourceFunction, SoyPythonSourceFunction {

@Override
public JavaScriptValue applyForJavaScriptSource(
JavaScriptValueFactory factory, List<JavaScriptValue> args, JavaScriptPluginContext context) {
return factory.callNamespaceFunction("soy", "soy.$$listLastIndexOf", args);
}

@Override
public PythonValue applyForPythonSource(
PythonValueFactory factory, List<PythonValue> args, PythonPluginContext context) {
return factory.global("runtime.list_lastindexof").call(args);
}

// lazy singleton pattern, allows other backends to avoid the work.
private static final class Methods {
static final Method LIST_CONTAINS_FN =
JavaValueFactory.createMethod(
BasicFunctionsRuntime.class,
"listLastIndexOf",
List.class,
SoyValue.class,
NumberData.class);
}

@Override
public JavaValue applyForJavaSource(
JavaValueFactory factory, List<JavaValue> args, JavaPluginContext context) {
return factory.callStaticMethod(
Methods.LIST_CONTAINS_FN,
args.get(0),
args.get(1),
args.size() == 3 ? args.get(2) : factory.constant(Integer.MAX_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import com.google.template.soy.basicfunctions.ListFlatMethod;
import com.google.template.soy.basicfunctions.ListIncludesFunction;
import com.google.template.soy.basicfunctions.ListIndexOfFunction;
import com.google.template.soy.basicfunctions.ListLastIndexOfFunction;
import com.google.template.soy.basicfunctions.ListReverseMethod;
import com.google.template.soy.basicfunctions.ListSliceMethod;
import com.google.template.soy.basicfunctions.ListUniqMethod;
Expand Down Expand Up @@ -2165,7 +2166,8 @@ private void finishMethodCallNode(MethodCallNode node, boolean nullSafe) {
node.setType(returnType);
}
} else if (sourceFunction instanceof ListIncludesFunction
|| sourceFunction instanceof ListIndexOfFunction) {
|| sourceFunction instanceof ListIndexOfFunction
|| sourceFunction instanceof ListLastIndexOfFunction) {
node.setType(sourceMethod.getReturnType());
if (node.getParam(0) instanceof RecordLiteralNode) {
errorReporter.report(node.getParam(0).getSourceLocation(), RECORD_LITERAL_NOT_ALLOWED);
Expand Down
12 changes: 12 additions & 0 deletions javascript/soyutils_usegoog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,17 @@ const $$listIndexOf = function(list, val, startIndex = 0) {
return indexInSublist === -1 ? -1 : indexInSublist + clampedStartIndex;
};

/**
* Returns the last index of val in list or -1
* @param {!ReadonlyArray<?>} list
* @param {*} val
* @param {number=} startIndex
* @return {number}
*/
const $$listLastIndexOf = function(list, val, startIndex = list.length - 1) {
return list.lastIndexOf(val, startIndex);
};

/**
* Reverses a list and returns it. The original list passed is unaffected.
* @param {!ReadonlyArray<T>} list
Expand Down Expand Up @@ -3104,6 +3115,7 @@ exports = {
$$truncate,
$$listContains,
$$listIndexOf,
$$listLastIndexOf,
$$listReverse,
$$listUniq,
$$listFlat,
Expand Down
11 changes: 11 additions & 0 deletions python/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,17 @@ def list_indexof(l, item, start_index=0):
return -1


def list_lastindexof(l, item, start_index=None):
"""Equivalent getting the last index of `item in l` but using soy's equality algorithm."""
if start_index is None or start_index >= len(l):
start_index = len(l) - 1
clamped_start_index = clamp_list_start_index(l, start_index)
for i in range(clamped_start_index, -1, -1):
if strict_eq(l[i], item):
return i
return -1


def strict_eq(first, second):
"""An equality function that handles JS primitive types as primitives."""
if (
Expand Down