Implement slice expression (#1133)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bart Louwers <bart@emeel.net>
This commit is contained in:
Siarhei Fedartsou
2023-08-26 18:32:45 +02:00
committed by GitHub
parent ecd59d4cc4
commit 09f17c0fc4
15 changed files with 417 additions and 40 deletions

View File

@@ -1565,7 +1565,7 @@ public class Expression {
}
/**
* Returns the first position at which a `needle` can be found in a `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
@@ -1577,6 +1577,33 @@ public class Expression {
return new Expression("index-of", keyword, input, fromIndex);
}
/**
* Returns items from an array or a substring from a string from a specified start index.
* The return value is inclusive of the start index.
*
* @param input the array or string expression
* @param fromIndex the index to start slice from
* @return array or string
* @see <a href="https://maplibre.org/maplibre-style-spec/expressions/#slice">Style specification</a>
*/
public static Expression slice(@NonNull Expression input, @NonNull Expression fromIndex) {
return new Expression("slice", input, fromIndex);
}
/**
* Returns items from an array or a substring from a string between a start index and an end index if set.
* The return value is inclusive of the start index, but not of the end index.
*
* @param input the array or string expression
* @param fromIndex the index to start slice from
* @param toIndex the index to end slice at
* @return array or string
* @see <a href="https://maplibre.org/maplibre-style-spec/expressions/#slice">Style specification</a>
*/
public static Expression slice(@NonNull Expression input, @NonNull Expression fromIndex, @NonNull Expression toIndex) {
return new Expression("slice", input, fromIndex, toIndex);
}
/**
* Retrieves whether an item exists in an array or a substring exists in a string.
*

View File

@@ -477,6 +477,26 @@ class ExpressionTest {
Assert.assertTrue("expression should match", Arrays.deepEquals(expected, actual))
}
@Test
@Throws(Exception::class)
fun testSlice() {
val expected = arrayOf<Any>("slice", arrayOf<Any>("literal", arrayOf<Any>(1f, 2f, 3f)), 1f)
val actual =
Expression.slice(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 testSliceWithToIndex() {
val expected = arrayOf<Any>("slice", arrayOf<Any>("literal", arrayOf<Any>(1f, 2f, 3f)), 1f, 3f)
val actual =
Expression.slice(Expression.literal(arrayOf<Any>(1f, 2f, 3f)), Expression.literal(1f), Expression.literal(3f))
.toArray()
Assert.assertTrue("expression should match", Arrays.deepEquals(expected, actual))
}
@Test
@Throws(Exception::class)
fun testInNumber() {