Skip to content
Merged
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 @@ -69,7 +69,8 @@ class ListBuiltinFunctions(private val valueMapper: ValueMapper) {
joinWithDelimiterFunction,
joinWithDelimiterAndPrefixAndSuffixFunction
),
"is empty" -> List(emptyFunction)
"is empty" -> List(emptyFunction),
"partition" -> List(partitionFunction)
)

private def listContainsFunction =
Expand Down Expand Up @@ -518,4 +519,16 @@ class ListBuiltinFunctions(private val valueMapper: ValueMapper) {
}
)

private def partitionFunction =
builtinFunction(
params = List("list", "size"),
invoke = { case List(ValList(list), ValNumber(size)) =>
if (size.intValue > 0) {
ValList(list.grouped(size.intValue).map(l => ValList(l)).toList)
} else {
ValError(s"'size' should be greater than zero but was '$size'")
}
}
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.camunda.feel.impl.builtin
import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpec
import org.camunda.feel._
import org.camunda.feel.api.EvaluationFailureType.FUNCTION_INVOCATION_FAILURE
import org.camunda.feel.impl.{EvaluationResultMatchers, FeelEngineTest, FeelIntegrationTest}
import org.camunda.feel.syntaxtree._

Expand Down Expand Up @@ -570,4 +571,32 @@ class BuiltinListFunctionsTest
evaluateExpression(" is empty([1,2,3]) ") should returnResult(false)
evaluateExpression(" is empty(list: [1]) ") should returnResult(false)
}

"A partition() function" should "return list partitioned by _" in {
evaluateExpression(" partition([1,2,3,4,5], 2) ") should returnResult(
List(List(1, 2), List(3, 4), List(5))
)
evaluateExpression(" partition(list: [1,2,3,4,5], size: 2) ") should returnResult(
List(List(1, 2), List(3, 4), List(5))
)

evaluateExpression(" partition([], 2) ") should returnResult(List())
evaluateExpression(" partition([1], 2) ") should returnResult(List(List(1)))
}

it should "return null if the size is invalid" in {
evaluateExpression(" partition([1,2], 0) ") should (
returnNull() and reportFailure(
FUNCTION_INVOCATION_FAILURE,
"Failed to invoke function 'partition': 'size' should be greater than zero but was '0'"
)
)

evaluateExpression(" partition([1,2], -1) ") should (
returnNull() and reportFailure(
FUNCTION_INVOCATION_FAILURE,
"Failed to invoke function 'partition': 'size' should be greater than zero but was '-1'"
)
)
}
}
Loading