Skip to content

Commit 5e142ac

Browse files
author
Irene Alvarado
committed
Simplify the csv examples API by forcing to returna and write object arrays
1 parent 15787d5 commit 5e142ac

File tree

3 files changed

+44
-47
lines changed

3 files changed

+44
-47
lines changed

csv.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import { parse, ParseOptions } from 'https://deno.land/[email protected]/encoding/csv.ts'
2+
import { DataItem, stringify } from 'https://deno.land/[email protected]/encoding/csv.ts';
3+
4+
export async function readCSV(path: string, options?: ParseOptions): Promise<Record<string, unknown>[]> {
5+
if (!options || !('skipFirstRow' in options)) {
6+
// to force library to be consistent and return an array of objects
7+
options = {
8+
...options,
9+
skipFirstRow: true
10+
}
11+
}
212

3-
export async function readCSV(path: string, options?: ParseOptions): Promise<unknown> {
413
const raw = await Deno.readTextFile(path)
514
const content = await parse(raw, options)
6-
return content
15+
return content as Record<string, unknown>[]
716
}
817

9-
export async function writeCSV(path: string, data: string) {
10-
await Deno.writeTextFile(path, data);
18+
export async function writeCSV(path: string, data: Record<string, unknown>[]) {
19+
const headers = Object.keys(data[0])
20+
// we have to stringify the data with a row header
21+
const dataString = await stringify(data as DataItem[], headers)
22+
23+
await Deno.writeTextFile(path, dataString);
1124
}

examples/csv/csv-example.ts

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
1-
import { Column, DataItem, stringify } from 'https://deno.land/[email protected]/encoding/csv.ts';
2-
import { readCSV, writeCSV } from 'https://deno.land/x/[email protected]/mod.ts'
1+
import { readCSV, writeCSV } from '../../csv.ts'
32

43
// Path to a csv file
54
const csvPath = './examples/csv/prices.csv';
65

76
/*
8-
Parse a csv file and return a string[][]
7+
Parse a csv file and return an object[]
98
109
[
11-
[ "Name", "Amount", "Price" ],
12-
[ "One", "500", "$0.5" ],
13-
[ "Two", "13", "$10" ],
14-
[ "Three", "-3", "$3000" ]
10+
{ Name: "One", Amount: "500", Price: "$0.5" },
11+
{ Name: "Two", Amount: "13", Price: "$10" },
12+
{ Name: "Three", Amount: "-3", Price: "$3000" }
1513
]
1614
*/
1715
const originalCSV = await readCSV(csvPath)
1816
console.log(originalCSV)
1917

2018
/*
21-
Parse a CSV file and skip the first row. Return an object[]
22-
23-
[
24-
{ Name: "One", Amount: "500", Price: "$0.5" },
25-
{ Name: "Two", Amount: "13", Price: "$10" },
26-
{ Name: "Three", Amount: "-3", Price: "$3000" }
27-
]
19+
Can use other options for reading CSV
20+
More detail on options can be found here: https://deno.land/[email protected]/encoding#csv
2821
*/
29-
const skipFirstRowCSV = await readCSV(csvPath, {
30-
skipFirstRow: true
31-
// separator: ',' // can use an optional separator. default is comma
32-
// trimLeadingSpace: false, // whether to trim the leading space. default is false
33-
// lazyQuotes: false // Allow unquoted quote in a quoted field or non double quoted quotes in quoted field. default is false
22+
const csvOptions = await readCSV(csvPath, {
23+
separator: ',', // can use an optional separator. default is comma
24+
trimLeadingSpace: false, // whether to trim the leading space. default is false
25+
lazyQuotes: false // Allow unquoted quote in a quoted field or non double quoted quotes in quoted field. default is false
3426
})
35-
console.log(skipFirstRowCSV);
27+
console.log(csvOptions);
3628

3729
/*
3830
Parse a CSV file, skip the first row, and rename the column headers. Return an object[]
@@ -44,7 +36,6 @@ Parse a CSV file, skip the first row, and rename the column headers. Return an o
4436
]
4537
*/
4638
const renameColumnsCSV = await readCSV(csvPath, {
47-
skipFirstRow: true,
4839
columns: ['id', 'quantity', 'cost'],
4940
});
5041
console.log(renameColumnsCSV);
@@ -59,7 +50,6 @@ Parse a CSV file, skip the first row, and apply a custom function to the second
5950
]
6051
*/
6152
const parseColumnCSV = await readCSV(csvPath, {
62-
skipFirstRow: true,
6353
columns: [
6454
{
6555
name: 'id'
@@ -78,27 +68,23 @@ const parseColumnCSV = await readCSV(csvPath, {
7868
console.log(parseColumnCSV)
7969

8070
/*
81-
Write data to a file with a header row
71+
Write data to a CSV file
8272
83-
name,age
84-
Rick,70
85-
Smith,14
73+
age,name
74+
70,Rick
75+
14,Smith
8676
*/
8777
const data = [
88-
{
78+
{
8979
age: 70,
90-
name: "Rick"
80+
name: 'Rick'
9181
},
9282
{
9383
age: 14,
94-
name: "Smith"
95-
},
96-
];
97-
const columns: Column[] = ["name", "age"];
98-
99-
// we have to stringify the data with a row header
100-
const dataString = await stringify(data, columns)
101-
writeCSV('./examples/csv/names.csv', dataString)
84+
name: 'Smith'
85+
}
86+
]
87+
writeCSV('./examples/csv/names.csv', data)
10288

10389
/*
10490
Write one of the previously parsed csv examples
@@ -108,7 +94,5 @@ One,50,$0.5
10894
Two,1.3,$10
10995
Three,-0.3,$3000
11096
*/
111-
const data2 = parseColumnCSV as DataItem[]; // have to recast the output
112-
const columns2: Column[] = ["id", "quantity", "cost"];
113-
const dataString2 = await stringify(data2, columns2)
114-
writeCSV('./examples/csv/prices-write.csv', dataString2)
97+
console.log(parseColumnCSV)
98+
writeCSV('./examples/csv/prices-write.csv', parseColumnCSV)

examples/csv/names.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
name,age
2-
Rick,70
3-
Smith,14
1+
age,name
2+
70,Rick
3+
14,Smith

0 commit comments

Comments
 (0)