-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv2jsonstat
60 lines (56 loc) · 1.89 KB
/
csv2jsonstat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env node
var
utils=require("jsonstat-utils"),
argv=require("yargs")
.version()
.usage("Usage:\n $0 [input filename] [output filename]\n $0 < [input] > [output] -t")
.example("$0 oecd.csv oecd.json", "converts the CSV file oecd.csv into a new file (oecd.json) in the JSON-stat format.")
.example("$0 < oecd.csv > oecd.json -t", "converts the CSV stream oecd.csv into a new stream (oecd.json) in the JSON-stat format.")
.alias("c", "column")
.describe("c", "Column separator (default, ',')")
.alias("d", "decimal")
.describe("d", "Decimal separator (default, '.', unless column separator is ';', then default is ',')")
.alias("v", "vlabel")
.describe("v", "Name of the value column. When not provided, the value column must be the last one.")
.alias("l", "slabel")
.describe("l", "Name of the status column (default is 'Status'). If no column has the specified name, no status information will be included in the output.")
.alias("a", "label")
.describe("a", "Dataset label.")
.boolean("o")
.alias("o", "ovalue")
.describe("o", "Type of value: object instead of array")
.boolean("s")
.alias("s", "ostatus")
.describe("s", "Type of status: object instead of array")
.boolean("t")
.alias("t", "stream")
.describe("t", "Enable the stream interface")
.help("h")
.alias("h", "help")
.argv
,
inout=require("./inout"),
callback=function(contents){
var
json=utils.fromCSV(
contents,
{
vlabel: argv.vlabel,
slabel: argv.slabel,
delimiter: (argv.column==="\\t") ? "\t" : argv.column,
decimal: argv.decimal,
label: argv.label,
ovalue: argv.ovalue,
ostatus: argv.ostatus
}
)
;
if(json===null || !json.size.length || (json.size.length===1 && json.value[0]===null)){
console.error("Error: Check your CSV and the options you supplied.");
process.exit(1);
}else{
return json;
}
}
;
inout.main(argv, callback);