Skip to content

Commit

Permalink
Merge pull request #108 from doyaaaaaken/issue-97
Browse files Browse the repository at this point in the history
Refactoring about #97
  • Loading branch information
doyaaaaaken authored Aug 7, 2022
2 parents dbcfd46 + a628703 commit 70e8ee7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ csvWriter().openAsync(testFileName) {
}
```

#### Write as String

```kotlin
val rows = listOf(listOf("a", "b", "c"), listOf("d", "e", "f"))
val csvString: String = csvWriter().writeAllAsString(rows) //a,b,c\r\nd,e,f\r\n
```

#### long-running write (manual control for file close)

If you want to close a file writer manually for performance reasons (e.g. streaming scenario), you can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ actual class CsvWriter actual constructor(
writer.use { it.write() }
}

fun writeString(write: ICsvFileWriter.() -> Unit): String {
internal fun writeAsString(write: ICsvFileWriter.() -> Unit): String {
val baos = ByteArrayOutputStream()
open(baos, write)
return String(baos.toByteArray(), Charset.forName(ctx.charset))
Expand Down Expand Up @@ -144,7 +144,7 @@ actual class CsvWriter actual constructor(
/**
* write all rows to string
*/
fun writeAll(rows: List<List<Any?>>): String {
return writeString { writeRows(rows) }
fun writeAllAsString(rows: List<List<Any?>>): String {
return writeAsString { writeRows(rows) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ class CsvWriterTest : WordSpec({
}
}

"writeString method" should {
"writeAsString method" should {
val row1 = listOf("a", "b", null)
val row2 = listOf("d", "2", "1.0")
val expected = "a,b,\r\nd,2,1.0\r\n"

"write simple csv data to String" {
val actual = csvWriter().writeString {
val actual = csvWriter().writeAsString {
writeRow(row1)
writeRow(row2)
}
Expand Down Expand Up @@ -128,7 +128,7 @@ class CsvWriterTest : WordSpec({
}

"write data to String" {
val actual = csvWriter().writeAll(rows)
val actual = csvWriter().writeAllAsString(rows)
actual shouldBe expected
}
}
Expand Down

0 comments on commit 70e8ee7

Please sign in to comment.