Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new built in function to find duplicates #688

Merged
merged 5 commits into from
Jul 17, 2023
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 @@ -464,6 +464,25 @@ distinct values([1,2,3,2,1])
// [1,2,3]
```

## duplicate values(list)

<MarkerCamundaExtension></MarkerCamundaExtension>

Returns all duplicate values of the given list.

**Function signature**

```js
duplicate values(list: list): list
```

**Examples**

```js
duplicate values([1,2,3,2,1])
// [1,2]
```

## flatten(list)

Returns a list that includes all elements of the given list without nested lists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ object ListBuiltinFunctions {
"index of" -> List(indexOfFunction),
"union" -> List(unionFunction),
"distinct values" -> List(distinctValuesFunction),
"duplicate values" -> List(duplicateValuesFunction),
"flatten" -> List(flattenFunction),
"sort" -> List(sortFunction),
"string join" -> List(joinFunction,
Expand Down Expand Up @@ -347,6 +348,11 @@ object ListBuiltinFunctions {
case List(ValList(list)) => ValList(list.distinct)
})

private def duplicateValuesFunction =
builtinFunction(params = List("list"), invoke = {
case List(ValList(list)) => ValList(list.distinct.filter(x => list.count(_ == x) > 1))
})

private def flattenFunction =
builtinFunction(params = List("list"), invoke = {
case List(ValList(list)) => ValList(flatten(list))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,24 @@ class BuiltinListFunctionsTest
ValList(List(ValNumber(1), ValNumber(2), ValNumber(3))))
}

"A duplicate values() function" should "return duplicate values" in {

eval(" duplicate values([1,2,3,2,1]) ") should be(
ValList(List(ValNumber(1), ValNumber(2))))
}

it should "return null duplicate values" in {

eval(" duplicate values([1,2,1,null,null]) ") should be(
ValList(List(ValNumber(1), ValNull)))
}

it should "return duplicate values for named parameters" in {

eval(" duplicate values(list: [1,2,3,2,1]) ") should be(
ValList(List(ValNumber(1), ValNumber(2))))
}

"A flatten() function" should "flatten nested lists" in {

eval(" flatten([[1,2],[[3]], 4]) ") should be(
Expand Down