Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [Unreleased]

### Added

- Add `Flow<Boolean>.not()` operator - Returns a Flow that emits the boolean negation of each value emitted by the source Flow.
- Add `Flow<Boolean>.inverted()` operator - Alias for `not()`.
- Add `Flow<Boolean>.toggle()` operator - Alias for `not()`.

## [1.0.0] - Sep 22, 2024

This is our first stable release! Thanks everyone for using FlowExt, reporting bugs, providing feedback and sending PRs.
Expand Down
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ dependencies {

- Intermediate operators
- [`bufferCount`](#buffercount--chunked)
- [`not`](#not--inverted--toggle) (`Flow<Boolean>`)
- [`inverted`](#not--inverted--toggle) (`Flow<Boolean>`)
- [`toggle`](#not--inverted--toggle) (`Flow<Boolean>`)
- [`combine`](#combine)
- [`cast`](#cast--castnotnull--castnullable--safeCast)
- [`castNotNull`](#cast--castnotnull--castnullable--safeCast)
Expand Down Expand Up @@ -228,6 +231,53 @@ bufferCount: [8, 9]

----

#### not / inverted / toggle

Returns a `Flow<Boolean>` that emits the boolean negation (opposite) of each value emitted by the source `Flow<Boolean>`.

- `not()` - Returns the negated boolean values
- `inverted()` - Alias for `not()`
- `toggle()` - Alias for `not()`

```kotlin
flowOf(true, false, true, false)
.not()
.collect { println("not: $it") }

println("---")

flowOf(true, false, true, false)
.inverted()
.collect { println("inverted: $it") }

println("---")

flowOf(true, false, true, false)
.toggle()
.collect { println("toggle: $it") }
```

Output:

```none
not: false
not: true
not: false
not: true
---
inverted: false
inverted: true
inverted: false
inverted: true
---
toggle: false
toggle: true
toggle: false
toggle: true
```

----

#### concat

- Similar to [RxJS concat](https://rxjs.dev/api/index/function/concat)
Expand Down
6 changes: 6 additions & 0 deletions api/FlowExt.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
public final class com/hoc081098/flowext/BooleanOperatorsKt {
public static final fun inverted (Lkotlinx/coroutines/flow/Flow;)Lkotlinx/coroutines/flow/Flow;
public static final fun not (Lkotlinx/coroutines/flow/Flow;)Lkotlinx/coroutines/flow/Flow;
public static final fun toggle (Lkotlinx/coroutines/flow/Flow;)Lkotlinx/coroutines/flow/Flow;
}

public final class com/hoc081098/flowext/BufferCountKt {
public static final fun bufferCount (Lkotlinx/coroutines/flow/Flow;I)Lkotlinx/coroutines/flow/Flow;
public static final fun bufferCount (Lkotlinx/coroutines/flow/Flow;II)Lkotlinx/coroutines/flow/Flow;
Expand Down
54 changes: 54 additions & 0 deletions src/commonMain/kotlin/com/hoc081098/flowext/booleanOperators.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* MIT License
*
* Copyright (c) 2021-2024 Petrus Nguyễn Thái Học
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.hoc081098.flowext

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/**
* Returns a [Flow] that emits the boolean negation (opposite) of each value emitted by the source [Flow].
*
* @return A [Flow] that emits `false` when the source emits `true` and `true` when the source emits `false`.
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun Flow<Boolean>.not(): Flow<Boolean> = map { !it }

/**
* Returns a [Flow] that emits the boolean negation (opposite) of each value emitted by the source [Flow].
* This is an alias for [not].
*
* @return A [Flow] that emits `false` when the source emits `true` and `true` when the source emits `false`.
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun Flow<Boolean>.inverted(): Flow<Boolean> = not()

/**
* Returns a [Flow] that emits the boolean negation (opposite) of each value emitted by the source [Flow].
* This is an alias for [not].
*
* @return A [Flow] that emits `false` when the source emits `true` and `true` when the source emits `false`.
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun Flow<Boolean>.toggle(): Flow<Boolean> = not()
146 changes: 146 additions & 0 deletions src/commonTest/kotlin/com/hoc081098/flowext/BooleanOperatorsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* MIT License
*
* Copyright (c) 2021-2024 Petrus Nguyễn Thái Học
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.hoc081098.flowext

import com.hoc081098.flowext.utils.BaseTest
import com.hoc081098.flowext.utils.TestException
import com.hoc081098.flowext.utils.test
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.toList

@ExperimentalCoroutinesApi
@InternalCoroutinesApi
class BooleanOperatorsTest : BaseTest() {
@Test
fun notBasic() = runTest {
val values = flowOf(true, false, true, false).not().toList()
assertEquals(listOf(false, true, false, true), values)
}

@Test
fun notEmpty() = runTest {
val values = flowOf<Boolean>().not().toList()
assertEquals(emptyList(), values)
}

@Test
fun notSingleTrue() = runTest {
val values = flowOf(true).not().toList()
assertEquals(listOf(false), values)
}

@Test
fun notSingleFalse() = runTest {
val values = flowOf(false).not().toList()
assertEquals(listOf(true), values)
}

@Test
fun notUpstreamError() = runTest {
val throwable = TestException()

flow<Boolean> { throw throwable }
.not()
.test(listOf(Event.Error(throwable)))
}

@Test
fun notSequence() = runTest {
listOf(true, false, true, false, true)
.asFlow()
.not()
.test(
listOf(
Event.Value(false),
Event.Value(true),
Event.Value(false),
Event.Value(true),
Event.Value(false),
Event.Complete,
),
)
}

@Test
fun invertedBasic() = runTest {
val values = flowOf(true, false, true, false).inverted().toList()
assertEquals(listOf(false, true, false, true), values)
}

@Test
fun invertedEmpty() = runTest {
val values = flowOf<Boolean>().inverted().toList()
assertEquals(emptyList(), values)
}

@Test
fun invertedUpstreamError() = runTest {
val throwable = TestException()

flow<Boolean> { throw throwable }
.inverted()
.test(listOf(Event.Error(throwable)))
}

@Test
fun toggleBasic() = runTest {
val values = flowOf(true, false, true, false).toggle().toList()
assertEquals(listOf(false, true, false, true), values)
}

@Test
fun toggleEmpty() = runTest {
val values = flowOf<Boolean>().toggle().toList()
assertEquals(emptyList(), values)
}

@Test
fun toggleUpstreamError() = runTest {
val throwable = TestException()

flow<Boolean> { throw throwable }
.toggle()
.test(listOf(Event.Error(throwable)))
}

@Test
fun allVariantsProduceSameResult() = runTest {
val input = flowOf(true, false, true, false)

val notResult = input.not().toList()
val invertedResult = input.inverted().toList()
val toggleResult = input.toggle().toList()

assertEquals(notResult, invertedResult)
assertEquals(notResult, toggleResult)
assertEquals(listOf(false, true, false, true), notResult)
}
}