Skip to content

Commit 2519f70

Browse files
committed
read csv unstrictly by default
1 parent 4b23e0a commit 2519f70

File tree

5 files changed

+14
-62
lines changed

5 files changed

+14
-62
lines changed

README.md

-27
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ Usage: ramda [options] [function] ...
362362
--csv-delimiter custom csv delimiter character
363363
--js use javascript instead of livescript
364364
--import import a module from npm
365-
-u, --csv-unstrict use with input type csv/tsv and headers to ignore column mismatch
366365
-C, --configure edit config in $EDITOR
367366
-v, --verbose print debugging information (use -vv for even more)
368367
--version print version
@@ -513,32 +512,6 @@ EOF
513512
]
514513
```
515514
516-
##### `-u, --csv-unstrict`
517-
518-
Ignore CSV/TSV column mismatch, useful with some POSIX commands.
519-
520-
__Examples__
521-
522-
```sh
523-
$ ps aux | sed -E 's/ +/,/g'
524-
USER,PID,%CPU,%MEM,VSZ,RSS,TTY,STAT,START,TIME,COMMAND
525-
root,1,0.0,0.0,8324,152,?,Ss,May28,0:00,/init
526-
root,3,0.0,0.0,8332,156,tty1,Ss,May28,0:00,/init
527-
pi,17039,0.0,0.0,17380,1940,tty3,R,18:15,0:00,ps,aux
528-
pi,17040,0.0,0.0,14860,1160,tty3,S,18:15,0:00,sed,-E,s/,+/,/g
529-
```
530-
531-
```sh
532-
$ ps aux | sed -E 's/ +/,/g' | ramda -i csv -p 'countBy (.USER)'
533-
Error: Unexpected Error: column header mismatch expected: 11 columns got: 12
534-
```
535-
536-
```sh
537-
$ ps aux | sed -E 's/ +/,/g' | ramda -i csv -u -p 'countBy (.USER)'
538-
{ root: 2, pi: 2 }
539-
```
540-
541-
542515
#### `-o, --output-type`
543516
544517
Instead of JSON, format output as one of: `pretty`, `raw`, `csv`, `tsv`, `table`.

src/argv.ls

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ HELP =
3030
--csv-delimiter custom csv delimiter character
3131
--js use javascript instead of livescript
3232
--import import a module from npm
33-
-u, --csv-unstrict use with inupt type csv/tsv and headers to ignore column mismatch
3433
-C, --configure edit config in $EDITOR
3534
-v, --verbose print debugging information (use -vv for even more)
3635
--version print version
@@ -76,7 +75,7 @@ export parse = (argv) ->
7675
argv = map (wrap-number-lookup << wrap-function), argv.slice 2
7776
opts = camelize minimist argv,
7877
string: <[ file input-type output-type json-path csv-delimiter ]>
79-
boolean: <[ compact slurp unslurp pretty verbose version raw-input raw-output configure no-stdin js transduce headers interactive csv-unstrict ]>
78+
boolean: <[ compact slurp unslurp pretty verbose version raw-input raw-output configure no-stdin js transduce headers interactive ]>
8079
alias: parse-aliases HELP
8180
default: {
8281
+stdin,

src/stream.ls

+9-5
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ blank-obj-stream = ->
5959
PassThrough {+object-mode}
6060
..end {}
6161

62-
csv-opts = (type, delimiter, headers, unstrict = false) ->
63-
opts = { headers, include-end-row-delimiter: true, delimiter }
64-
if headers and unstrict
65-
opts <<< { +discardUnmappedColumns, -strictColumnHandling }
62+
csv-opts = (type, delimiter, headers, unstrict = true) ->
63+
opts = {
64+
headers,
65+
delimiter,
66+
+include-end-row-delimiter,
67+
+discard-unmapped-columns,
68+
-strict-column-handling
69+
}
6670
switch type
6771
| \csv => opts
6872
| \tsv => opts <<< delimiter: '\t'
@@ -100,7 +104,7 @@ opts-to-output-stream = (opts) ->
100104
opts-to-input-parser-stream = (opts) ->
101105
switch opts.input-type
102106
| \raw => split2!
103-
| <[ csv tsv ]> => (require 'fast-csv') csv-opts opts.input-type, opts.csv-delimiter, opts.headers, opts.csv-unstrict
107+
| <[ csv tsv ]> => (require 'fast-csv') csv-opts opts.input-type, opts.csv-delimiter, opts.headers
104108
| otherwise => JSONStream.parse opts.json-path
105109

106110
make-stdin-parser = (on-error, opts, stdin) ->

test/unit/argv.ls

-8
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,3 @@ describe 'argv.parse' (,) ->
9999
it 'reads ".0" as string' ->
100100
args = argv.parse [,, '.0', '.foo']
101101
args._.0 `eq` '(.0)'
102-
103-
describe '-u, --unstrict', (,) ->
104-
it 'defaults to false' ->
105-
parse '' .csv-unstrict `eq` false
106-
it 'set csv-unstrict to true, short' ->
107-
parse '-u' .csv-unstrict `eq` true
108-
it 'set csv-unstrict to true' ->
109-
parse '--csv-unstrict' .csv-unstrict `eq` true

test/unit/basic.ls

+4-20
Original file line numberDiff line numberDiff line change
@@ -337,39 +337,23 @@ describe '--input-type csv' (,) ->
337337
output `strip-eq` expected
338338
done!
339339

340-
it 'reads csv with headers unstrict' (done) ->
341-
args = <[ -i csv -u identity ]>
340+
it 'with headers, ignores extra columns that don\'t have a header' (done) ->
341+
args = <[ -i csv identity ]>
342342
input = """
343343
name,code
344344
Afghanistan,AF,XX
345345
Åland Islands,AX
346-
Albania
346+
Albania,AL
347347
"""
348348
expected = """
349349
[ { "name": "Afghanistan", "code": "AF" },
350350
{ "name": "Åland Islands", "code": "AX" },
351-
{ "name": "Albania", "code": "" } ]
351+
{ "name": "Albania", "code": "AL" } ]
352352
"""
353353
output <-! run-main args, input
354354
output `strip-eq` expected
355355
done!
356356

357-
describe '--input-type csv/tsv default strict' (,) ->
358-
stub-process-exit!
359-
it 'strict mode, header mismatch' (done) ->
360-
args = <[ -i csv ]>
361-
input = """
362-
Afghanistan,AF
363-
Åland Islands,AX,XX
364-
Albania,AL
365-
"""
366-
expected = """
367-
Error: Unexpected Error: column header mismatch expected: 2 columns got: 3\n
368-
"""
369-
output, errput <-! run-main args, input
370-
errput `eq` expected
371-
done!
372-
373357
describe '--input-type tsv' (,) ->
374358
it 'reads tsv with headers into a list of objects' (done) ->
375359
args = <[ -i tsv identity ]>

0 commit comments

Comments
 (0)