https://github.com/grantmacken/csv
Yet another CSV xQuery library
https://github.com/grantmacken/csv
arrow-functions csv-converter existdb financial-data xquery
Last synced: 2 months ago
JSON representation
Yet another CSV xQuery library
- Host: GitHub
- URL: https://github.com/grantmacken/csv
- Owner: grantmacken
- Created: 2019-02-23T06:02:22.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-09T07:59:35.000Z (over 7 years ago)
- Last Synced: 2025-03-05T04:29:39.117Z (over 1 year ago)
- Topics: arrow-functions, csv-converter, existdb, financial-data, xquery
- Language: Shell
- Homepage:
- Size: 48.8 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Support: .github/SUPPORT.md
Awesome Lists containing this project
README
# yet another CSV xQuery Library module
[](https://travis-ci.org/grantmacken/csv)
[](https://gitHub.com/grantmacken/csv/releases/latest)
The csv xQuery library provides functions turn a CSV file into an array of
arrays. You can visualise the array of arrays as being like a spreadsheet.
We have multiple records where each record is in a row and each record contains fields in field column.
[](https://asciinema.org/a/232587)
## Use Case
I intend to use this library for handling downloaded CSV financial statements.
## Basics
Start some CSV properties,
in the form of a key-value map.
Then this map to create an array of arrays
```
let $map := map {
'href' : '/db/unit-tests/fixtures/2018-12.csv',
'header-line': 6,
'record-start': 8,
'separator': ','
}
return
$map => csv:lines() => csv:toArray($map)
```
## Convenience Functions
The lib provides a simple mapping of the CSV header line to the fields index integer
So if we have a header named 'amount' we can 'sum' the amount field column.
e.g.
```
let $lines := $map => csv:lines()
let $field := $lines => csv:mapFields($map)
let $records := $lines => csv:toArray($map)
let $sumAmount := string(sum( $records?*?($field('amount')) ! number(.)))
```
The library also provides some formating alignment functions so when rendering on a terminal all the field columns will be lined up.
```
let $lines := $map => csv:lines()
let $field := $lines => csv:mapFields($map)
let $records := $lines => csv:toArray($map)
let $width := $records => csv:colWidth($field)
return (
for $record in $records?*
let $date := csv:pad($record?($field('date')), $width('date') )
let $payee := csv:pad($record?($field('payee')), $width('payee') )
let $amount := csv:pad($record?($field('amount')), $width('amount') )
return (
string-join(($date,$payee,$amount,$nl),' ')
)
```
The following asciicast showcases the above mentioned field column alignment and
performing a sum calculation on the field column.
The CSV data comes from a downloaded monthly statement and is found in the unit-tests/fixtures folder
[](https://asciinema.org/a/232385)