Skip to content

Commit 085bd97

Browse files
committed
fix
1 parent d5cdc31 commit 085bd97

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/main/kotlin/com/github/t45k/kotlin_data_table/strTableToRow.kt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.github.t45k.kotlin_data_table
22

33
fun strTableToRow(table: String, delimiter: String = "|"): List<StrTableRow> {
44
return table.split(System.lineSeparator())
5+
.filter { it.isNotBlank() }
56
.map { line -> line.split(delimiter).map { it.trim() } }
67
.map { StrTableRow(it) }
78
}

src/main/kotlin/com/github/t45k/kotlin_data_table/strTableToRowWithName.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.t45k.kotlin_data_table
22

33
fun strTableToRowWithName(table: String, delimiter: String = "|"): List<StrTableRowWithName> {
4-
val lines = table.split(System.lineSeparator())
4+
val lines = table.split(System.lineSeparator()).filter { it.isNotBlank() }
55
val headerColumns = lines[0].split(delimiter).map { it.trim() }
66
return lines.subList(1, lines.size)
77
.map { line -> line.split(delimiter).map { it.trim() } }

src/test/kotlin/com/github/t45k/kotlin_data_table/TableTest.kt

+34
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ class TableTest {
5050
assertEquals(expected, actual)
5151
}
5252

53+
@Test
54+
fun `strTableToRow ignores empty line`() {
55+
val actual = strTableToRow(
56+
"""
57+
Bob | 27 | MALE
58+
59+
Alice | 34 | FEMALE
60+
61+
Alex | 1 | MALE
62+
"""
63+
).map { (name, age, gender) -> Person(name, age.toInt(), Gender.valueOf(gender)) }
64+
65+
assertEquals(expected, actual)
66+
}
67+
5368
@Test
5469
fun strTableToRowWithName() {
5570
val actual = strTableToRowWithName(
@@ -64,6 +79,25 @@ class TableTest {
6479
assertEquals(expected, actual)
6580
}
6681

82+
@Test
83+
fun `strTableToRowWithName ignores empty line`() {
84+
val actual = strTableToRowWithName(
85+
"""
86+
name | age | gender
87+
88+
Bob | 27 | MALE
89+
90+
Alice | 34 | FEMALE
91+
92+
Alex | 1 | MALE
93+
"""
94+
).map {
95+
Person(it["name"], it["age"].toInt(), Gender.valueOf(it["gender"]))
96+
}
97+
98+
assertEquals(expected, actual)
99+
}
100+
67101
private data class Person(val name: String, val age: Int, val gender: Gender)
68102

69103
private enum class Gender {

0 commit comments

Comments
 (0)