+
Skip to content

sergejsha/csv

Repository files navigation

Maven Central maintenance-status

Feel free to open PRs for features you miss, please remember keeping API minimalistic, predictable and self-explanatory.

🗂 CSV ️

Small, fast and convenient multiplatform CSV parser and builder written for one of my projects, and then shared with awesome Kotlin community.

Architecture

Usage

Here is what you can do with the library:

// (1) build csv
val csv = buildCsv {
    header {
        column("Code")
        column("Name")
    }
    data {
        value("DE")
        value("Deutschland")
    }
    data {
        value("BY")
        value("Belarus")
    }
} as CsvWithHeader

val code = csv.header.columnByName("Code") as CsvColumn
val name = csv.header.columnByName("Name") as CsvColumn

assertEquals(code, CsvColumn(0, "Code"))
assertEquals(name, CsvColumn(1, "Name"))
assertEquals(csv.data[0][code], "DE")
assertEquals(csv.data[0][name], "Deutschland")
assertEquals(csv.data[1][code], "BY")
assertEquals(csv.data[1][name], "Belarus")

// (2) csv to text
val csvText = csv.toCsvText()
assertEquals("Code,Name\nDE,Deutschland\nBY,Belarus\n", csvText)

// (3) parse csv text
val csv2 = CsvWithHeader.parseCsvText(csvText) as CsvWithHeader

assertEquals(csv.header, csv2.header)
assertEquals(csv.data, csv2.data)
assertEquals(csv.allRows, csv2.allRows)

// (4) transform csv
val csv3 = csv.copy(
    data = csv.data.map { row ->
        row.mapValueOf(name) { value ->
            if (value == "Belarus") "Weißrussland" else value
        }
    }
)
assertEquals("Code,Name\nDE,Deutschland\nBY,Weißrussland\n", csv3.toCsvText())

Dependencies

In gradle/libs.versions.toml

[versions]
kotlin = "2.2.0"
csv = "1.1"

[libraries]
csv = { module = "de.halfbit:csv", version.ref = "csv" }

[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }

In shared/build.gradle.kts

plugins {
    alias(libs.plugins.kotlin.multiplatform)
}

kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation(libs.csv)
        }
    }
}

Releasing

  1. Bump version in build.gradle.kts of the root project
  2. ./gradlew clean build releaseToMavenCentral

License

Copyright 2023-2025 Sergej Shafarenka, www.halfbit.de

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Tiny Kotlin Multiplatform library for parsing and building CSV strings

Topics

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •  

Languages

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载