-
Notifications
You must be signed in to change notification settings - Fork 621
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
BREAKING(encoding/csv): add return type to csv's parse and remove a parse func from args #1724
Merged
kt3k
merged 6 commits into
denoland:main
from
mitch292:feature/remove-custom-parse-function-from-csv
Jan 20, 2022
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd78424
Adds a return type to csv's parse and removes a custom parse option f…
mitch292 ae8253e
Removes references to parse option from jsdoc
mitch292 6837d3f
Removes unnecessary variable declaration in csv's parse
mitch292 6452c7f
Remove parse option from columns for csv's parse function
mitch292 ec91cb8
Add function overloads to csv's parse function
mitch292 fd9e89f
Reconciles readme for csv's parse with current function state
mitch292 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,20 +334,12 @@ export async function readMatrix( | |
* 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; | ||
/** | ||
* Parse function for the column. | ||
* This is executed on each entry of the header. | ||
* This can be combined with the Parse function of the rows. | ||
*/ | ||
parse?: (input: string) => unknown; | ||
} | ||
|
||
export interface ParseOptions extends ReadOptions { | ||
|
@@ -361,44 +353,22 @@ export interface ParseOptions extends ReadOptions { | |
* If you provide `string[]` or `ColumnOptions[]`, those names will be used for header definition. | ||
*/ | ||
columns?: string[] | ColumnOptions[]; | ||
|
||
/** Parse function for rows. | ||
* Example: | ||
* ```ts | ||
* import { parse } from "./csv.ts"; | ||
* const r = await parse('a,b,c\ne,f,g\n', { | ||
* columns: ["this", "is", "sparta"], | ||
* parse: (_e: unknown) => { | ||
* const e = _e as { this: unknown, is: unknown, sparta: unknown }; | ||
* return { super: e.this, street: e.is, fighter: e.sparta }; | ||
* } | ||
* }); | ||
* // output | ||
* // [ | ||
* // { super: "a", street: "b", fighter: "c" }, | ||
* // { super: "e", street: "f", fighter: "g" } | ||
* // ] | ||
* ``` | ||
*/ | ||
parse?: (input: unknown) => unknown; | ||
} | ||
|
||
/** | ||
* Csv parse helper to manipulate data. | ||
* Provides an auto/custom mapper for columns and parse function | ||
* for columns and rows. | ||
* Provides an auto/custom mapper for columns. | ||
* @param input Input to parse. Can be a string or BufReader. | ||
* @param opt options of the parser. | ||
* @returns If you don't provide `opt.skipFirstRow`, `opt.parse`, and `opt.columns`, it returns `string[][]`. | ||
* If you provide `opt.skipFirstRow` or `opt.columns` but not `opt.parse`, it returns `object[]`. | ||
* If you provide `opt.parse`, it returns an array where each element is the value returned from `opt.parse`. | ||
* @returns If you don't provide `opt.skipFirstRow` and `opt.columns`, it returns `string[][]`. | ||
* If you provide `opt.skipFirstRow` or `opt.columns`, it returns `Record<string, unkown>[]`. | ||
*/ | ||
export async function parse( | ||
input: string | BufReader, | ||
opt: ParseOptions = { | ||
skipFirstRow: false, | ||
}, | ||
): Promise<unknown[]> { | ||
): Promise<string[][] | Record<string, unknown>[]> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if we do overloading like the below, it would be able to return more correct type depending of the options: export async function parse(
input: string | BufReader
): Promise<string[][]>;
export async function parse(
input: string | BufReader,
opt: Omit<ParseOptions, "columns" | "skipFirstRow">,
): Promise<string[][]>;
export async function parse(
input: string | BufReader,
opt: Omit<ParseOptions, "columns"> & {
columns: string[] | ColumnOptions[],
},
): Promise<Record<string, unknown>[]>;
export async function parse(
input: string | BufReader,
opt: Omit<ParseOptions, "skipFirstRow"> & {
skipFirstRow: true,
},
): Promise<Record<string, unknown>[]>;
export async function parse(
input: string | BufReader,
opt: ParseOptions = {
skipFirstRow: false,
},
): Promise<string[][] | Record<string, unknown>[]> { ... } |
||
let r: string[][]; | ||
if (input instanceof BufReader) { | ||
r = await readMatrix(input, opt); | ||
|
@@ -436,31 +406,18 @@ export async function parse( | |
); | ||
} | ||
} | ||
return r.map((e): unknown => { | ||
|
||
return r.map((e) => { | ||
if (e.length !== headers.length) { | ||
throw `Error number of fields line:${i}`; | ||
} | ||
i++; | ||
const out: Record<string, unknown> = {}; | ||
for (let j = 0; j < e.length; j++) { | ||
const h = headers[j]; | ||
if (h.parse) { | ||
out[h.name] = h.parse(e[j]); | ||
} else { | ||
out[h.name] = e[j]; | ||
} | ||
} | ||
if (opt.parse) { | ||
return opt.parse(out); | ||
out[headers[j].name] = e[j]; | ||
} | ||
return out; | ||
}); | ||
} | ||
if (opt.parse) { | ||
return r.map((e: string[]): unknown => { | ||
assert(opt.parse, "opt.parse must be set"); | ||
return opt.parse(e); | ||
}); | ||
} | ||
return r; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be reading too much into the comment thread in #881. To keep the logic consistent I removed the parse function from columns as well. Should this be done?
Now, if the parse is removed from column options maybe the function signature for
columns?: string[] | ColumnOptions[]
should just be simplified tocolumns?: string[]
as below will evaluate to the same.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm in favor of removing this