Sign in

    NyaCSV

    Purr-fectly parse your CSV files with delightful simplicity!

    CSV
    Parser
    Download zip
    Version
    0.3.3
    License
    Apache-2.0
    Last updated
    5 months ago
    Downloads
    3K

    #NyaCSV

    Parse CSV files in MoonBit with a small, focused API.

    Build Status License codecov

    NyaCSV parses CSV data from strings, buffers, and bytes. It supports custom delimiters, quoted fields, multiline fields, optional whitespace trimming, header extraction, generated headers, and formatted output.

    #Features

    • Multiple input sources: parse CSV from String, Buffer, or Bytes.
    • Configurable parsing: set delimiter, quote character, whitespace trimming, empty-line handling, and multiline quoted fields.
    • Header handling: use the first row as headers or generate column names.
    • Simple accessors: read headers, rows, and shape through public methods.
    • Formatted output: render a table through the Show implementation.

    #Quick Start

    ///|
    test "readme/quick_start" {
    let data =
    #|Name,Age,Favorite Toy
    #|Mittens,3,Ball of yarn
    #|Whiskers,5,Laser pointer
    #|
    let csv = CSV::parse_string(data)
    let (rows, columns) = csv.shape()
    assert_eq(rows, 2)
    assert_eq(columns, 3)
    assert_eq(csv.header()[0], "Name")
    assert_eq(csv.header()[2], "Favorite Toy")
    assert_eq(csv.data()[0][0], "Mittens")
    assert_eq(csv.data()[1][2], "Laser pointer")
    }

    #Installation

    Add NyaCSV to your MoonBit project with moon add moonbit-community/NyaCSV.

    #Usage Examples

    #Parsing CSV with Custom Options

    ///|
    test "readme/custom_options" {
    let data =
    #|Name;Description;Adopted
    #|Simba;"Playful orange tabby
    #|Loves treats";Yes
    #|
    let csv = CSV::parse_string(data, options={
    delimiter: ';',
    allow_newlines_in_quotes: true,
    quote_char: '"',
    skip_empty_lines: true,
    trim_spaces: true,
    })
    assert_eq(csv.header()[0], "Name")
    assert_eq(csv.header()[1], "Description")
    assert_eq(csv.data()[0][0], "Simba")
    let description =
    #|Playful orange tabby
    #|Loves treats
    assert_eq(csv.data()[0][1], description)
    assert_eq(csv.data()[0][2], "Yes")
    }

    #Creating CSV Data

    ///|
    test "readme/create_from_array" {
    let data : Array[Array[String]] = [
    ["Name", "Age"],
    ["Luna", "2"],
    ["Oliver", "1"],
    ]
    let csv = CSV::from_array(data)
    assert_eq(csv.header().length(), 2)
    assert_eq(csv.data().length(), 2)
    assert_eq(csv.header()[0], "Name")
    assert_eq(csv.data()[0][0], "Luna")
    let expected =
    #|Name,Age
    #|Luna,2
    #|Oliver,1
    #|
    assert_eq(csv.to_string(), expected)
    }

    #Working with 2D Arrays

    ///|
    test "readme/array_input" {
    let shelter_data : Array[Array[String]] = [
    ["Name", "Age", "Color", "Adopted"],
    ["Luna", "2", "Black", "Yes"],
    ["Oliver", "1", "Tabby", "No"],
    ["Bella", "4", "Calico", "Yes"],
    ]
    let csv = CSV::from_array(shelter_data)
    let (rows, columns) = csv.shape()
    assert_eq(rows, 3)
    assert_eq(columns, 4)
    assert_eq(csv.data()[2][0], "Bella")
    assert_eq(csv.data()[2][3], "Yes")
    }

    #Generated Headers

    ///|
    test "readme/generated_headers" {
    let data : Array[Array[String]] = [
    ["Alice", "30", "Wonderland"],
    ["Bob", "25", "Builderland"],
    ]
    let csv = CSV::from_array(data, has_header=false, generate_headers=true)
    assert_eq(csv.header()[0], "column1")
    assert_eq(csv.header()[1], "column2")
    assert_eq(csv.header()[2], "column3")
    assert_eq(csv.data()[1][0], "Bob")
    }

    #API Reference

    #CSV

    CSV is the parsed CSV value. Its fields are private; use header(), data(), shape(), to_string(), and output(logger) to work with it.

    #CSVOptions

    CSVOptions controls parsing:

    • delimiter : Char
    • allow_newlines_in_quotes : Bool
    • quote_char : Char
    • skip_empty_lines : Bool
    • trim_spaces : Bool

    #Parsing Methods

    • CSV::parse_string(data : String, options? : CSVOptions) -> CSV
    • CSV::parse_buffer(data : Buffer, options? : CSVOptions) -> CSV
    • CSV::parse_bytes(data : Bytes, options? : CSVOptions) -> CSV

    #Creation Methods

    • CSV::new() -> CSV
    • CSV::from_array(data : Array[Array[String]], has_header? : Bool, generate_headers? : Bool) -> CSV

    #Advanced Examples

    #Formatted Table Output

    ///|
    test "readme/formatted_table_output" {
    let data =
    #|Breed,Origin,Size,Temperament
    #|Siamese,Thailand,Medium,"Vocal, Active"
    #|Maine Coon,USA,Large,"Gentle, Playful"
    #|Ragdoll,USA,Large,"Calm, Affectionate"
    #|
    let csv = CSV::parse_string(data)
    let logger = @buffer.new()
    csv.output(logger)
    let table = logger.contents().to_unchecked_string()
    assert_true(table.contains("Breed"))
    assert_true(table.contains("Maine Coon"))
    assert_true(table.contains("Gentle, Playful"))
    }

    #Handling Multiline Fields

    ///|
    test "readme/multiline_fields" {
    let data =
    #|Name,Bio
    #|Lucy,"Lucy is curious.
    #|She loves exploring."
    #|Max,"Max is quiet.
    #|He sleeps often."
    #|
    let csv = CSV::parse_string(data)
    assert_eq(csv.header()[1], "Bio")
    let lucy_bio =
    #|Lucy is curious.
    #|She loves exploring.
    let max_bio =
    #|Max is quiet.
    #|He sleeps often.
    assert_eq(csv.data()[0][1], lucy_bio)
    assert_eq(csv.data()[1][1], max_bio)
    }

    #Contribution

    Contributions are welcome. Please open issues and pull requests on GitHub.

    #License

    Apache-2.0

    CSV

    type CSV

    Represents a CSV (Comma-Separated Values) data structure with headers and rows.

    Parameters:

    • headers : An array of strings representing the column names in the CSV.
    • rows : A two-dimensional array where each inner array represents a row of data, with each element corresponding to a column defined in the headers.

    impl Show for CSV

    CSV::data

    fn CSV::data(self : CSV) -> Array[Array[String]]

    data of the CSV.

    CSV::from_array

    fn CSV::from_array(data : Array[Array[String]], has_header? : Bool, generate_headers? : Bool) -> CSV

    CSV::header

    fn CSV::header(self : CSV) -> Array[String]

    header of the CSV.

    CSV::new

    fn CSV::new() -> CSV

    Creates a new empty CSV structure with no headers and no rows.

    Returns a new CSV instance with empty headers and rows arrays.

    CSV::parse_buffer

    fn CSV::parse_buffer(data :
    Buffer
    , options? : CSVOptions) -> CSV

    Parses a buffer containing CSV data into a structured CSV object.

    Parameters:

    • buffer : A buffer containing the CSV data to be parsed.
    • options : A configuration object of type CSVOptions that controls parsing behavior with the following fields:
    • delimiter : The character used to separate fields (default: ',')
    • allow_newlines_in_quotes : Whether to allow newlines within quoted fields (default: true)
    • quote_char : The character used for quoting fields (default: '"')
    • skip_empty_lines : Whether to ignore empty lines in the input (default: true)
    • trim_spaces : Whether to remove leading and trailing whitespace from fields (default: false)

    Returns a CSV object containing the parsed data, with headers from the first row and subsequent rows as data.

    CSV::parse_bytes

    fn CSV::parse_bytes(data : Bytes, options? : CSVOptions) -> CSV

    Parses binary data containing CSV content into a structured CSV object.

    Parameters:

    • data : A sequence of bytes containing the CSV content to be parsed.
    • options : A configuration object that controls parsing behavior with the following fields:
    • delimiter : The character used to separate fields (default: ',')
    • allow_newlines_in_quotes : Whether to allow newlines within quoted fields (default: true)
    • quote_char : The character used for quoting fields (default: '"')
    • skip_empty_lines : Whether to ignore empty lines in the input (default: true)
    • trim_spaces : Whether to remove leading and trailing whitespace from fields (default: false)

    Returns a CSV object containing the parsed data, with headers from the first row and subsequent rows as data.

    CSV::parse_string

    fn CSV::parse_string(data : String, options? : CSVOptions) -> CSV

    Parses a CSV (Comma-Separated Values) string into a structured CSV object.

    Parameters:

    • csv_string : A string containing CSV data to be parsed.
    • options : A configuration object of type CSVOptions that controls the parsing behavior with the following fields:
    • delimiter : The character used to separate fields (default: ',')
    • allow_newlines_in_quotes : Whether to allow newlines within quoted fields (default: true)
    • quote_char : The character used for quoting fields (default: '"')
    • skip_empty_lines : Whether to ignore empty lines in the input (default: true)
    • trim_spaces : Whether to remove leading and trailing whitespace from fields (default: false)

    Returns a CSV object containing the parsed data, with headers from the first row and subsequent rows as data.

    CSV::shape

    fn CSV::shape(self : CSV) -> (Int, Int)

    shape of the CSV.

    CSVOptions

    pub(all) struct CSVOptions {
    delimiter : Char
    allow_newlines_in_quotes : Bool
    quote_char : Char
    skip_empty_lines : Bool
    trim_spaces : Bool
    }

    Represents configuration options for CSV parsing operations.

    Parameters:

    • delimiter : Character used to separate fields in the CSV data (typically a comma ',').
    • allow_newlines_in_quotes : Boolean flag indicating whether newline characters are allowed within quoted fields.
    • quote_char : Character used for quoting fields that contain special characters (typically a double quote '"').
    • skip_empty_lines : Boolean flag indicating whether empty lines in the input should be ignored.
    • trim_spaces : Boolean flag indicating whether leading and trailing whitespace should be removed from fields.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io