Skip to content

Commit

Permalink
add count with predicate to family
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven committed Dec 22, 2024
1 parent deb414e commit c694660
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/commonMain/kotlin/com/github/quillraven/fleks/family.kt
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ data class Family(
*/
fun none(predicate: (Entity) -> Boolean): Boolean = mutableEntities.none(predicate)

/**
* Returns the number of [entities][Entity] matching the given [predicate].
*/
fun count(predicate: (Entity) -> Boolean): Int = mutableEntities.count(predicate)

/**
* Returns a [Map] containing key-value pairs provided by the [transform] function applied to
* each [entity][Entity] of the family.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class FamilyBagFunctionsTest {
assertFalse(testFamily.none { it.id == 0 })
}

@Test
fun testCount() {
assertEquals(1, testFamily.count { it.id == 0 })
}

@Test
fun testAssociate() {
val expected = mapOf(testEntity1 to 0, testEntity2 to 1)
Expand Down

0 comments on commit c694660

Please sign in to comment.