Implement index-of expression (#1113)

This commit is contained in:
Siarhei Fedartsou
2023-05-09 00:16:58 +02:00
committed by GitHub
parent d716636d83
commit 379b1fc606
15 changed files with 458 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ MapLibre welcomes participation and contributions from everyone. Please read [`C
### ✨ Features and improvements
* Change to a more natural fling animation and allow setting `flingThreshold` and `flingAnimationBaseTime` in `UiSettings` ([#963](https://github.com/maplibre/maplibre-native/pull/963))
* Add support for [index-of expression](https://maplibre.org/maplibre-style-spec/expressions/#index-of) ([#1113](https://github.com/maplibre/maplibre-native/pull/1113))
### 🐞 Bug fixes

View File

@@ -1552,6 +1552,31 @@ public class Expression {
return new Expression("in", needle, haystack);
}
/**
* Returns the first position at which a `needle` can be found in a `haystack`.
*
* @param needle the item expression
* @param haystack the array or string expression
* @return position in the array or string or -1 if not found.
* @see <a href="https://maplibre.org/maplibre-gl-js-docs/style-spec/#index-of">Style specification</a>
*/
public static Expression indexOf(@NonNull Expression keyword, @NonNull Expression input) {
return new Expression("index-of", keyword, input);
}
/**
* Returns the first position at which a `needle` can be found in a `haystack`.
*
* @param needle the item expression
* @param haystack the array or string expression
* @param fromIndex the index to start searching from
* @return position in the array or string or -1 if not found.
* @see <a href="https://maplibre.org/maplibre-gl-js-docs/style-spec/#index-of">Style specification</a>
*/
public static Expression indexOf(@NonNull Expression keyword, @NonNull Expression input, @NonNull Expression fromIndex) {
return new Expression("index-of", keyword, input, fromIndex);
}
/**
* Retrieves whether an item exists in an array or a substring exists in a string.
*

View File

@@ -455,6 +455,27 @@ class ExpressionTest {
Assert.assertTrue("expression should match", Arrays.deepEquals(expected, actual))
}
@Test
@Throws(Exception::class)
fun testIndexOf() {
val expected = arrayOf<Any>("index-of", 2f, arrayOf<Any>("literal", arrayOf<Any>(1f, 2f, 3f)))
val actual =
Expression.indexOf(Expression.literal(2f), Expression.literal(arrayOf<Any>(1f, 2f, 3f)))
.toArray()
Assert.assertTrue("expression should match", Arrays.deepEquals(expected, actual))
}
@Test
@Throws(Exception::class)
fun testIndexOfWithFromIndex() {
val expected = arrayOf<Any>("index-of", 2f, arrayOf<Any>("literal", arrayOf<Any>(1f, 2f, 3f)), 1f)
val actual =
Expression.indexOf(Expression.literal(2f), Expression.literal(arrayOf<Any>(1f, 2f, 3f)), Expression.literal(1f))
.toArray()
Assert.assertTrue("expression should match", Arrays.deepEquals(expected, actual))
}
@Test
@Throws(Exception::class)
fun testInNumber() {