Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
green-nick committed Nov 12, 2019
2 parents 1fcd3ef + a173689 commit 49d919f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ val property = propertyOf("Hello")
val length: MutableProperty<Int> = property.map { it.length } // will contain 5
```
Also notice that mapped value will be triggered on all updates of origin one.

Besides that, there is mapper for nullable properties:
```
val origin = propertyOf<String?>(null)
val length: MutableProperty<Int> = property.mapNotNull(0) { it.length } // will contain 0
origin.value = "hello"
length.value == 5
```
If init value of origin property is `null`, default value will be used for initialization.
All non-null set values will be mapped.
#### Addition:
You can add two different properties and get new one and receive all updates pushed to origins as Pair of their values:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ fun <T, R> Property<T>.map(mapper: (T) -> R): MutableProperty<R> {
return new
}

fun <T, R> Property<T?>.mapNotNull(default: R, mapper: (T) -> R): MutableProperty<R> {
val init = this.value
val new = if (init == null) {
propertyOf(default)
} else {
propertyOf(mapper(init))
}

this.subscribe { new.value = mapper(it ?: return@subscribe) }

return new
}

fun <T1, T2, E> Property<T1>.zipWith(another: Property<T2>, zipper: (T1, T2) -> E): MutableProperty<E> {
val new = propertyOf(zipper(this.value, another.value))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,52 @@ class PropertiesMappingTest {

assert(mapped.value == newValue.length)
}

@Test
fun `property got default instead of mapped null value`() {
val default = 2

val origin = propertyOf<String?>(null)
val mapped = origin.mapNotNull(default) { it.length }

assert(mapped.value == default)
}

@Test
fun `property got nonnull mapped value`() {
val default = 0
val initValue = "hello"

val origin = propertyOf<String?>(initValue)
val mapped = origin.mapNotNull(default) { it.length }

assert(mapped.value == initValue.length)
assert(mapped.value != default)
}

@Test
fun `mapped property receive updated nonnull value`() {
val default = 0
val origin = propertyOf<String?>("hello")

val mapped = origin.mapNotNull(default) { it.length }

val newValue = "worlds!!!"
origin.value = newValue

assert(mapped.value == newValue.length)
}

@Test
fun `mapped property doesn't receive updated nullable value`() {
val default = 0
val init = "hello"
val origin = propertyOf<String?>(init)

val mapped = origin.mapNotNull(default) { it.length }

origin.value = null

assert(mapped.value == init.length)
}
}

0 comments on commit 49d919f

Please sign in to comment.