Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(encoding/csv): remove ColumnOptions #2536

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 6 additions & 37 deletions encoding/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,6 @@ export async function stringify(
return output;
}

/**
* Parse the CSV string/buffer with the options provided.
*
* ColumnOptions provides the column definition
* and the parse function for each entry of the
* column.
*/
export interface ColumnOptions {
/**
* Name of the column to be used as property
*/
name: string;
}

export interface ParseOptions extends ReadOptions {
/**
* If you provide `skipFirstRow: true` and `columns`, the first line will be skipped.
Expand All @@ -216,7 +202,7 @@ export interface ParseOptions extends ReadOptions {
/**
* If you provide `string[]` or `ColumnOptions[]`, those names will be used for header definition.
*/
columns?: string[] | ColumnOptions[];
columns?: string[];
}

/**
Expand All @@ -237,7 +223,7 @@ export function parse(
export function parse(
input: string,
opt: Omit<ParseOptions, "columns"> & {
columns: string[] | ColumnOptions[];
columns: string[];
},
): Record<string, unknown>[];
export function parse(
Expand All @@ -260,35 +246,18 @@ export function parse(
const r = parser.parse(input);

if (opt.skipFirstRow || opt.columns) {
let headers: ColumnOptions[] = [];
let headers: string[] = [];
let i = 0;

if (opt.skipFirstRow) {
const head = r.shift();
assert(head != null);
headers = head.map(
(e): ColumnOptions => {
return {
name: e,
};
},
);
headers = head;
i++;
}

if (opt.columns) {
if (typeof opt.columns[0] !== "string") {
headers = opt.columns as ColumnOptions[];
} else {
const h = opt.columns as string[];
headers = h.map(
(e): ColumnOptions => {
return {
name: e,
};
},
);
}
headers = opt.columns;
}

return r.map((e) => {
Expand All @@ -300,7 +269,7 @@ export function parse(
i++;
const out: Record<string, unknown> = {};
for (let j = 0; j < e.length; j++) {
out[headers[j].name] = e[j];
out[headers[j]] = e[j];
}
return out;
});
Expand Down
21 changes: 3 additions & 18 deletions encoding/csv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,22 +742,7 @@ Deno.test({
);
},
});
await t.step({
name: "header mapping object",
fn() {
const input = "a,b,c\ne,f,g\n";
const output = [
{ this: "a", is: "b", sparta: "c" },
{ this: "e", is: "f", sparta: "g" },
];
assertEquals(
parse(input, {
columns: [{ name: "this" }, { name: "is" }, { name: "sparta" }],
}),
output,
);
},
});

await t.step({
name: "provides both opts.skipFirstRow and opts.columns",
fn() {
Expand All @@ -769,7 +754,7 @@ Deno.test({
assertEquals(
parse(input, {
skipFirstRow: true,
columns: [{ name: "foo" }, { name: "bar" }, { name: "baz" }],
columns: ["foo", "bar", "baz"],
}),
output,
);
Expand All @@ -783,7 +768,7 @@ Deno.test({
() =>
parse(input, {
skipFirstRow: true,
columns: [{ name: "foo" }, { name: "bar" }, { name: "baz" }],
columns: ["foo", "bar", "baz"],
}),
Error,
"Error number of fields line: 1\nNumber of fields found: 3\nExpected number of fields: 2",
Expand Down