From 33ff252f17dfb515155d46dd9bc959fbe38f79e8 Mon Sep 17 00:00:00 2001 From: lzgabel Date: Mon, 11 Nov 2024 22:12:31 +0800 Subject: [PATCH] feat(builtin): add partition function --- .../feel/impl/builtin/ListBuiltinFunctions.scala | 15 ++++++++++++++- .../impl/builtin/BuiltinListFunctionsTest.scala | 10 ++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/scala/org/camunda/feel/impl/builtin/ListBuiltinFunctions.scala b/src/main/scala/org/camunda/feel/impl/builtin/ListBuiltinFunctions.scala index dea911e08..2c2527a47 100644 --- a/src/main/scala/org/camunda/feel/impl/builtin/ListBuiltinFunctions.scala +++ b/src/main/scala/org/camunda/feel/impl/builtin/ListBuiltinFunctions.scala @@ -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 = @@ -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 { + ValNull + } + } + ) + } diff --git a/src/test/scala/org/camunda/feel/impl/builtin/BuiltinListFunctionsTest.scala b/src/test/scala/org/camunda/feel/impl/builtin/BuiltinListFunctionsTest.scala index a2a80c0b7..70fe51522 100644 --- a/src/test/scala/org/camunda/feel/impl/builtin/BuiltinListFunctionsTest.scala +++ b/src/test/scala/org/camunda/feel/impl/builtin/BuiltinListFunctionsTest.scala @@ -570,4 +570,14 @@ 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([], 2) ") should returnResult(List()) + evaluateExpression(" partition([1], 2) ") should returnResult(List(List(1))) + evaluateExpression(" partition([1,2], 0) ") should returnNull() + evaluateExpression(" partition([1,2], -1) ") should returnNull() + } }