@dbl-works/csv-unique-values
Advanced tools
Comparing version 1.0.4 to 1.1.0
52
index.js
@@ -17,3 +17,3 @@ #!/usr/bin/env node | ||
'-c, --columns <columns>', | ||
'columns to extract, comma-separated, all by default' | ||
'columns to extract, comma-separated\nyou can get unique values per unique values from another column by\nchaining them together using `::`\n(default: all columns)' | ||
) | ||
@@ -47,11 +47,22 @@ .option( | ||
.pipe(parser({ separator: program.opts().delimiter })) | ||
.on('data', (data) => { | ||
.on('data', (row) => { | ||
const columns = program.opts().columns | ||
? program.opts().columns.split(',') | ||
: Object.keys(data); | ||
: Object.keys(row); | ||
columns.forEach((column) => { | ||
const value = data[column]; | ||
const groups = column.split('::'); | ||
if (typeof value === 'undefined') { | ||
if (groups.length === 1) { | ||
const value = row[column]; | ||
if (typeof value === 'undefined') { | ||
return; | ||
} | ||
if (!uniqueValues[column]) { | ||
uniqueValues[column] = new SetWithToJSONSupport(); | ||
} | ||
uniqueValues[column].add(value); | ||
return; | ||
@@ -61,6 +72,30 @@ } | ||
if (!uniqueValues[column]) { | ||
uniqueValues[column] = new SetWithToJSONSupport(); | ||
uniqueValues[column] = {}; | ||
} | ||
uniqueValues[column].add(value); | ||
let target = uniqueValues[column]; | ||
groups.forEach((group, i) => { | ||
const value = row[group]; | ||
if (typeof value === 'undefined') { | ||
return; | ||
} | ||
if (i === groups.length - 1) { | ||
target.add(value); | ||
} else if (i === groups.length - 2) { | ||
if (!target[value]) { | ||
target[value] = new SetWithToJSONSupport(); | ||
} | ||
target = target[value]; | ||
} else { | ||
if (!target[value]) { | ||
target[value] = {}; | ||
} | ||
target = target[value]; | ||
} | ||
}); | ||
}); | ||
@@ -93,6 +128,7 @@ }) | ||
const output = fs.createWriteStream( | ||
path.resolve(parsedOutputPath, `${column}.json`) | ||
path.resolve(parsedOutputPath, `${column.replace(/::/gi, '-')}.json`) | ||
); | ||
output.write(json); | ||
}); | ||
}); |
{ | ||
"name": "@dbl-works/csv-unique-values", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Get unique values from a CSV file", | ||
@@ -5,0 +5,0 @@ "bin": "./index.js", |
@@ -20,3 +20,6 @@ # csv-unique-values | ||
-d, --delimiter <delimiter> delimiter used in the CSV (default: ",") | ||
-c, --columns <columns> columns to extract, comma-separated, all by default | ||
-c, --columns <columns> columns to extract, comma-separated | ||
you can get unique values per unique values from another column by | ||
chaining them together using `::` | ||
(default: all columns) | ||
-o, --output <directory> output destination, will create a file per column | ||
@@ -38,6 +41,8 @@ --no-sort don't sort unique values | ||
```csv | ||
Name, Country | ||
Joe, US | ||
John, US | ||
John, DE | ||
Name, Country, Region | ||
Joe, US, NA | ||
Joelle, US, NA | ||
John, CA, NA | ||
Johan, DE, EU | ||
Johanna, DE, EU | ||
``` | ||
@@ -53,2 +58,3 @@ | ||
[ | ||
"CA", | ||
"DE", | ||
@@ -58,1 +64,19 @@ "US" | ||
``` | ||
To get all countries *per region* you can run: | ||
```npx @dbl-works/csv-unique-values --input team.csv --keys region::country --output ~/Desktop/team-country``` | ||
and you will get: | ||
```json | ||
{ | ||
"NA": [ | ||
"CA", | ||
"US", | ||
], | ||
"EU": [ | ||
"DE" | ||
] | ||
} | ||
``` |
6384
118
79