Skip to content

Commit

Permalink
feat(dsl): add Field.contains to enable the in operator
Browse files Browse the repository at this point in the history
  • Loading branch information
DanySK committed Jan 31, 2025
1 parent e37116c commit 8b4ea41
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@ package it.unibo.collektive.field.operations

import it.unibo.collektive.field.Field
import it.unibo.collektive.field.Field.Companion.fold
import it.unibo.collektive.field.Field.Companion.foldWithId
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads

/**
* Check if the field contains the [value], **including the local value**.
* If you need to exclude the local value, use instead:
*
* ```kotlin
* value in field.withoutSelf().values
* ```
*/
operator fun <ID : Any, T> Field<ID, T>.contains(value: T): Boolean = anyWithSelf { it == value }

Check warning on line 19 in dsl/src/commonMain/kotlin/it/unibo/collektive/field/operations/Fields.kt

View check run for this annotation

Codecov / codecov/patch

dsl/src/commonMain/kotlin/it/unibo/collektive/field/operations/Fields.kt#L19

Added line #L19 was not covered by tests

/**
* Check if the field contains the [id], **including the local id**.
* If you need to exclude the local value, use instead:
*
* ```kotlin
* id in field.withoutSelf().keys
* ```
*/
@JvmName("containsId")
operator fun <ID : Any, T> Field<ID, T>.contains(id: ID): Boolean =
foldWithId(localValue == id) { current, id, _ -> current || id == id }

/**
* Count the number of elements in the field that satisfy the [predicate],
* ignoring the local value.
Expand Down

0 comments on commit 8b4ea41

Please sign in to comment.