Splitting CSV into header and body.

This commit is contained in:
Dominik Pantůček 2023-03-30 13:41:21 +02:00
parent 5f1ad3e756
commit 71e016a3aa

View file

@ -29,6 +29,7 @@
csv-simple
(
csv-parse
csv-split-header
csv-simple-tests!
)
@ -86,6 +87,19 @@
(csv-parse-line (make-csv-line-parser separator string-delimiter)))
(map csv-parse-line lines)))
;; Splits CSV into header and body based on the first empty row.
(define (csv-split-header csv)
(let loop ((body csv)
(rhead '()))
(if (null? body)
(list (reverse rhead) '())
(let ((row (car body)))
(if (null? row)
(list (reverse rhead)
(cdr body))
(loop (cdr body)
(cons row rhead)))))))
;; Module self-tests
(define (csv-simple-tests!)
(run-tests
@ -96,6 +110,12 @@
(test-equal? csv-parse-line
((make-csv-line-parser ";" "\"") "test;\"2;quoted\";3")
'("test" "2;quoted" "3"))
(test-equal? csv-split-header
(csv-split-header '((1 2) () (3 4)))
'(((1 2)) ((3 4))))
(test-equal? csv-split-header
(csv-split-header '((1 2) (5 6) (3 4)))
'(((1 2) (5 6) (3 4)) ()))
))
)