Skip to content

Commit f6ae8a7

Browse files
Add isNotNullNorBlank() and isNotNullNorEmpty()
1 parent c216500 commit f6ae8a7

File tree

7 files changed

+102
-5
lines changed

7 files changed

+102
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

55
### Added
6+
- `isNotNullNorBlank()` and `isNotNullNorEmpty()`
67
- `second()`, `third()`, `forth()`, `fifth()` and `penultimate()`
78

89
### Changed

kotlin-stdlib/api/kotlin-stdlib.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ public final class com/javiersc/kotlin/stdlib/CollectionsKt {
152152
}
153153

154154
public final class com/javiersc/kotlin/stdlib/StringsKt {
155+
public static final fun isNotNullNorBlank (Ljava/lang/CharSequence;)Z
156+
public static final fun isNotNullNorEmpty (Ljava/lang/CharSequence;)Z
155157
public static final fun remove (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/String;
156158
public static synthetic fun remove$default (Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Ljava/lang/String;
157159
}

kotlin-stdlib/commonMain/kotlin/AnsiColors.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.javiersc.kotlin.stdlib
22

3-
public fun String.ansiColor(color: AnsiColor): String = "${color}$this${AnsiColor.Reset}"
3+
public inline fun String.ansiColor(color: AnsiColor): String = "${color}$this${AnsiColor.Reset}"
44

55
public abstract class AnsiColor {
66

kotlin-stdlib/commonMain/kotlin/Collections.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,39 @@
22

33
package com.javiersc.kotlin.stdlib
44

5+
/**
6+
* Returns second element.
7+
*
8+
* Throws: `NoSuchElementException` - if the list size is 1.
9+
*/
510
public inline fun <T> Iterable<T>.second(): T = getIndex(2)
611

12+
/**
13+
* Returns third element.
14+
*
15+
* Throws: `NoSuchElementException` - if the list size is 2.
16+
*/
717
public inline fun <T> Iterable<T>.third(): T = getIndex(3)
818

19+
/**
20+
* Returns forth element.
21+
*
22+
* Throws: `NoSuchElementException` - if the list size is 3.
23+
*/
924
public inline fun <T> Iterable<T>.forth(): T = getIndex(4)
1025

26+
/**
27+
* Returns fifth element.
28+
*
29+
* Throws: `NoSuchElementException` - if the list size is 4.
30+
*/
1131
public inline fun <T> Iterable<T>.fifth(): T = getIndex(5)
1232

33+
/**
34+
* Returns penultimate element.
35+
*
36+
* Throws: `NoSuchElementException` - if the list size is 1.
37+
*/
1338
public inline fun <T> Iterable<T>.penultimate(): T {
1439
return when (this) {
1540
is List -> this[size - 2]
@@ -30,7 +55,8 @@ public inline fun <T> Iterable<T>.penultimate(): T {
3055
}
3156
}
3257

33-
public inline fun <T> Iterable<T>.getIndex(index: Int): T {
58+
@PublishedApi
59+
internal inline fun <T> Iterable<T>.getIndex(index: Int): T {
3460
return when (this) {
3561
is List -> this[index - 1]
3662
else -> {
@@ -48,5 +74,5 @@ public inline fun <T> Iterable<T>.getIndex(index: Int): T {
4874
}
4975

5076
@PublishedApi
51-
internal fun throwNoSuchElementException(size: Int): Unit =
77+
internal inline fun throwNoSuchElementException(size: Int): Unit =
5278
throw NoSuchElementException("Collection size is lower than $size.")
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
package com.javiersc.kotlin.stdlib
22

3+
import kotlin.contracts.ExperimentalContracts
4+
import kotlin.contracts.contract
5+
36
/**
47
* Returns a new string obtained by removing all occurrences of the [value] substring in this string
58
*/
6-
public fun String.remove(value: String, ignoreCase: Boolean = false): String =
9+
public inline fun String.remove(value: String, ignoreCase: Boolean = false): String =
710
replace(oldValue = value, newValue = "", ignoreCase = ignoreCase)
11+
12+
@OptIn(ExperimentalContracts::class)
13+
public inline fun CharSequence?.isNotNullNorBlank(): Boolean {
14+
contract { returns(false) implies (this@isNotNullNorBlank == null) }
15+
16+
return this != null && isNotBlank()
17+
}
18+
19+
@OptIn(ExperimentalContracts::class)
20+
public inline fun CharSequence?.isNotNullNorEmpty(): Boolean {
21+
contract { returns(false) implies (this@isNotNullNorEmpty == null) }
22+
23+
return this != null && isNotEmpty()
24+
}

kotlin-stdlib/commonTest/kotlin/CollectionsTest.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@file:Suppress("MagicNumber")
22

3-
import com.javiersc.kotlin.stdlib.second
3+
package com.javiersc.kotlin.stdlib
4+
45
import kotlin.test.Test
56
import kotlin.test.assertTrue
67

@@ -12,4 +13,24 @@ class CollectionsTest {
1213
fun collection_second() {
1314
assertTrue { numbers.second() == 2 }
1415
}
16+
17+
@Test
18+
fun collection_third() {
19+
assertTrue { numbers.third() == 3 }
20+
}
21+
22+
@Test
23+
fun collection_forth() {
24+
assertTrue { numbers.forth() == 4 }
25+
}
26+
27+
@Test
28+
fun collection_fifth() {
29+
assertTrue { numbers.forth() == 5 }
30+
}
31+
32+
@Test
33+
fun collection_penultimate() {
34+
assertTrue { numbers.penultimate() == 9 }
35+
}
1536
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.javiersc.kotlin.stdlib
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertFalse
5+
import kotlin.test.assertTrue
6+
7+
class StringsTest {
8+
9+
@Test
10+
fun string_remove() {
11+
assertTrue { "Hello, World".remove("Hello, ") == "World" }
12+
}
13+
14+
@Test
15+
fun string_isNotNullNorEmpty_and_string_isNotNullNorBlank() {
16+
val nullable: String? = null
17+
val blank = " "
18+
val empty = ""
19+
val notBlank = "Hello, World"
20+
21+
assertFalse { nullable.isNotNullNorBlank() }
22+
assertFalse { nullable.isNotNullNorEmpty() }
23+
assertFalse { blank.isNotNullNorBlank() }
24+
assertTrue { blank.isNotNullNorEmpty() }
25+
assertFalse { empty.isNotNullNorBlank() }
26+
assertFalse { empty.isNotNullNorEmpty() }
27+
assertTrue { notBlank.isNotNullNorBlank() }
28+
assertTrue { notBlank.isNotNullNorEmpty() }
29+
}
30+
}

0 commit comments

Comments
 (0)